Answer the question
In order to leave comments, you need to log in
How to set the color of a ListView row based on a certain condition?
Good evening comrades. I fight with the specified problem. Database with 5 columns. There may be no data in the last column. It is necessary that in the ListView those rows in which the last column is empty are highlighted in a different color. At the moment, the output in the ListView is done through SimpleCursorAdapter():
from = new String[]{DB.COLUMN_NUMBER, DB.COLUMN_START_TIME, DB.COLUMN_FINISH_TIME, DB.COLUMN_SU_TIME};
int[] to = new int[]{R.id.Number_tv, R.id.StartTime_tv, R.id.FinishTime_tv, R.id.SuTime_tv};
scAdapter = new SimpleCursorAdapter(this.getActivity(), R.layout.protocol_list_item, null, from, to, 0);
Protokol_lv.setAdapter(scAdapter);
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class Ladapter extends BaseAdapter {
Cursor c;
Context context;
public Ladapter(Context context, Cursor cursor) {
c = cursor;
this.context = context;
}
@Override
public int getCount() {
return c.getCount();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View someView, ViewGroup viewGroup) {
LayoutInflater inflater = LayoutInflater.from(context);
if (someView == null) {
someView = inflater.inflate(R.layout.protocol_list_item, viewGroup, false);
}
TextView Number_tv = (TextView) someView.findViewById(R.id.Number_tv);
TextView StartTime_tv = (TextView) someView.findViewById(R.id.StartTime_tv);
TextView FinishTime_tv = (TextView) someView.findViewById(R.id.FinishTime_tv);
TextView SuTime_tv = (TextView) someView.findViewById(R.id.SuTime_tv);
return someView;
}
}
Answer the question
In order to leave comments, you need to log in
I solved my problem as follows: I created a class, copied all the text from SimpleCursorAdapter into it, and slightly modified GetView, now it looks like this:
public void bindView(View view, Context context, Cursor cursor) {
final ViewBinder binder = mViewBinder;
final int count = mTo.length;
final int[] from = mFrom;
final int[] to = mTo;
for (int i = 0; i < count; i++) {
final View v = view.findViewById(to[i]);
if (v != null) {
boolean bound = false;
if (binder != null) {
bound = binder.setViewValue(v, cursor, from[i]);
}
if (!bound) {
String text = cursor.getString(from[i]);
view.setBackgroundResource(R.drawable.list_green);
if (text == null || text.equals("")) {
text = "";
view.setBackgroundResource(R.drawable.tripbutton_red);
}
if (v instanceof TextView) {
setViewText((TextView) v, text);
} else if (v instanceof ImageView) {
setViewImage((ImageView) v, text);
} else {
throw new IllegalStateException(v.getClass().getName() + " is not a " +
" view that can be bounds by this SimpleCursorAdapter");
}
}
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question