我创建了一个自定义的ListView
。 ListView
包含有关每个对象的一些信息,以及2个按钮,用于编辑和删除。我已通过自定义ArrayList
为ListViewAdapter
对象添加了该信息。
现在,我希望按钮在单击时更新ArrayList
对象。我找不到办法去做。我按照此链接中的说明进行操作..
http://www.migapro.com/click-events-listview-gridview/
程序不会抛出任何错误。但它也没有做任何事情。现在,我只是想通过为每次点击按钮举杯来测试。
ListViewAdapter.getView()
方法的代码是:
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
ViewHolder holder;
// check if the view already exists
// if so, no need to inflate and findViewById again!
if (convertView == null) {
// Inflate the custom row layout from your XML.
convertView = mInflater.inflate(R.layout.list_item, null);
// create a new "Holder" with subviews
holder = new ViewHolder();
holder.itemNameView = (TextView) convertView.findViewById(R.id.item_name);
holder.itemExpiryView = (TextView) convertView.findViewById(R.id.item_expiry);
// Taking care of the buttons
holder.editButton = (Button) convertView.findViewById(R.id.button_edit);
holder.editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0); // Let the event be handled in onItemClick()
}
});
holder.deleteButton = (Button) convertView.findViewById(R.id.button_delete);
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0); // Let the event be handled in onItemClick()
}
});
// hang onto this holder for future recycling
convertView.setTag(holder);
} else {
// skip all the expensive inflation/findViewById
// and just get the holder you already made
holder = (ViewHolder) convertView.getTag();
}
// More code after this
// Grab the title and author from the JSON
String name = "";
String expiry = "7 days";
// Write appropriate codes to obtain values for the string variables above
name = (String) getItem(position);
// Send these Strings to the TextViews for display
holder.itemNameView.setText(name);
holder.itemExpiryView.setText(expiry);
return convertView;
}
我在MainActivity中实现了OnItemClickListener()并编写了这段代码片段来处理按钮点击。
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
long viewId = view.getId();
if (viewId == R.id.button_edit) {
Toast.makeText(this, "Edit Button CLicked", Toast.LENGTH_SHORT).show();
} else if (viewId == R.id.button_delete) {
Toast.makeText(this, "Delete Button CLicked", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "ListView clicked" + id, Toast.LENGTH_SHORT).show();
}
}
相应的布局文件如下所示:
<?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">
<TextView
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/item_expiry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/item_name" />
<Button
android:id="@+id/button_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/item_expiry"
android:layout_alignParentLeft="true"
android:text="Edit"
android:clickable="true" />
<!--android:onClick="editItem" />-->
<Button
android:id="@+id/button_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/item_expiry"
android:layout_alignParentRight="true"
android:text="Delete"
android:clickable="true" />
<!--android:onClick="ItemEdits.deleteItem" />-->
</RelativeLayout>
如果你们需要更多代码片段,请告诉我们。在logcat上找不到任何东西。因此,无法帮助处理任何错误代码/警告消息。非常感谢。
我在这里想要实现的是,当我点击编辑/删除按钮时,我需要调用一个函数来修改存储列表值的ArrayList。不知何故,我需要传递点击项目的位置,以便我可以更新正确的信息。有没有更好的方法呢?如果是这样,那么请帮我解决一下代码示例。
答案 0 :(得分:0)
在if(convertview == null)中添加单击操作。需要更改尝试以下内容。这只是一个问题。试试这个,让我知道结果会是什么。
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
ViewHolder holder;
// check if the view already exists
// if so, no need to inflate and findViewById again!
if (convertView == null) {
// Inflate the custom row layout from your XML.
convertView = mInflater.inflate(R.layout.list_item, null);
// create a new "Holder" with subviews
holder = new ViewHolder();
holder.itemNameView = (TextView) convertView.findViewById(R.id.item_name);
holder.itemExpiryView = (TextView) convertView.findViewById(R.id.item_expiry);
// Taking care of the buttons
holder.editButton = (Button) convertView.findViewById(R.id.button_edit);
holder.deleteButton = (Button) convertView.findViewById(R.id.button_delete);
// hang onto this holder for future recycling
convertView.setTag(holder);
} else {
// skip all the expensive inflation/findViewById
// and just get the holder you already made
holder = (ViewHolder) convertView.getTag();
}
holder.editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0); // Let the event be handled in onItemClick()
}
});
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0); // Let the event be handled in onItemClick()
}
});
// More code after this
// Grab the title and author from the JSON
String name = "";
String expiry = "7 days";
// Write appropriate codes to obtain values for the string variables above
name = (String) getItem(position);
// Send these Strings to the TextViews for display
holder.itemNameView.setText(name);
holder.itemExpiryView.setText(expiry);
return convertView;
}
注意:在getView方法中使用onclick或使用onItemClick方法。使用两者都会产生一些问题。