Android的ListView项点击不工作 [英] Android listview item click is not working

查看:99
本文介绍了Android的ListView项点击不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了类似的问题,并试图解决方案,如设置可聚焦为false,但它仍然没有工作。这是我的布局,包括列表视图:

I have seen similar questions and tried the solutions such as setting focusable to false but it is still not working. Here is my layout that contains the listview:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >


<ListView 
    android:id="@+id/listView_open_groups"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/button_add_group"
    android:layout_alignParentTop="true"
    ></ListView>

<Button 
    android:id="@+id/button_add_group"
    android:text="Add Group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:focusable="false"
    />

<Button 
    android:id="@+id/button_add_entry"
    android:text="Add Entry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/button_add_group"
    android:focusable="false"
    />


</RelativeLayout>

和我custom_list_row

And my custom_list_row

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:longClickable="true" >


<ImageView 
    android:id="@+id/imgView_group_entry_icon"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:focusable="false"
    />
<TextView 
    android:id="@+id/textView_group_entry_name"
    android:layout_toRightOf="@id/imgView_group_entry_icon"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="false"/>
<TextView 
    android:id="@+id/textView_group_entry_type"
    android:layout_alignParentRight="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"/>


</RelativeLayout>

和我的自定义适配器:

public class OpenDbListAdapter extends ArrayAdapter<KeepassDBGroupv1>{

Context context;

public OpenDbListAdapter(Context context, int resource,
        List<KeepassDBGroupv1> objects) {
    super(context, resource, objects);
    this.context=context;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    KeepassDBGroupv1 rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.custom_list_row, null);
        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.textView_group_entry_name);
        holder.imageView = (ImageView) convertView.findViewById(R.id.imgView_group_entry_icon);
        holder.type = (TextView) convertView.findViewById(R.id.textView_group_entry_type);
        convertView.setTag(holder);
    } else 
        holder = (ViewHolder) convertView.getTag();

    holder.name.setText(rowItem.getGroupName());
    holder.type.setText("Group");
    Drawable d = context.getResources().getDrawable(context.getResources().getIdentifier("ic"+Integer.toString(rowItem.getImageId()), "drawable", context.getPackageName()));
    holder.imageView.setImageDrawable(d);
    //App.getDB().drawFactory.assignDrawableTo(holder.imageView, context.getResources(), rowItem.icon);

    return convertView;
}

private class ViewHolder {
    ImageView imageView;
    TextView name;
    TextView type;
}

}

最后,这是我如何设置监听器:

And finally, here is how i set the listener:

public class OpenDbActivity extends Activity{    
    static ListView lv;  

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_opendb);
    db = MyApplication.keepassdb;
    InitializeComponents();

}

    private void InitializeComponents() {

    lv = (ListView) findViewById(R.id.listView_open_groups);
    OpenDbListAdapter adapter = new OpenDbListAdapter(this, R.layout.custom_list_row, childGroups);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Log.d("Something");

        }
    });



}

任何人都可以看到的问题是什么呢?

Can anyone see what the problem is?

感谢

推荐答案

我也遇到这个问题,我是从这个问题克服点击监听器设置<$以 convertView C $ C>自定义适配器。我不知道这是很好的做法,但它解决了我的问题。

I too faced this issue, I overcome from this issue by setting click listener to convertView of custom adapter. I don't know is this good approach but it solved my issue.

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    KeepassDBGroupv1 rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.custom_list_row, null);
        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.textView_group_entry_name);
        holder.imageView = (ImageView) convertView.findViewById(R.id.imgView_group_entry_icon);
        holder.type = (TextView) convertView.findViewById(R.id.textView_group_entry_type);
        convertView.setTag(holder);
    } else 
        holder = (ViewHolder) convertView.getTag();

    holder.name.setText(rowItem.getGroupName());
    holder.type.setText("Group");
    Drawable d = context.getResources().getDrawable(context.getResources().getIdentifier("ic"+Integer.toString(rowItem.getImageId()), "drawable", context.getPackageName()));
    holder.imageView.setImageDrawable(d);
    //App.getDB().drawFactory.assignDrawableTo(holder.imageView, context.getResources(), rowItem.icon);
    convertView.setOnClickListener(new OnClickListener(){
          @Override
          public void onClick(View v) {
              Log.v("OpenDbListAdapter ","List View Clicked");
          }
    });
    return convertView;
}

这篇关于Android的ListView项点击不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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