Problem with listView background logic
I am using an array list, a listView, and a custom simple adapter in order to create my list in Android. In the following code, I use addItem() to add an Item. I need to add the item onActivityResult, using some of the extras I get in the intent. This all works fine, but the problem comes when I am trying to set the background color based on those extras that I am getting. I think it has something to do with the adapter not being refreshed, etc. My code will clear up what I am trying to say.
SimpleAdapter adapter;
List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();
ListView listthings;
int[] to;
String[] from;
**OnCreate()**{
from = new String[] { "row_1", "row_2" };
to = new int[] { R.id.row1, R.id.row2 };
adapter = new Adapter(this, painItems, R.layout.mylistlayout, from, to);
listthings.setAdapter(adapter);
}
Custom Adapter code:
public class Adapter extends SimpleAdapter {
HashMap<String, String> map = new HashMap<String, String>();
public Adapter(Context context,
List<? extends Map<String, String>> data, int resource,
String[] from, int[] to) {
super(context, data, resource, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
row = super.getView(position, convertView, parent);
if (row == null) {
LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = mInflater.inflate(R.l开发者_如何学编程ayout.mylistlayout, parent, false);
}
if (Integer.valueOf(painItems.get(position).get("row_3")) != null) {
if (Integer.valueOf(painItems.get(position).get("row_3")) == 2) {
row.setBackgroundColor(0xFF0000FF);//**BACKGROUND COLOR SET HERE**
//Toast.makeText(getApplicationContext(), "this one", Toast.LENGTH_SHORT).show();
}
}
return row;
}
}
How I am retrieving the necessary extras and adding the item:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == 1) {
row1 = data.getStringExtra("com.painLogger.row1");
row2 = data.getStringExtra("com.painLogger.row2");
painLevelString = data.getStringExtra("com.painLogger.painLevel");
painLocation = data.getStringExtra("painLocation");
timeOfPainString = data.getStringExtra("com.painLogger.painTime");
textTreatmentString = data
.getStringExtra("com.painLogger.treatment");
addItem();
}
}
// to add the item, put it in the map, and add the map into the list
private void addItem() {
HashMap<String, String> map = new HashMap<String, String>();
map.put("row_1", row1);
map.put("row_2", row2);
map.put("row_3", painLevelString);
map.put("row_4", painLocation);
map.put("row_5", timeOfPainString);
map.put("row_6", textTreatmentString);
painItems.add(map);
adapter.notifyDataSetChanged();
}
Again, when I add the item to my list, I want to see what the 'painlevelString' is. If it is a certain value, then I would like to set my background to a color. All the adding, etc is working, but it just ends up setting the background color to blue of whatever I add, regardless of the data I pass through the intent for that certain item.
Although I believe I have added all the relevant code, my full activity can be found here: http://pastebin.com/4BWQYpnj.
Thank You in advance for taking your time to solve my problem
You should set color both if your condition is true (one color) or not (another color).
But actually I can't get where is painItems declaration in Adapter class. It seems that you meshed up two classes code
精彩评论