ListView and List Adapter in android change color
I have a list activity which has a list showing results of a query. Well I want to be able to click on each item and the item changes color but it doesn't work. I want the item to remain selecetd state untill "accepte" button is pressed or item is pressed again. I know that is how text boxes work but i prefer to do it my own way.
Here is my code:
public void createList() {
if (ok == 1) {
//hay muachas possibilidades
if (sol.get(i).getMultiseleccion() != 0){
bt2.setVisibility(View.INVISIBLE);
}else {
//solo se clika en una
//lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
bt2.setVisibility(View.VISIBLE);
}
String hd1 = sol.get(i).getDescSolicitud();
tv2.setText(hd1);
ArrayList<SubSolicitud> sub = sol.get(i).getSubSol();
mAdapter = new EventAdapter(this, sub);
setListAdapter(mAdapter);
lv.setTextFilterEnabled(true);
lv.computeScroll();
lv.setDividerHeight(1);
lv.setItemsCanFocus(false);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
ok = 1;
//OnListClick(position, arg1);
if (sol.get(i).getMultiseleccion() != 0) {
// multiples respuestas
((EventEntryView)arg1).text1.setTextColor(Color.YELLOW);
guardarRespuesta();
}else {
buscarElementos();
}
}
});
}
// informar el usuario de que hay un error
else
buildAlertDialog();
}
and the other classes are: public class EventAdapter extends BaseAdapter {
public ArrayList<SubSolicitud> mEvents = null;
public EventAdapter(Context c, ArrayList<SubSolicitud> subsol) {
mContext = c;
mEvents = subsol;
}
public int getCount() {
return mEvents.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
EventEntryView btv;
if (convertView == null) {
btv = new EventEntryView(mContext, mEvents.get(position));
} else {
btv = (EventEntryView) convertView;
String title1 = mEvents.get(position).getDescripcion();
if (title1 != null) {
btv.setText1Title(title1);
}
}
btv.setBackgroundColor(Color.BLACK);
return btv;
}
private Context mContext;
public void clearEvents() {
mEvents.clear();
notifyDataSetChanged();
}
public void addEvent(SubSolicitud e) {
mEvents.add(e);
}
}
public class EventEntryView extends LinearLayout {
// private View inflatedView;
private TextView text1;
// private TextView text2;
public EventEntryView(Context context, SubSolicitud subSolicitud) {
super(context);
this.setOrientation(VERTICAL);
text1=new TextView(context);
text1.setTextSize(20);
text1.setPadding(10, 10, 10, 10);
text1.setTextColor(Color.WHITE);
String t = subSolicitud.getDescripcion();开发者_StackOverflow中文版
text1.setText(t);
addView(text1, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
public void setText1Title(String title1) {
// TODO Auto-generated method stub
text1.setText(title1);
}
}
As you can see I try to get the text in yellow but it doesn't work I click and it doesn't become yellow.
Is there a solution?
thanks
It doesn't work because there is not an EventEntryView
for each item in the list - the same EventEntryView
is reused to render each.
You need to add something on your SubSolicitud
model object to indicate it's been selected (let's say a boolean "selected" property).
In your onItemClicked
handler you would toggle this property -
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
// ...
SubSolicitud selectedSubSol = (SubSolicitud)adapterView.getAdapter().getItem(id);
boolean currentValue = selectedSubSol.isSelected();
selectedSubSol.setSelected(!currentValue); // toggle 'selected' on and off
// ...
}
(You also need to fix your EventAdapter
getItem
method to return mEvents.get(position)
for this to work...)
Then in your EventAdapter
getView
method, you use the value of the "selected" property to render the text color -
public View getView(int position, View convertView, ViewGroup parent) {
// ...
if (mEvents.get(position).isSelected()) {
btv.text1.setTextColor(Color.YELLOW);
} else {
// you have to have an else to set it back to the default
// color, because the view is reused for all list items.
btv.text1.setTextColor(Color.WHITE);
}
// ...
}
This is how you change the color.
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
position = position - listView.getFirstVisibleItem();
((EditText)arg0.getChildAt(position).findViewById(R.id.myTextView)).setTextColor(Color.YELLOW);
}
But if you want to release the item from the color you should iterate through each item of the listview and change it back to normal or you can do it inside the getView()
since it is called every time there is action on the listview
精彩评论