在创建我自己的SimpleAdapter对象之前,因为我想更改行的颜色,所以我只是使用new SimpleAdapter(...)。现在,我正在使用自己的自定义SimpleAdapter,行颜色正在更改,但是我的文本没有得到更新。我已经调用了adapter.notifyDataSetChanged(),但它仍只显示示例文本“TextView”。如我所说,当我没有创建自己的适配器时,一切工作正常。我怀疑这可能与我初始化事情的顺序有关:
public class AddScreen extends Activity implements OnClickListener,
OnItemClickListener, OnItemLongClickListener {
SimpleAdapter adapter;
List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();
ListView listthings;
int[] to;
String[] from;
public void onCreate(Bundle savedInstanceState) {
...
listthings = (ListView) findViewById(R.id.listthings);
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);
...
}
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;
if (row == null) {
LayoutInflater mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = mInflater.inflate(R.layout.mylistlayout, parent, false);
}
row.setBackgroundColor(0xFF0000FF);
TextView rw1 = (TextView)findViewById(R.id.row1);
// TextView rw2 = (TextView)findViewById(R.id.row2);
rw1.setText(map.get(position));
return row;
}
}
// 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();
}
编辑:添加了代码
这就是我从位于addItem代码之前的intent(onActivityResult())获取数据的方式:
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();
}
}
*此外,以防万一,这与放置的顺序有关:onCreate()->自定义适配器类-> onActivityResult()-> addItem()* **
这是其外观的屏幕截图。每个项目中的两个TextView字段应填充信息(直到我这样做为止)。
最佳答案
如果以前只能使用new SimpleAdapter(...)
起作用,则在getView(...)
实现中,将第一行更改为:
View row = super.getView(position, convertView, parent);
看看这是否是您所期望的。也取出
LayoutInflater
的东西。关于java - 自定义SimpleAdapter仅显示示例文本Android,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6916584/