Android的ListView控件onClickListener自定义适配器 [英] Android ListView onClickListener Custom Adapter

查看:458
本文介绍了Android的ListView控件onClickListener自定义适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也读到了自定义适配器,以及如何对其进行索引帖,但似乎我不能让我的工作。我覆盖getView和我的XML包含TextView的1和2按钮。我做了,通过的onClickListener检测两个按钮但是我不能区分其中的ListView元素是谁引发了ClickEvent之一。我想我不同的方法,但我总是在onclick方法一个NullPointerException异常。

  @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    ViewHolder持有人;
    如果(convertView == NULL){
        LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.listexample,NULL);
        持有人=新ViewHolder();
        holder.textView =(TextView中)convertView.findViewById(R.id.commandLine_text);
        holder.start =(按钮)convertView.findViewById(R.id.test_start_button);
        holder.stop =(按钮)convertView.findViewById(R.id.test_stop_button);
        convertView.setTag(保持器);
        convertView.findViewById(R.id.commandLine_text);
        convertView.findViewById(R.id.test_start_button);
        convertView.findViewById(R.id.test_stop_button);    }其他{
        支架=(ViewHolder)convertView.getTag();
    }
    holder.textView.setText(this.getItem(位置));
    holder.start.setOnClickListener(本);
    holder.stop.setOnClickListener(本);
    返回convertView;}
@覆盖
公共无效的onClick(视图v){
 //在这里,我想知道这两个按钮(启动,停止)被点击,什么位置
    INT位置=(整数)v.getTag();
    Log.d(onclick事件,位置:+位置);}
静态类ViewHolder {
    TextView中的TextView;
    按钮启动;
    按钮停止;
}


解决方案

尝试使用<一个href=\"https://developer.android.com/reference/android/widget/AdapterView.html#getPositionForView%28android.view.View%29\"相对=nofollow> getPositionForView(V) 找到关联到这是pressed按钮的位置。

I read posts about custom adapters and how to index them but it seems i cannot make mine work. I overwrite the getView and my XML contains 1 TextView and 2 Buttons. I made it that both buttons were detected by the onClickListener however i couldnt differentiate which ListView element was the one who triggered the ClickEvent. I tried i different approach but i always get a NullPointerException in the onClick Method.

    @Override
public View getView(int position, View convertView, ViewGroup parent){
    ViewHolder holder;      
    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater)  context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        convertView = inflater.inflate(R.layout.listexample, null);
        holder = new ViewHolder();
        holder.textView = (TextView) convertView.findViewById(R.id.commandLine_text);
        holder.start = (Button) convertView.findViewById(R.id.test_start_button);
        holder.stop = (Button) convertView.findViewById(R.id.test_stop_button);
        convertView.setTag(holder);
        convertView.findViewById(R.id.commandLine_text);
        convertView.findViewById(R.id.test_start_button);
        convertView.findViewById(R.id.test_stop_button);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.textView.setText(this.getItem(position));
    holder.start.setOnClickListener(this);
    holder.stop.setOnClickListener(this);
    return convertView;

}
@Override
public void onClick(View v) {
 //Here i want to know which button of the two (start,stop) was clicked and what position
    int position =(Integer)v.getTag();
    Log.d("OnClick","Position: "+position);

}
static class ViewHolder {
    TextView textView;
    Button start;
    Button stop;
}

解决方案

Try using getPositionForView(v) to find the position that correlates to the button that was pressed.

这篇关于Android的ListView控件onClickListener自定义适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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