在访问列表视图自定义对象 [英] accessing custom objects in a listview

查看:126
本文介绍了在访问列表视图自定义对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在建设一个具有请求被发送到Web应用程序,结果再被解析并投入对象的ArrayList

这个名单,然后,填充一个ListView。
我想创建一个 onClickListener ,让我知道单击了哪个对象,但我无法找到实现这个正确的方法。

无论是分配的onClick在原来的活动ListView控件,或者指定自定义适配器 getView 函数中的监听器。

在我看来,像在 getView 有太多的开销分配监听器。

它是如何工作的?什么是更好的?

code为主要活动:

 公共类NoPicList延伸活动{
    ListView控件列表;
    NoPicAdapter适配器;
    ProgressDialog支持mDialog;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.no_pic_list);
        名单=(ListView控件)findViewById(R.id.noPicListView);        束B = getIntent()getExtras()。
        串请求= b.getString(请求);
        支持mDialog =新ProgressDialog(本);
        mDialog.setCancelable(假);
        mDialog.setMessage(Lodaing数据);
        mDialog.show();        新GetNewsAndCalendar()执行(要求);
    }    @覆盖
    保护无效的onPause(){
        mDialog.dismiss();
        super.onPause();
    }    类GetNewsAndCalendar扩展
        AsyncTask的<弦乐,无效的ArrayList<消息>> {        @覆盖
        保护的ArrayList<消息> doInBackground(字符串... PARAMS){
            字符串URL =参数[0];
            DOMFeedParser分析器=新DOMFeedParser(URL);
            返回parser.parse();
        }        @覆盖
        保护无效onPostExecute(ArrayList的<消息>的结果){
            适配器=新NoPicAdapter(NoPicList.this,结果);
            list.setAdapter(适配器);
            //首先选项插入onClickListener
            mDialog.dismiss();
        }
    } // GetNewsAndCalendar结束
}

code代表名单适配器:

 公共类NoPicAdapter延伸BaseAdapter {    私人的ArrayList<消息>数据;
    私人活动mActivity;
    私人LayoutInflater吹气= NULL;    公共NoPicAdapter(活动一,ArrayList的<消息>的结果){
        mActivity =一;
        数据=结果;
        吹气=(LayoutInflater)mActivity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }    @覆盖
    公众诠释的getCount(){
        返回data.size();
    }    @覆盖
    公共对象的getItem(INT位置){
        返回的位置;
    }    @覆盖
    众长getItemId(INT位置){
        返回的位置;
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        查看VI = convertView;
        如果(convertView == NULL)
            VI = LayoutInflater.from(mActivity).inflate(R.layout.no_pic_list_item,
                空值);        TextView的标题=(TextView中)vi.findViewById(R.id.noPicTitle);
        TextView的字幕=(TextView中)vi.findViewById(R.id.noPicSubtitle);        title.setText(data.get(位置).getTitle());
        subtitle.setText(data.get(位置).getDate());
    //第二位插入onClickListener
        返回VI;
    }
}


解决方案

按我的关心,分配在的onClick在原有活动的ListView 是不错的主意,而不是分配给它在 getView()。因为在 getView()您的适配器,它总是创建一个新View.onClick()的对象 ..

  list.setOnItemClickListener(新OnItemClickListener()
 {
  @覆盖
  公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3)
  {
   adapter.get(位置);
   //从适配器获取的数据,线以上code给选中的列表项的当前位置的自定义适配器的对象
  }
 });

I'm currently building an app that has a request being sent into the web, the result then gets parsed and put into an ArrayList of objects.

this list then, populates a ListView. I want to create an onClickListener that will allow me to know which object was clicked, but I can't find the correct way to implement this.

either assign the onClick to the ListView in the original activity, or, assign the listener within the getView function in the custom adapter.

it seems to me like assigning the listeners in the getView has too much overhead.

how does it work? what is better?

code for main activity:

public class NoPicList extends Activity {
    ListView list;
    NoPicAdapter adapter;
    ProgressDialog mDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.no_pic_list);
        list = (ListView) findViewById(R.id.noPicListView);

        Bundle b = getIntent().getExtras();
        String request = b.getString("REQUEST");
        mDialog = new ProgressDialog(this);
        mDialog.setCancelable(false);
        mDialog.setMessage("Lodaing Data");
        mDialog.show();

        new GetNewsAndCalendar().execute(request);
    }

    @Override
    protected void onPause() {
        mDialog.dismiss();
        super.onPause();
    }

    class GetNewsAndCalendar extends
        AsyncTask<String, Void, ArrayList<Message>> {

        @Override
        protected ArrayList<Message> doInBackground(String... params) {
            String url = params[0];
            DOMFeedParser parser = new DOMFeedParser(url);
            return parser.parse();
        }

        @Override
        protected void onPostExecute(ArrayList<Message> result) {
            adapter = new NoPicAdapter(NoPicList.this, result);
            list.setAdapter(adapter);
            //FIRST OPTION TO INSERT onClickListener
            mDialog.dismiss();
        }
    }    //end of GetNewsAndCalendar
}

code for list adapter:

public class NoPicAdapter extends BaseAdapter {

    private ArrayList<Message> data;
    private Activity mActivity;
    private LayoutInflater inflater = null;

    public NoPicAdapter(Activity a, ArrayList<Message> result) {
        mActivity = a;
        data = result;
        inflater = (LayoutInflater) mActivity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)
            vi = LayoutInflater.from(mActivity).inflate(R.layout.no_pic_list_item,
                null);

        TextView title = (TextView) vi.findViewById(R.id.noPicTitle);
        TextView subtitle = (TextView) vi.findViewById(R.id.noPicSubtitle);

        title.setText(data.get(position).getTitle());
        subtitle.setText(data.get(position).getDate());
    // SECOND PLACE TO INSERT THE onClickListener
        return vi;
    }
}

解决方案

As per my concern, assign the onClick to the ListView in the original activity is good idea, rather then assigning it to in getView() of your custom adapter. Because in getView() of your adapter it always create a new View.onClick()'s object..

list.setOnItemClickListener(new OnItemClickListener()
 {
  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
  {
   adapter.get(position);  
   // Get data from your adapter,   the above code of line give the custom adapter's object of   current position of selected list item     
  }   
 });  

这篇关于在访问列表视图自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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