Reputation: 377
I'm making some listview based app with ability to delete specific row with a button in it, like on screenshot below: Screenshot
Here are my classes:
MainActivity: package 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;
}
}
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;
}
}
I've tried many ideas from Stack Overflow but always there has been my clue missing. Firstly I wanted to make delete action on OnclickListener method in ItemArrayAdapter class, but the problem was to access ArrayList "itemList" located in MainActivity class, so I couldn't perform itemList.remove(position).
On the other hand I wanted to make deleteRow method in MainActivity and attach it to delete button on ItemArrayAdapter class by adding android:onClick="deleteRow" tag, but that makes exception similar to that: IllegalStateException I'd rather chose first idea, so I would like to know how to get access to this ArrayList within adapter class
cheers :)
Upvotes: 1
Views: 38
Reputation: 852
Reference variable of ArrayList is send to the adapter class. So you can easily do this:
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;
}
Then
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();
}
});
Upvotes: 1
Reputation: 3607
You don't need to access the MainActivity, item list is available in the constructor
public ItemArrayAdapter(Context context, int layoutId, ArrayList<Item> itemList) {
super(context, layoutId, itemList);
listItemLayout = layoutId;
}
You better assign to a variable and use it.
Like this
Private ArrayList <Item> itemList;
public ItemArrayAdapter(Context context, int layoutId, ArrayList<Item> itemList) {
super(context, layoutId, itemList);
listItemLayout = layoutId;
this.itemList = itemList
}
After remove please call notifytheDataSetChange method to refresh the list view.
Upvotes: 0