gpt4 book ai didi

java - listView后台逻辑有问题

转载 作者:行者123 更新时间:2023-12-01 15:46:45 25 4
gpt4 key购买 nike

我使用数组列表、listView 和自定义简单适配器来在 Android 中创建列表。在下面的代码中,我使用 addItem() 添加一个项目。我需要使用我在 Intent 中获得的一些额外功能来添加 onActivityResult 项目。这一切工作正常,但是当我尝试根据我获得的那些额外内容设置背景颜色时,问题就出现了。我认为这与适配器未刷新等有关。我的代码会澄清我想说的内容。

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);

}

自定义适配器代码:

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.layout.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;

}

}

我如何检索必要的附加内容并添加项目:

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();

}

同样,当我将项目添加到列表中时,我想看看“painlevelString”是什么。如果它是某个值,那么我想将背景设置为一种颜色。所有添加等都在工作,但它最终只是将我添加的任何内容的背景颜色设置为蓝色,无论我通过该特定项目的 Intent 传递的数据如何。

虽然我相信我已经添加了所有相关代码,但我的完整 Activity 可以在这里找到:http://pastebin.com/4BWQYpnj .

预先感谢您花时间解决我的问题

最佳答案

如果您的条件为真(一种颜色)或不为真(另一种颜色),您应该设置颜色。

但实际上我无法获取 Adapter 类中 painItems 声明的位置。看来您将两个类代码融合在一起

关于java - listView后台逻辑有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6922651/

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