自定义 SimpleAdapter 仅显示示例文本 Android [英] Custom SimpleAdapter only shows Sample Text Android

查看:21
本文介绍了自定义 SimpleAdapter 仅显示示例文本 Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建我自己的 SimpleAdapter 对象之前,因为我想改变行的颜色,我只是使用了 new SimpleAdapter(...).现在我正在使用我自己的自定义 SimpleAdapter,行颜色正在改变,但我的文本没有得到更新.我已经调用了adapter.notifyDataSetChanged(),但它仍然只显示示例文本-TextView".正如我所说,当我没有创建自己的适配器时,一切正常.我怀疑这可能与我初始化事物的顺序有关:

Before making my own SimpleAdapter object because I wanted to change the color of the rows, I was just using new SimpleAdapter(...). Now that I am using my own custom SimpleAdapter, the row color is changing, but my text is not getting updated. I have called adapter.notifyDataSetChanged(), but it is still showing only the sample text- "TextView". As I said, everything was working fine when I didn't create my own adapter. I suspect it might have something to do with the order I am initializing things:

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 代码之前的意图(onActivityResult())获取数据的方式:

This is how I am getting the data from the intent(onActivityResult()), placed before the addItem Code:

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

*Also, just in case this is relevant the order of placement is this: onCreate() -> custom Adapter class -> onActivityResult() -> addItem()* **

这是它的外观截图.每个项目中的两个 TextView 字段应填充信息(在我这样做之前,它们是).

Here is a screenshot of what it looks like. The two TextView fields in each item should be filled with info(which they were, until I did this).

推荐答案

如果它以前只使用 new SimpleAdapter(...) 然后在你的 getView(...) 实现将第一行更改为:

If it worked previously with just using new SimpleAdapter(...) then in your getView(...) implementation change the first line to this:

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

看看这是否是您的期望.也取出 LayoutInflater 的东西.

And see if that is what you're expecting. Take out the LayoutInflater stuff too.

这篇关于自定义 SimpleAdapter 仅显示示例文本 Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆