listView背景逻辑的问题

时间:2011-08-03 06:48:55

标签: java android android-listview

我正在使用数组列表,listView和自定义简单适配器,以便在Android中创建我的列表。在下面的代码中,我使用addItem()来添加Item。我需要添加项目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'是什么。如果它是某个值,那么我想将我的背景设置为一种颜色。所有添加等都在起作用,但它最终会将我添加的任何内容的背景颜色设置为蓝色,而不管我通过该特定项目的意图传递的数据。

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

提前感谢您抽出宝贵时间解决我的问题

1 个答案:

答案 0 :(得分:1)

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

但实际上我无法获得Adapter类中的painItems声明。看起来你编写了两个类代码