繁体   English   中英

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

[英]Custom SimpleAdapter only shows Sample Text Android

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




}

编辑:添加代码

这就是我从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() - > custom Adapter class - > onActivityResult() - > addItem()* **

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

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

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

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

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

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

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

您需要在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>();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM