ListActivity onListItemClick不起作用

时间:2012-05-22 20:27:35

标签: android android-listview adapter listactivity

在使用ListActivity的应用程序中,单击onListItemClick项不会触发覆盖的ListView回调。

我已经使用followin xml Files,permissionview.xml

定义了一个自定义列表视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_Apply"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_apply"
            android:onClick="onApplyClick" />

        <Button
            android:id="@+id/btn_install"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_install"
            android:onClick="onInstallClick" />

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancel" />

    </LinearLayout>

    <ListView android:id="@android:id/list" 
            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false">

    </ListView>  
</LinearLayout>

和viewitem布局checkable.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="wrap_content" >


   <ImageView android:id="@+id/img" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" android:layout_alignParentBottom="true"
        android:src="@drawable/ic_launcher" android:focusable="false" />

    <CheckBox
        android:id="@+id/check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:layout_alignParentRight="true"
        android:layout_marginLeft="4px"
    android:layout_marginRight="10px"

    android:clickable="true">

 </CheckBox>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/img"
        android:gravity="bottom"
        android:text="@+id/label"
        android:textSize="15px"/>
</RelativeLayout>

定义的ArrayAdapter定义如下:

     public class InteractiveArrayAdapter extends ArrayAdapter<Model> {


    private final List<Model> list;
    private final Activity context;
    private boolean changeable;

    public InteractiveArrayAdapter(Activity context, List<Model> list, boolean changeable) {
        super(context, R.layout.checkable, list);
        this.context = context;
        this.list = list;
        this.changeable = changeable;
    }

    static class ViewHolder {
        protected TextView text;
        protected CheckBox checkbox;
        protected ImageView image;
        protected int pos;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
                view = inflater.inflate(R.layout.checkable, null);


            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.label);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
            viewHolder.image = (ImageView) view.findViewById(R.id.img);
            viewHolder.pos = position;


            view.setClickable(true);

            if (!changeable)viewHolder.checkbox.setEnabled(false);


            viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {



                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Model element = (Model) viewHolder.checkbox.getTag();

                    element.setActive(buttonView.isChecked());
                }

            }
            );



            view.setTag(viewHolder);
            viewHolder.checkbox.setTag(list.get(position));

        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));

        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).showPermission());
        holder.checkbox.setChecked(list.get(position).isActive());

        if (list.get(position).isRedundant()) {
            holder.image.setImageDrawable(context.getResources().getDrawable(R.drawable.warning));
        } else {
            holder.image.setImageDrawable(context.getResources().getDrawable(R.drawable.ok));
        }
        return view;

    }

}

在扩展ListActivity的类中,定义的ArrayAdapter在asyncTask中填充了一个arrayList(列表信息在doInBackground()方法中计算):

public class PermissionViewActivity extends ListActivity {

    private final static String TAG = "PMA";
    ...
     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.permissionview);
        ...
    }

    ... 

     private class CurrentPermissions extends AsyncTask<String, Integer, String> {

        private ListActivity la;
        private Context context;



        private int progress = 0;

        public CurrentPermissions(ListActivity la) {
            this.la = la;
            this.context = la;

        }

        protected void onPreExecute() {
        ...

        protected void onPostExecute(String result) {
          adapter = new InteractiveArrayAdapter(la, permissionList, changeable);
          la.setListAdapter(adapter);
          /output with number of listitems
              System.out.println("DEBUG1 " + la.getListAdapter().getCount());
        }
     }
      ...
      @Override
      protected void onListItemClick(ListView l,  View view, int position, long id) {
        super.onListItemClick(l, view, position, id);
        System.out.println("clicked !");
      }
      ...
    }

未执行onListItemClick方法。在ArrayAdapter中定义onClickListener时,click事件起作用:

 view.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {

                    System.out.println("Klick D1 " + list.get(viewHolder.pos));

                }

            });

为什么ArrayAdapter中click事件的定义有效,但ListActivity中的覆盖不起作用?我找不到失败。

4 个答案:

答案 0 :(得分:4)

尝试同时将行布局中的CheckBox设置为focusable

//...
 <CheckBox
    android:id="@+id/check"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="4px"
    android:layout_marginRight="10px"
    android:focusable="false" 
    android:clickable="true">

//...

答案 1 :(得分:0)

如果您想要一个“常规”点击监听器(适用于列表视图中的所有项目),您的活动应该实现OnItemClickListener

public class PermissionViewActivity extends ListActivity implements OnItemClickListener

您需要覆盖方法

@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3)
{
     Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show();
}

然后您必须在列表视图上手动设置点击监听器

离。

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.permissionview);
        ...
        getListView().setOnItemClickListener(this);
}

答案 2 :(得分:0)

我在包含ListView的视图中执行此操作:

ListView lv = (ListView)findViewById(R.id.myList);
lv.setAdapter(new myCustomAdapter,items);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id){
        /* do something i've been clicked */

});

或在onItemSelected事件中

lv.setOnItemSelectedListener(new AdapterView.onItemSelectedListener(){
    public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id){
        ...
    }
    public void onNothingSelected(AdapterView<?> arg0){
        ...
    }
});

希望这有帮助

答案 3 :(得分:0)

如果ListItem中有可点击视图,则需要将focusable标志设置为false。通过在布局中设置focusable = false,当以编程方式设置监听器时,它将被覆盖。

我的解决方案通过以编程方式调用设置可聚焦性

为我工作
listview.setOnItemClickListener(this);
listview.setFocusable(false);