gpt4 book ai didi

java - 自定义 SimpleAdapter 仅显示示例文本 Android

转载 作者:太空宇宙 更新时间:2023-11-03 12:24:06 27 4
gpt4 key购买 nike

在创建我自己的 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();




}

编辑:添加代码

这就是我从 intent(onActivityResult()) 获取数据的方式,放在 addItem 代码之前:

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com