Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

android - Coloring specific item is ListView

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...