简体   繁体   中英

Listview item background color change on condition

mydb = new Databaseop(Main15Activity.this);
    Intent i = getIntent();
    final String rname = i.getStringExtra("rname");
    t1.setText(rname);
    Databaseop dp = new Databaseop(ctx);
    mydb = new Databaseop(Main15Activity.this);

    Cursor data = mydb.getListContents(dp, rname);
    if (data.getCount() == 0) {
        Toast.makeText(Main15Activity.this, "There are no contents in this list!", Toast.LENGTH_LONG).show();
    } else {
        while (data.moveToNext()) {
            String theListcon = data.getString(0);
          final  String[] contents = convertStringToArray(theListcon);
            listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,contents){@Override
            public View getView(int position , View convertView, ViewGroup parent){
                View view=super.getView(position, convertView, parent);
                TextView textView=(TextView)view.findViewById(android.R.id.text1);
                textView.setTextColor(Color.WHITE);
                String content=getItem(position);

                if(content.equals(tagcontent))
                    {
                        textView.setBackgroundColor(Color.GREEN);
                        Intent intent=getIntent();
                        String user=intent.getStringExtra("name");
                        String rank=intent.getStringExtra("rank");
                        Databaseop dp = new Databaseop(ctx);
                        dp.reports(dp, user, rank, tagcontent);
                        Toast.makeText(Main15Activity.this, "one room visited", Toast.LENGTH_SHORT).show();

                    }


                return view;
            }
            };
            l1.setAdapter(listAdapter); 

I want to match a string with the list view items and if the item matches with the string the background of the item must turn green and my items in the list view are from database.

you have to set else condition while you set background to item list.

Like below.

if(condition to compare string){
 // set item color as green
}
else{
 // set normal color to item
}

You can do it in your adapter. When the cell is inflated, check if the string matches. If it does, fetch the view you want, and alter its background accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM