0
public class MainActivity extends AppCompatActivity {
    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list=(ListView) findViewById(R.id.listview);

        String[] months={"January","Feburary","March","April","May","June","July","August","September","October","November","December"};

        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,android.R.id.text1,months);
        list.setAdapter(adapter);

//i want here to use the positions of rows of listview

//Actually i want that whenever activity open, background color of odd listview items should be of black color and even should be of white background color.

//with simple adapter and without using any click listener.

}
}
Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
  • 2
    Why not use click listeners? Why do you think click listeners were made? It was made for this. – Jacob Celestine Sep 11 '18 at 12:23
  • //Actually i want that whenever activity open, background color of odd listview items should be of black color and even should be of white background color. for this you have to make custom Adapter – Ashutosh Sep 11 '18 at 12:27
  • Actually i want that whenever activity open, odd listview items should have same background color but different than even rows. how can i do this without clicking on any row item??? – Syed Huzaifa Bukhari Sep 11 '18 at 12:27
  • i want it to be done by simple adapter. Can it be possible with simple adapter? – Syed Huzaifa Bukhari Sep 11 '18 at 12:30

4 Answers4

0

All you need is getItemViewType which will allow you to inflate multiple views within a recyclerview.

In your adapter you can write:

//Returns the view type of the item at position for the purposes of view recycling.
  @Override
  public int getItemViewType(int position) {
      if (position%2 == 0) {
          //set even color
      } else {
          //set odd color
      }
      return -1;
  }

See this and this

kgandroid
  • 5,507
  • 5
  • 39
  • 69
0

try this idea

for ( int i = 0 ; i < listView.getCount() ; i++){
    View v = getViewByPosition(i,listView);
    // then check if i is odd set background color with what u want
    v.setBackgroundColor(Color.Parse("#000000"));
}

public View getViewByPosition(int position, ListView listView) {
    final int firstListItemPosition = listView.getFirstVisiblePosition();
    final int lastListItemPosition =firstListItemPosition + 
    listView.getChildCount() - 1;

    if (position < firstListItemPosition || position > lastListItemPosition ) {
    return listView.getAdapter().getView(position, 
    listView.getChildAt(position), listView);
    } else {
    final int childIndex = position - firstListItemPosition;
    return listView.getChildAt(childIndex);
    }
}
Mostafa Anter
  • 3,445
  • 24
  • 26
0
    public class BlackWhiteAdapter extends ArrayAdapter<String> {

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       if (convertView == null) {
          convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
       }
       String txt = getItem(position);  
       TextView tvMonth = (TextView) convertView.findViewById(android.R.id.text1);
       tvMonth.setTextColor((position%2==0) 0xFF00000 : 0xFFFFFFFF);
       return convertView;
   }
}
Meltzer
  • 127
  • 2
  • 10
0

When you setAdapter() on an AdapterView for example ListView, getView() is called internally and the views returned from the same are populated in the AdapterView Overriding getView method u can get selected value

String[] months={"January","Feburary","March","April","May","June","July","August","September","October","November","December"};

          String selectedMonth ;
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,android.R.id.text1,months){
            @NonNull
            @Override
            public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                View v = super.getView(position, convertView, parent);
                 v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    selectedMonth = months[position];
                }
            });
                return v;
            }
        };