Reputation: 617
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.
Upvotes: 0
Views: 92
Reputation: 8392
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);
}
});
Upvotes: 0
Reputation: 106
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);
Upvotes: 1
Reputation: 1064
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
Upvotes: 0