0

So, for example I have some ListView lv.

In each item in lv I got TextView tv.

Now I want to search each tv in lv and if I find tv with text I wanted to find, then I want to color this specific item.

I tried that: android - listview get item view by position and then color an item, but it crashed. Sorry, I deleted the code (was trying several methods).

Any idea how to do it? Thx for help ;)

Edit: I know I can use custom adapter etc. But I want to avoid creating new adapters due to optimization.

List View: 1, 2, 3

User clicks button to find 2

TextView with 2 has to change background to red

I wanted to create some loop to go through variables, if found change color of an item and break the loop.

Community
  • 1
  • 1
Janusz Hain
  • 457
  • 4
  • 14

3 Answers3

1

You can create a custom adapter them check if the TextViw has the text you want.

public class ProgramasListAdapter extends ArrayAdapter<YourModel> {

    public CustomAdapter(Context context,List<YourModel> objects) {
        super(context, 0, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        YourModel object = getItem(position);

        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_list, null);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView.findViewById(R.id.textview);
            holder.layout = (LinearLayout) convertView.findViewById(R.id.layout);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textView.setText(object.getText());
        if (object.getText().equals("your_text"))
            holder.layout.setBackground(...);

        return convertView;
    }

    static class ViewHolder {
        TextView textView;
        LinearLayout layout;
    }
}

Then set the adapter to your ListView:

mAdapter = new CustomAdapter(context, myModelArray);
listView.setAdapter(mAdapter);
  • thank you, +1 for some code that I wanted to create too. I am thinking about optimization. I want to avoid creating new adapters etc. Just using some loops check for value and color that one item, if possible. No idea how to do that tho and it seems I will have to create custom adapter. – Janusz Hain Jun 15 '15 at 20:27
  • Sorry, i dont how to do that without creating a custom adapter, in fact i think is more optimizated using custom adapter, because you are setting the color when LisView cell is created. – Eric Castelo Branco Mesquita Jun 15 '15 at 20:36
  • True, but list view can be created earlier and user can be asking for already created item. Then it can be optimised without creating new adapter. I think I will do new adapter, maybe someday I will find some optimisation for it. Thx a lot of help – Janusz Hain Jun 15 '15 at 20:41
0

You need to set items with type and then based on that color.

Override two fallowing methods in your adapter.

@Override
public int getItemViewType(int position) {
// Define a way to determine which layout to use, here it's just evens and   odds.
return position % 2;
 }

  @Override
public int getViewTypeCount() {
return 2; // Count of different layouts
}

then in getview method use getItemViewType() and set color

Adam Fręśko
  • 1,034
  • 9
  • 14
  • Thx for try, but I want to do it without using adapter. User want to find some text in TextView which is an item in ListView. This item has to be red if found. I want to do that without using adapter. I know I could just create custom adapter for that, rewrite data and change color, but I want to do some optimization. – Janusz Hain Jun 15 '15 at 20:02
  • There is no point in such optimization, i wouldn't even call that optimization. Even if you don't use custom adapter one is always created. – Adam Fręśko Jun 15 '15 at 20:05
  • Yes, but look, I have to recreate all listview again to change color of 1 item. I can have a lot of items there, so yea, think it is optimization. I think it is better to just find text using some loop, change color of 1 item and break the loop – Janusz Hain Jun 15 '15 at 20:07
  • And what will you do when users scrolls listview? Logic for handling that needs to be there anyway – Adam Fręśko Jun 15 '15 at 20:13
  • Yea, but doesn't creating new ListView more than just handling items? – Janusz Hain Jun 15 '15 at 20:23
0

You need to use an adapter to feed into the list view. It can be an array, or a MatrixCursor created from a sqlite call. Once you have that, and assuming your item list xml file has a LinearLayout with a TextView in it:

    View parentThis = (LinearLayout)inflater.inflate(R.layout.your_layout, container, false);
    ListView listView = (ListView)parentThis.findViewById(R.id.your_list_view);
    listView.setAdapter(getListViewAdapter()); //set up your array or matrix cursor
    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            LinearLayout layout = (LinearLayout) view;
            TextView textView = (TextView) layout.findViewById(R.id.YourTextView);

        }
    });
Kristy Welsh
  • 6,477
  • 9
  • 53
  • 87
  • thx for answer. I am looking for coloring specific item. On item click is useless, sadly. For example I got such ListView: 1 2 3 I want to find 2 and then color it red. – Janusz Hain Jun 15 '15 at 20:21