我制作了一些基于listview的应用,可以删除特定行,其中包含一个按钮,如下面的屏幕截图所示:Screenshot
以下是我的课程:
MainActivity: 包com.example.patryk.notes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listView;
LayoutInflater layoutInflater;
ArrayList<Item> itemList = new ArrayList<Item>();
ItemArrayAdapter itemArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
itemArrayAdapter = new ItemArrayAdapter(this, R.layout.list_item, itemList);
listView = (ListView) findViewById(R.id.item_list);
listView.setAdapter(itemArrayAdapter);
layoutInflater = getLayoutInflater();
ViewGroup footer = (ViewGroup) layoutInflater.inflate(R.layout.list_view_footer, listView, false);
listView.addFooterView(footer);
itemList.add(new Item("Item", 1));
}
public void addRow(View v) {
itemList.add(new Item("Item", itemList.size() + 1));
Log.d("add", "metoda add row");
itemArrayAdapter.notifyDataSetChanged();
}
}
ItemArrayAdapter
package com.example.patryk.notes;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by patryk on 04.01.2018.
*/
public class ItemArrayAdapter extends ArrayAdapter<Item> {
private int listItemLayout;
ImageButton deleteButton;
public ItemArrayAdapter(Context context, int layoutId, ArrayList<Item> itemList) {
super(context, layoutId, itemList);
listItemLayout = layoutId;
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Item item = getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(listItemLayout,parent, false);
// viewHolder.item = (TextView) convertView.findViewById(R.id.row_item);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
deleteButton = (ImageButton) convertView.findViewById(R.id.xButtonID);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
**here expected to do something**
}
});
return convertView;
}
private static class ViewHolder {
TextView item;
}
}
项目
package com.example.patryk.notes;
/**
* Created by patryk on 04.01.2018.
*/
public class Item {
private String name;
private double value;
public Item(String name, double value) {
this.value = value;
this.name = name;
}
public double getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我尝试了Stack Overflow的许多想法,但总是有我的线索遗失。首先,我想在ItemArrayAdapter类中对OnclickListener方法进行删除操作,但问题是访问ArrayList&#34; itemList&#34;位于MainActivity类中,因此我无法执行itemList.remove(position)。
另一方面,我想在MainActivity中创建deleteRow方法,并通过添加android:onClick =&#34; deleteRow&#34;将它附加到ItemArrayAdapter类的删除按钮上。标记,但是这样的异常类似于:IllegalStateException 我宁愿选择第一个想法,所以我想知道如何在适配器类中访问这个ArrayList
欢呼:)答案 0 :(得分:1)
ArrayList的引用变量被发送到适配器类。所以你可以轻松地做到这一点:
public class ItemArrayAdapter extends ArrayAdapter<Item> {
private int listItemLayout;
ImageButton deleteButton;
private ArrayList<Item> itemList;
public ItemArrayAdapter(Context context, int layoutId, ArrayList<Item> itemList) {
super(context, layoutId, itemList);
listItemLayout = layoutId;
this.itemList = itemList;
}
然后
deleteButton = (ImageButton) convertView.findViewById(R.id.xButtonID);
deleteButton.setTag(position);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = (int) v.getTag();
itemList.remove(pos);
notifyDataSetChanged();
notifyDataSetInvalidated();
}
});
答案 1 :(得分:0)
您不需要访问MainActivity,项目列表在构造函数中可用
public ItemArrayAdapter(Context context, int layoutId, ArrayList<Item> itemList) {
super(context, layoutId, itemList);
listItemLayout = layoutId;
}
您最好分配给变量并使用它。
喜欢这个
Private ArrayList <Item> itemList;
public ItemArrayAdapter(Context context, int layoutId, ArrayList<Item> itemList) {
super(context, layoutId, itemList);
listItemLayout = layoutId;
this.itemList = itemList
}
删除后,请调用notifytheDataSetChange方法刷新列表视图。