如何设置OnitemClick自定义列表视图 [英] How to set OnitemClick in custom listview

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

问题描述

我使用的扩展底座适配器列表视图适配器创建一个列表视图。

它工作正常。但我需要onitemClick侦听器添加到列表视图,当我点击列表项它去到另一个活动,与listitem.how一起,我们可以做到这一点?

这是我的code在ListView适配器

 公共类listviewAdapter延伸BaseAdapter {
    字符串FIRST_COLUMN = Constant.FIRST_COLUMN;
    字符串是second_column = Constant.SECOND_COLUMN;
    字符串THIRD_COLUMN = Constant.THIRD_COLUMN;
    字符串FOURTH_COLUMN = Constant.FOURTH_COLUMN;
    公众的ArrayList<&HashMap的LT;字符串,字符串>>清单;
    活动活动;    公共listviewAdapter(活动活动,ArrayList的<&HashMap的LT;字符串,字符串>>名单){
    超();
    this.activity =活动;
    this.list =清单;
    }    @覆盖
    公众诠释的getCount(){
    // TODO自动生成方法存根
    返回则为list.size();
    }    @覆盖
    公共对象的getItem(INT位置){
    // TODO自动生成方法存根
    返回list.get(位置);
    }    @覆盖
    众长getItemId(INT位置){
    // TODO自动生成方法存根
    返回0;
    }    私有类ViewHolder {
       TextView的txtFirst;
       TextView的txtSecond;
       TextView的txtThird;
       TextView的txtFourth;
    }
    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    // TODO自动生成方法存根
    查看VI = convertView;
    // TODO自动生成方法存根
            ViewHolder持有人;
            LayoutInflater吹气= activity.getLayoutInflater();            如果(convertView == NULL)
            {
                convertView = inflater.inflate(R.layout.requestcard_columnsdetails,NULL);
                持有人=新ViewHolder();
                holder.txtFirst =(TextView中)convertView.findViewById(R.id.date);
                holder.txtSecond =(TextView中)convertView.findViewById(R.id.from);
                holder.txtThird =(TextView中)convertView.findViewById(R.id.to);
                holder.txtFourth =(TextView中)convertView.findViewById(R.id.time);
                convertView.setTag(保持器);
            }
            其他
            {
                支架=(ViewHolder)convertView.getTag();
            }            HashMap的<字符串,字符串>地图= list.get(位置);
            holder.txtFirst.setText(map.get(FIRST_COLUMN));
            holder.txtSecond.setText(map.get(是second_column));
            holder.txtThird.setText(map.get(THIRD_COLUMN));
            holder.txtFourth.setText(map.get(FOURTH_COLUMN));        返回convertView;
    }
}

和我居住使用的主要活动列表项

  INT A,B;
名单=新的ArrayList<&HashMap的LT;字符串,字符串>>();
用于:(a = 0;一个与所述; outputString.length;一个++){
    HashMap的<字符串,字符串> temp1中=新的HashMap<字符串,字符串>();
    temp1.put(FIRST_COLUMN,outputString [一个] [0]);
    temp1.put(是second_column,outputString [一个] [1]);
    temp1.put(THIRD_COLUMN,outputString [一个] [4]);
    temp1.put(FOURTH_COLUMN,outputString [一个] [5]);
    list.add(temp1目录);
}
listviewAdapter适配器=新listviewAdapter(这一点,清单);
lview.setAdapter(适配器);


解决方案

  

随着列表项一起


如果你想获得从列表项的一些数据,只需要使用 parent.getItemAtPosition(位置)这是已经提及的监听器里。这将返回相应的对象,你可以执行与进一步的行动。

 的HashMap<字符串,字符串>地图=(HashMap的<字符串,字符串>)parent.getItemAtPosition(位置);
  串的一个= map.get(钥匙);
  String中的两个= map.get(KEY2);  接下来意向=新意图(FirstActivity.this,NextActivity.class);
  next.putExtra(一,一个);
  next.putExtra(二,二);
  startActivity(下);

得到它在你的第二个活动:

 捆绑额外= getIntent()getExtras()。
 串的一个= extras.getStringExtra(1);
 String中的两个= extras.getStringExtra(二);

I created a listview using listview adapter that extends base adapter.

It works fine. but I need to add onitemClick Listener to listview, when I click a list item it go to another activity ,along with the listitem.how can we do this ?

This is my code in the listview adapter

public class listviewAdapter extends BaseAdapter{
    String FIRST_COLUMN = Constant.FIRST_COLUMN;
    String SECOND_COLUMN = Constant.SECOND_COLUMN;
    String THIRD_COLUMN = Constant.THIRD_COLUMN;
    String FOURTH_COLUMN = Constant.FOURTH_COLUMN;
    public ArrayList<HashMap<String,String>> list;
    Activity activity;

    public listviewAdapter(Activity activity, ArrayList<HashMap<String,String>> list) {
    super();
    this.activity = activity;
    this.list = list;
    }

    @Override
    public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
    }

    @Override
    public Object getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
    }

    @Override
    public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
    }

    private class ViewHolder {
       TextView txtFirst;
       TextView txtSecond;
       TextView txtThird;
       TextView txtFourth;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View vi=convertView;
    // TODO Auto-generated method stub
            ViewHolder holder;
            LayoutInflater inflater =  activity.getLayoutInflater();

            if (convertView == null)
            {
                convertView = inflater.inflate(R.layout.requestcard_columnsdetails, null);
                holder = new ViewHolder();




                holder.txtFirst = (TextView) convertView.findViewById(R.id.date);
                holder.txtSecond = (TextView) convertView.findViewById(R.id.from);
                holder.txtThird = (TextView) convertView.findViewById(R.id.to);
                holder.txtFourth = (TextView) convertView.findViewById(R.id.time);
                convertView.setTag(holder);
            }
            else
            {
                holder = (ViewHolder) convertView.getTag();
            }

            HashMap<String, String> map = list.get(position);
            holder.txtFirst.setText(map.get(FIRST_COLUMN));
            holder.txtSecond.setText(map.get(SECOND_COLUMN));
            holder.txtThird.setText(map.get(THIRD_COLUMN));
            holder.txtFourth.setText(map.get(FOURTH_COLUMN));



        return convertView;
    }
}

And I populated list items in the main activity using

int a,b;
list = new ArrayList<HashMap<String,String>>();
for ( a=0; a<outputString.length;a++){
    HashMap<String,String> temp1 = new  HashMap<String,String>();
    temp1.put( FIRST_COLUMN,outputString[a][0]);
    temp1.put(SECOND_COLUMN, outputString[a][1]);
    temp1.put(THIRD_COLUMN, outputString[a][4]);
    temp1.put(FOURTH_COLUMN, outputString[a][5]);
    list.add(temp1);
}
listviewAdapter adapter = new listviewAdapter(this, list);
lview.setAdapter(adapter);

解决方案

along with the listitem

if you want to get some data from the ListItem, just use parent.getItemAtPosition(position) inside the listener which was already mentioned. This will return a corresponding Object that you can perform further actions with

   HashMap<String, String> map = (HashMap<String, String>) parent.getItemAtPosition(position);
  String one = map.get("key");
  String two = map.get("key2");

  Intent next = new Intent(FirstActivity.this, NextActivity.class);
  next.putExtra("One", one);
  next.putExtra("Two", two);
  startActivity(next);

Get it in your second Activity:

 Bundle extras = getIntent().getExtras();
 String one = extras.getStringExtra("One");
 String two = extras.getStringExtra("Two");

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

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