我已经学会了如何创建一个自定义ListItem并向其添加一个可点击的ImageButton,部分归功于这个答案:
how to use onClickListener method of Button on a ListView
但是,我有两个单独的活动,其中包含用户可能想要删除数据的ArrayLists。我是否可以为每个活动添加代码,以检测何时按下特定的ListItem的“删除”按钮,或者我是否必须在ListItemAdapter中定义该方法并尝试找到一种方法来检测当前正在运行的活动? (到目前为止,我的应用程序不需要任何特殊权限,但我确信在以后的版本中会有所改变。)
供参考,这是我的ListItem.java的代码:
package net.player1diary.pocketscribe;
// custom class to be used for an advanced List Item display
public class ListItem {
private int listIcon; // resource ID of the icon
private String listText; // the text
private String listDetail; // details about the list item (optional)
private boolean deleteEnabled; // whether or not the list item contains a "delete" button
// constructors
public ListItem() {};
public ListItem(int resource, String t, String d, boolean del) {
this.listIcon = resource;
this.listText = t;
this.listDetail = d;
this.deleteEnabled = del;
}
// functions for displaying variables
public int getListIcon() {
return this.listIcon;
}
public String getListText() {
return this.listText;
}
public String getListDetail() {
return this.listDetail;
}
public boolean getDeleteListItemEnabled() {
return this.deleteEnabled;
}
// functions for altering variables
public void setListIcon(int newIcon) {
this.listIcon = newIcon;
}
public void setListText(String newText) {
this.listText = newText;
}
public void setListDetail(String newDetail) {
this.listDetail = newDetail;
}
public void setDeleteListItemEnabled(boolean newStatus) {
this.deleteEnabled = newStatus;
}
}
ListItemAdapter.java的代码:
package net.player1diary.pocketscribe;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
// custom advanced List Adapter
public class ListItemAdapter extends ArrayAdapter<ListItem> {
private List<ListItem> objects;
public ListItemAdapter(Context context, List<ListItem> objects) {
super(context, R.layout.list_item, objects);
this.objects = objects;
}
// show the customized list items
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.list_item, null);
}
ListItem p = objects.get(position);
// display each list item
if (p != null) {
ImageView img = (ImageView) v.findViewById(R.id.imgListIcon);
final TextView text = (TextView) v.findViewById(R.id.txtListText);
final TextView detail = (TextView) v.findViewById(R.id.txtListDetail);
ImageButton deleteButton = (ImageButton) v.findViewById(R.id.btnListDeleteItem);
// prepare the delete button, if it exists
deleteButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO take action, depending on context
Toast.makeText(getContext(), "Are you sure you want to delete " + text.getText().toString() + "?", Toast.LENGTH_SHORT).show();
}
});
if (img != null) {
img.setImageResource(p.getListIcon());
}
if (text != null) {
text.setText(p.getListText());
}
if (detail != null) {
detail.setText(p.getListDetail());
}
// hide the detail from view if there is no text there
if (detail.getText().length() == 0) {
detail.setVisibility(View.INVISIBLE);
}
// hide the delete button from view if it is disabled
if (!p.getDeleteListItemEnabled()) {
deleteButton.setVisibility(View.GONE);
}
}
return v;
}
}
最后,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" >
<ImageView
android:id="@+id/imgListIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="@string/icon"
android:maxHeight="@dimen/list_icon"
android:maxWidth="@dimen/list_icon"
android:minHeight="32dp"
android:minWidth="32dp"
android:src="@drawable/ic_book" />
<TextView
android:id="@+id/txtListDetail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtListText"
android:layout_below="@+id/txtListText"
android:hint="@string/list_item_detail"
android:textColor="@color/list_detail"
android:textIsSelectable="false" />
<TextView
android:id="@+id/txtListText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/imgListIcon"
android:ellipsize="end"
android:hint="@string/list_item"
android:textColor="@color/list_text"
android:textIsSelectable="false"
android:textSize="@dimen/list_item"
android:textStyle="bold" />
<ImageButton
android:id="@+id/btnListDeleteItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_delete" android:contentDescription="@string/delete"/>
</RelativeLayout>
答案 0 :(得分:0)
由于您的两个活动都有自己的适配器实例(我推测),您可以创建一个在单击删除时传递位置的界面。活动实现该接口并处理arraylist的正确删除。这样,当单击列表项删除时,正确的活动就会被调用。