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

时间:2011-08-02 18:12:13

标签: java android android-listview simpleadapter

在制作我自己的SimpleAdapter对象之前因为我想改变行的颜色,我只是使用了新的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() - &gt;自定义适配器类 - &gt; onActivityResult() - &gt; addItem()* **

以下是它的外观截图。每个项目中的两个TextView字段应填充信息(他们是这样做的,直到我这样做)。

3 个答案:

答案 0 :(得分:5)

如果之前只使用new SimpleAdapter(...),那么在您的getView(...)实施中将第一行更改为:

View row = super.getView(position, convertView, parent);

看看这是否是你所期待的。取出LayoutInflater的东西。

答案 1 :(得分:1)

getView()中,关于设置行背景的位置,您还应该设置TextView的文字。

调用notifyDataSetChanged(),不会自动设置您的文本,只会导致ListView使用新数据重绘可见行...实际上每行调用getView()需要刷新。

我还建议设置mylistlayout.xml文件的背景颜色,如果getView()函数开始使用findViewByID,你还应该考虑使用“视图持有者”方法比如此示例:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

答案 2 :(得分:1)

您需要在getView()中设置文字。像这样:

public View getView(int position, View convertView, ViewGroup parent){

    TextView text;

    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.mylistlayout, parent, false);
        text = (TextView) convertView.findViewById(R.id.more_list_text);
    }
    convertView.setBackgroundColor(0xFF0000FF);
    text.setText(map.get(position));
    return convertView;
}

此外,这非常重要 - 将您的地图存储为SimpleAdapter的成员变量 即,将此行放在对象定义的顶部:

HashMap<String, String> map = new HashMap<String, String>();