ListView setOnItemClickListener不起作用

时间:2013-07-29 22:19:36

标签: android listview listener

就像标题所说,我的setOnItemClickListener不起作用。我查看了迄今为止我所看到的所有内容,并且无法找到我的错误。

这是代码:

这是有问题的课程。它不是主类,而是从意图调用:

package...;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;

public class GroupActivity extends Activity {

    String group_id = "";
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group);
        context = this;

        Intent intent = getIntent();
        group_id = intent.getStringExtra("group_id");
        Log.i("bla", "Launched GroupActivity for group_id " + group_id);

        final SubAdapter adapter = new SubAdapter(this, group_id);
        ListView lv = (ListView) findViewById(R.id.listView1);
        lv.setClickable(true);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> av, View v, int idx, long lidx) {
                Log.i("print here", "blaa " + idx);

            }

        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

这是SubAdapter类:

package...;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SubAdapter extends BaseAdapter {

    private static final int thumb_width = 128;
    private static final int thumb_hight = 128;
    private static Bitmap default_thumb = null;
    private static LayoutInflater inflater = null;
    private final String group_id_;
    private final Activity activity;
    private final SubAdapter t = this;

    // used to keep selected position in ListView
    private int selectedPos = -1;   // init value for not-selected

    public SubAdapter(Activity a, final String group_id) {
        Drawable d = a.getResources().getDrawable(R.drawable.ic_contact_picture);
        default_thumb = ((BitmapDrawable) d).getBitmap();
        activity = a;
        group_id_ = group_id;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int idx, View convertView, ViewGroup parent) {
        Log.i("bla2", "getting view with index " + idx);
        View vi = convertView;

        if (convertView == null) {
            vi = inflater.inflate(R.layout.sub_list_item, null);
            vi.setClickable(true);
        }
        TextView tv1 = (TextView)vi.findViewById(R.id.textView1);
        TextView tv2 = (TextView)vi.findViewById(R.id.textView2);
        ImageView img = (ImageView)vi.findViewById(R.id.imageView1);

        tv1.setText("first name" + " " + "last name");
        tv2.setText("some date");

        // put thumbnail
        Bitmap thumb = default_thumb;
        img.setImageBitmap(thumb);
        Log.i("bla3", "index is " + idx + "selected index is " + selectedPos);
        if(selectedPos == idx){
            Log.i("bla4", "inside if");
         }      
        return vi;
    }


    public void setSelectedPosition(int pos){
    selectedPos = pos;
         // inform the view of this change
         notifyDataSetChanged();
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

}

这是activity_group xml:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".GroupActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusableInTouchMode="false"
        android:focusable="false" >

    </ListView>

</RelativeLayout>

这是sub_list_item xml:

<?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:clickable="true"
    android:focusable="false" 
    android:focusableInTouchMode="false">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="6dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_margin="6dp"
        android:layout_toLeftOf="@+id/toggleButton1"
        android:layout_toRightOf="@+id/imageView1"
        android:text="some text"
        android:textAppearance="?android:attr/textAppearance" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_toLeftOf="@+id/toggleButton1"
        android:text="more text"
        android:textAppearance="?android:attr/textAppearance" />

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_margin="6dp"
        android:text="present" />

</RelativeLayout>

除了点击监听器外,其他一切似乎都有效。 SubAdapter正确打开并填充列表。 基本上我正在寻找的是获取消息“Log.i(”print here“,”blaa“+ idx);”打印 - 它是ListView的setOnItemClickListener中的日志(见上图)

如果缺少任何其他相关代码,请与我们联系 感谢!!!

3 个答案:

答案 0 :(得分:3)

在GroupActivity类中,请在findViewById方法之前添加以下代码:

现在您可以获得相同的视图并更改代码

ListView lv = (ListView) findViewById(R.id.listView1);

final View v = inflater.inflate(R.layout.rescuer_no_dialog, null);
ListView lv = (ListView)v.findViewById(R.id.listView1);

答案 1 :(得分:2)

你应该删除对的电话 您的SubAdapter类中的vi.setClickable(true);,因为您的convertView将在onItemClick侦听器之前使用单击。

答案 2 :(得分:0)

更改此

ListView lv = (ListView) findViewById(R.id.listView1);

lv = getListView()

扩展ListActivity并在方法外声明lv。