MachineDude
MachineDude

Reputation: 35

Android ListView set row colour on create

im trying to set the background colour for individual rows in my listview, I retrieve the info for the colour from my database at runtime and i'm having a few issues trying to figure out how to do that.

When I tried to access the listview in the onCreate, so that i could change the colours I kept getting errors. Can anyone think of a solution?

Thanks in advance.

 @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.item_list);

        // Read var from Intent
        Intent intent= getIntent();
        final String ListID = intent.getStringExtra("ListID");
        golbalItemID = ListID;

        ivAdd = (ImageView) findViewById(R.id.ivAdd);
        ivCancel = (ImageView) findViewById(R.id.ivCancel);
        tvTotItems = (TextView) findViewById(R.id.tvTotItems);

        final myDBClass myDb = new myDBClass(this);
        final ArrayList<HashMap<String, String>> MebmerList = myDb.SelectAllItemData(ListID);       

        myData =  myDb.SelectItemData(Integer.parseInt(ListID.toString())); 

        // listView1
        final ListView lisView1 = (ListView)findViewById(R.id.listView1); 

        registerForContextMenu(lisView1);

        SimpleAdapter sAdap;
        sAdap = new SimpleAdapter(ListItems.this, MebmerList, R.layout.activity_column,
                new String[] {"Name", "Price", "Quan"}, new int[] {R.id.ColName, R.id.ColTel, R.id.ColQuan});      
        lisView1.setAdapter(sAdap); 

        lisView1.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView<?> myAdapter, View myView, int position, long mylng) {
                  int iChk = 0;
                // Show Data
                String arrData[] = myDb.SelectItemData((MebmerList.get(position).get("ItemID").toString()));
                if(arrData != null)
                {
                    iChk = Integer.parseInt(arrData[4]);    
                }

                if(iChk == 1)
                {
                    ischkCheck(Integer.parseInt(MebmerList.get(position).get("ItemID").toString()), 0);
                    change_color(lisView1, position, 255, 255, 255);

                    System.out.println("POSITION!ichk=1" + myAdapter.getItemAtPosition(position).toString());
                    setTitle(myAdapter.getItemAtPosition(position).toString());
                }

                else if(iChk == 0)
                {
                    ischkCheck(Integer.parseInt(MebmerList.get(position).get("ItemID").toString()), 1);
                    change_color(lisView1, position, 155, 155, 138);

                    System.out.println("POSITION!ichk=0" + myAdapter.getItemAtPosition(position).toString());
                }

              }});

        ivAdd.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent newActivity = new Intent(ListItems.this,AddItem.class);
                newActivity.putExtra("ListID", ListID);
                startActivity(newActivity);
                finish();
            }
        });

        ivCancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent newActivity = new Intent(ListItems.this,MenuScreen.class);
                startActivity(newActivity); 
                finish();
            }
        });

Upvotes: 2

Views: 383

Answers (1)

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

If most likely be simplier to use own adapter, override getView(); method (you can simply call super.getView(); and then set own background color prior returning the view.

public class MyAdapter extends SimpleAdapter {

  @Override
  View getView(int position, View convertView, ViewGroup parent) {

      View view = super.getView( position, convertView, parent );
      view.setBackgroundColor( 0xff00ff00 );

      return view;
  }
}

should make your rows green.

Upvotes: 2

Related Questions