通过元数据从一个活动到下一个在Android的ListView [英] passing metadata from one activity to the next in an android listview

查看:112
本文介绍了通过元数据从一个活动到下一个在Android的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,有用于从一个活动到下一个使用数据传递的一套方法,但如何将其从列表视图在活动范围内的模型?

例如:我有,我用它来填充列表视图的JSON对象。每个列表项都有一个id。我想ID传递给上点击下一个活动。我怎么知道ListView的项目ID?我不知道怎么任意元数据附加到ListView项,并立即抓住它,并一起传递。

 公共类StuffListActivity扩展ListActivity
{
    私人所有MyApplication应用= NULL;    / **
     *当第一次创建活动调用。
     *
     * @参数savedInstanceState保存实例状态
     * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        应用=((应用程序)getApplication());
        请求REQ =新请求();
        Application.log(listType.toString());
        ArrayList的<串GT;清单= req.getStuff(application.getId(),listType);        适配器=新ArrayAdapter<串GT;(这一点,
                R.layout.list,R.id.stuff,清单);
        setListAdapter(适配器);
    }    保护无效onListItemClick(ListView中升,视图V,INT位置,长的ID)
    {
        字符串项=(字符串)getListAdapter()的getItem(位置)。
        Toast.makeText(这一点,项目+选中,Toast.LENGTH_LONG).show();
    }
}


解决方案

使用 setTag() getTag()属性中的适配器getView列表视图()code ..存储和检索数据。

I understand that there's a set of methods used to pass data from one activity to the next using, but how do I model it from within the activity with the list view?

Example: I have a json object which i use to populate a listview. Each list item has an id. I want to pass the id to the next activity on click. How do I know that listview's item id? I wasn't sure how to attach arbitrary metadata to a listview item and immediately grab it and pass it along.

public class StuffListActivity extends ListActivity
{
    private MyApplication application = null;

    /**
     * Called when the activity is first created.
     *
     * @param savedInstanceState the saved instance state
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        application = ((Application) getApplication());
        Requestor req = new Requestor();
        Application.log(listType.toString());
        ArrayList<String> list = req.getStuff(application.getId(), listType);

        adapter = new ArrayAdapter<String>(this,
                R.layout.list, R.id.stuff, list);
        setListAdapter(adapter);    
    }

    protected void onListItemClick(ListView l, View v, int position, long id) 
    {
        String item = (String) getListAdapter().getItem(position);
        Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
    }
}

解决方案

Use setTag() and getTag() property for list view in Adapter getView() code.. to store and retrieve the data.

这篇关于通过元数据从一个活动到下一个在Android的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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