gpt4 book ai didi

android - ListActivity 的 onListItemClick 不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 08:10:01 25 4
gpt4 key购买 nike

在我的带有 ListActivity 的应用程序中,覆盖的 onListItemClick 回调不会通过单击 ListView 项目触发。

我已经使用以下 xml 文件 permissionview.xml 定义了一个自定义 ListView

<?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>

和 View 项布局 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 时,点击事件有效:

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

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

}

});

为什么在 ArrayAdapter 中点击事件的定义有效,但在 ListActivity 中覆盖却不行?我找不到故障。

最佳答案

尝试将行布局中的 CheckBox 设置为不可可聚焦:

//...
<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">

//...

关于android - ListActivity 的 onListItemClick 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10709669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com