Michael Setyadarma Michael Setyadarma - 5 months ago 9
Android Question

Remove item from ArrayAdapter

I am trying to implement remove item (because it's stating to update from 0 to 1 from listView in Android when user press textView in each item. e.g:

There are 3 items


  • apple 0

  • orrange 0

  • grape 0



In each item there is a textView as a button. When user press textView for Apple, my app will update item 'apple' to 1, and remove it from listView, and in listview 'apple' will be gone.
This is my list code

public class ListPelangganUnsyncFragment extends ListFragment
{
AdapterListPelanggan pelangganAdp;
ArrayList<Pelanggan> pelanggan = null;
FloatingActionButton fab;
Button sync;
int list = 0;
int newList = 0;
ListView listView;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState)
{
final View rootView = inflater.inflate(R.layout.list_pelanggan, container, false);
super.onCreate(savedInstanceState);

listView = (ListView) rootView.findViewById(android.R.id.list);
// Buat kalo udah ada login
// SharedPreferences setting

pelanggan = new ArrayList<Pelanggan>();

// Buat ngambil data dari DB lokal
final DBPelanggan dbpel = new DBPelanggan(getActivity()); // get context dri activity, return null klo fragment ini ga attach
try
{
dbpel.open();
pelanggan = dbpel.getDataPelanggan();
dbpel.close();
list = pelanggan.size();
Log.d("List", list+"");
}
catch (SQLException e)
{
e.printStackTrace();
}

sync = (Button) rootView.findViewById(R.id.sinkron);
Button add = (Button) rootView.findViewById(R.id.addPelanggan);
add.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(rootView.getContext(), FormPelangganActivity.class);
intent.putExtra("fungsi", 0);
startActivity(intent);
getActivity().finish();
}
});

sync.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
try
{
dbpel.open();
dbpel.updateStat(pelanggan);
pelanggan = dbpel.getDataPelanggan();
dbpel.close();
if(pelanggan != null)
{
pelangganAdp = new AdapterListPelanggan(getActivity(), R.layout.isi_list_pelanggan, pelanggan);
listView.setAdapter(pelangganAdp);
}
((MainActivity)getActivity()).selectFragment(0);

}
catch (SQLException e)
{
e.printStackTrace();
}
}
});




//Adapter

listView.setClickable(true);
if(pelanggan != null)
{
pelangganAdp = new AdapterListPelanggan(getActivity(), R.layout.isi_list_pelanggan, pelanggan);
listView.setAdapter(pelangganAdp);

newList = listView.getCount();
Log.d("List", newList + "");
}

return rootView;
}


and this is my adapter:

public class AdapterListPelanggan extends ArrayAdapter<Pelanggan>
{
private Context context;
private int resource;
private ArrayList<Pelanggan> arPelanggan;
Holder h = null;
DBPelanggan db;
private boolean status;

// Holder = Object
static class Holder
{
TextView tv_KTP, tv_Nama;
ImageView syncButton;
}

public AdapterListPelanggan(Context context, int resource, ArrayList<Pelanggan> arPelanggan)
{
super(context, resource, arPelanggan);

this.context = context;
this.resource = resource;
this.arPelanggan = arPelanggan;

Log.d("ListAdapter", "Jumlah List c "+arPelanggan.size());
}

public AdapterListPelanggan(Context context, int resource, ArrayList<Pelanggan> arPelanggan, boolean status)
{
super(context, resource, arPelanggan);

this.context = context;
this.resource = resource;
this.arPelanggan = arPelanggan;
this.status = status;
}

public View getView(final int position, View convertView, ViewGroup parent)
{
h = new Holder();
final Pelanggan pelanggan = arPelanggan.get(position); // Set Data per urutan
Log.d("ListAdapter", "ID Pelanggan b "+pelanggan.getId_pelanggan()+"");
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(resource, parent, false);

h.tv_KTP = (TextView)convertView.findViewById(R.id.tv_ktp_isi_lp);
h.tv_Nama = (TextView)convertView.findViewById(R.id.tv_nama_isi_lp);
h.syncButton = (ImageView)convertView.findViewById(R.id.syncButton);

if(status)
{
h.syncButton.setVisibility(View.GONE);
}

convertView.setTag(h);
}
else
{
h = (Holder)convertView.getTag();
}

// ------------------ Button Sync ------------------

h.syncButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
try
{
db = new DBPelanggan(getContext());
db.open();
db.updateStatPelanggan(pelanggan);
Log.d("ListAdapter", "ID hilang " + pelanggan.getId_pelanggan());
arPelanggan.remove(pelanggan);
arPelanggan = db.getDataPelanggan();
Log.d("ListAdapter", "Jumlah List c "+arPelanggan.size());


Log.d("ListAdapter", pelanggan.getId_pelanggan()+"ID Sekarang");
db.close();
notifyDataSetChanged();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
});
// ---------------------------------------------------------------

h.tv_KTP.setText(pelanggan.getId_pelanggan());
h.tv_Nama.setText(pelanggan.getNama());

return convertView;
}


}


The problem is when I press textView for 'apple', indeed it was updated in db and gone from my list, but when I press 'orrange' after press 'apple' my app gives error.

This is my error:


11-17 10:34:55.639 5429-5429/com.example.michael.ims_application_apps E/AndroidRuntime: at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
11-17 10:34:55.639 5429-5429/com.example.michael.ims_application_apps E/AndroidRuntime: at java.util.ArrayList.get(ArrayList.java:304)
11-17 10:34:55.639 5429-5429/com.example.michael.ims_application_apps E/AndroidRuntime: at com.example.michael.ims_application_apps.AdapterListPelanggan.getView(AdapterListPelanggan.java:63)
11-17 10:34:55.639 5429-5429/com.example.michael.ims_application_apps E/AndroidRuntime: at android.widget.AbsListView.obtainView(AbsListView.java:2260)
11-17 10:34:55.639 5429-5429/com.example.michael.ims_application_apps E/AndroidRuntime: at android.widget.ListView.makeAndAddView(ListView.java:1769)
11-17 10:34:55.639 5429-5429/com.example.michael.ims_application_apps E/AndroidRuntime: at android.widget.ListView.fillDown(ListView.java:672)
11-17 10:34:55.639 5429-5429/com.example.michael.ims_application_apps E/AndroidRuntime: at android.widget.ListView.fillGap(ListView.java:636)
11-17 10:34:55.639 5429-5429/com.example.michael.ims_application_apps E/AndroidRuntime: at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5045)
11-17 10:34:55.639 5429-5429/com.example.michael.ims_application_apps E/AndroidRuntime: at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3202)
11-17 10:34:55.639 5429-5429/com.example.michael.ims_application_apps E/AndroidRuntime: at android.widget.AbsListView.onTouchEvent(AbsListView.java:3476)


Anyone can advice me how to fix it? Thank you.

Answer

The problem is in this line,

  arPelanggan = db.getDataPelanggan();

You are reloading the data from db after removing.

Comments