setOnItemClickListener 不适用于 ListView 中的按钮
setOnItemClickListener doesn't work for a button in a ListView
我正在尝试在每个列表项中放置一个按钮。这是想法:
Text1____________[btn]
Text2____________[btn]
等...
项目的 XML 是:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:id="@+id/list_item_textview"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/deleteButton"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@null"
android:clickable="true"
android:focusable="false"
android:src="@drawable/ic_delete_black_24dp"/>
</RelativeLayout>
有两个活动共享同一个片段。此片段使用 SimpleCursorAdapter 从数据库中获取数据。效果很好。
创建和设置适配器后,我尝试找到按钮并执行 setOnClickListener。这不起作用。
片段中onCreateView方法的代码如下:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_item_scroll, container, false);
ListView listView = (ListView) rootView.findViewById(R.id.listview_item);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
getActivity(),
R.layout.list_item,
((ListItems)getActivity()).getCursor(1,1),
new String[] {((ListItems) getActivity()).getColumnName()},
new int[] {R.id.list_item_textview}, 0);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity(), ((ListItems) getActivity()).getGoToClass());
intent.putExtra("apiary_id",(int) id);
intent.putExtra("hive_id", -1);
startActivity(intent);
}
});
/* SOMEWHERE BELOW IS THE PROBLEM */
View list_item_view = inflater.inflate(R.layout.list_item, container, false);
ImageButton deleteButton = (ImageButton) list_item_view.findViewById(R.id.deleteButton);
System.out.println("====================>" + deleteButton.toString()); //This prints something which includes the id of the button, so it is finding it.
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("THIS IS NOT WORKING!!!!");
}
});
return rootView;
}
这是我的第一个 android 应用程序,这个问题困扰我好几天了,没有任何进展。非常感谢任何提示!
非常感谢!
制作一个像这样的自定义列表适配器
public class CustomListAdapter extends BaseAdapter {
private String[] items;
private LayoutInflater inflater;
Context context;
public CustomListAdapter(Context c, String[] items) {
this.items = items;
inflater =LayoutInflater.from(c);
context = c;
}
@Override
public int getCount() {
return items.length;
}
@Override
public Object getItem(int location) {
return items[location];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = inflater.inflate(R.layout.list_item, null);
TextView txt = (TextView) convertView.findViewById(R.id.list_item_textview);
ImageButton button = (ImageButton) convertView.findViewById(R.id.deleteButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "clicked ",
Toast.LENGTH_SHORT).show();
}
});
txt.setText(""+items[position]);
return convertView;
}
}
在你的片段中
CustomListAdapter customListAdapter;
customListAdapter = new CustomListAdapter(getActivity(),str);
list.setAdapter(customListAdapter);
希望对您有所帮助!!!
我正在尝试在每个列表项中放置一个按钮。这是想法:
Text1____________[btn]
Text2____________[btn]
等...
项目的 XML 是:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:id="@+id/list_item_textview"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/deleteButton"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@null"
android:clickable="true"
android:focusable="false"
android:src="@drawable/ic_delete_black_24dp"/>
</RelativeLayout>
有两个活动共享同一个片段。此片段使用 SimpleCursorAdapter 从数据库中获取数据。效果很好。
创建和设置适配器后,我尝试找到按钮并执行 setOnClickListener。这不起作用。
片段中onCreateView方法的代码如下:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_item_scroll, container, false);
ListView listView = (ListView) rootView.findViewById(R.id.listview_item);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
getActivity(),
R.layout.list_item,
((ListItems)getActivity()).getCursor(1,1),
new String[] {((ListItems) getActivity()).getColumnName()},
new int[] {R.id.list_item_textview}, 0);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity(), ((ListItems) getActivity()).getGoToClass());
intent.putExtra("apiary_id",(int) id);
intent.putExtra("hive_id", -1);
startActivity(intent);
}
});
/* SOMEWHERE BELOW IS THE PROBLEM */
View list_item_view = inflater.inflate(R.layout.list_item, container, false);
ImageButton deleteButton = (ImageButton) list_item_view.findViewById(R.id.deleteButton);
System.out.println("====================>" + deleteButton.toString()); //This prints something which includes the id of the button, so it is finding it.
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("THIS IS NOT WORKING!!!!");
}
});
return rootView;
}
这是我的第一个 android 应用程序,这个问题困扰我好几天了,没有任何进展。非常感谢任何提示!
非常感谢!
制作一个像这样的自定义列表适配器
public class CustomListAdapter extends BaseAdapter {
private String[] items;
private LayoutInflater inflater;
Context context;
public CustomListAdapter(Context c, String[] items) {
this.items = items;
inflater =LayoutInflater.from(c);
context = c;
}
@Override
public int getCount() {
return items.length;
}
@Override
public Object getItem(int location) {
return items[location];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = inflater.inflate(R.layout.list_item, null);
TextView txt = (TextView) convertView.findViewById(R.id.list_item_textview);
ImageButton button = (ImageButton) convertView.findViewById(R.id.deleteButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "clicked ",
Toast.LENGTH_SHORT).show();
}
});
txt.setText(""+items[position]);
return convertView;
}
}
在你的片段中
CustomListAdapter customListAdapter;
customListAdapter = new CustomListAdapter(getActivity(),str);
list.setAdapter(customListAdapter);
希望对您有所帮助!!!