Reputation: 1532
Basically, this is the relevant chunk of code of the MainActivity
class:
private ListView listView;
private ArrayList<String> tasks = new ArrayList<String>();
private CustomListViewAdapter customListViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tasks.add("Hfrfello");
tasks.add("Heefrefllo");
tasks.add("Hellefrefo");
tasks.add("Helggglo");
tasks.add("Helggglo");
tasks.add("aaHello");
tasks.add("aaaaaaHelaaalo");
tasks.add("Hfrfello");
tasks.add("Heefrefllo");
tasks.add("Hellefrefo");
tasks.add("Helggglo");
tasks.add("Helggglo");
tasks.add("aaHello");
tasks.add("aaaaaaHelaaalo");
this.listView = (ListView) findViewById(R.id.tasks);
this.customListViewAdapter = new CustomListViewAdapter(getApplicationContext(), this.tasks);
this.listView.setAdapter(this.customListViewAdapter);
this.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//deleteTask(position);
customListViewAdapter.remove(position);
//tasks.remove(position);
customListViewAdapter.notifyDataSetChanged();
}
});
}
And this is my CustomListViewAdapter
class:
package com.hassanalthaf.dailytaskmanager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomListViewAdapter extends BaseAdapter {
private Context context;
private ArrayList<String> tasks;
private static LayoutInflater inflater = null;
public CustomListViewAdapter(Context context, ArrayList<String> data) {
this.context = context;
this.tasks = data;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return this.tasks.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public void remove(int position) {
this.tasks.remove(position);
System.out.println(this.tasks);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (convertView == null) {
view = this.inflater.inflate(R.layout.list_row, null);
TextView taskName = (TextView) view.findViewById(R.id.taskName);
ImageView image = (ImageView) view.findViewById(R.id.deleteButton);
String task = this.tasks.get(position);
taskName.setText(task);
}
return view;
}
}
My issue: When I click on my Graphical User Interface, it does delete and update the ListView, however, it updates it wrong. What it does is, when I click on the first 3 elements(which should delete them), it does delete them from the ArrayLists and I get the right output after getting rid of them. However, in the user interface, instead of deleting what I clicked, it just removes the bottom items, and doesn't touch the top ones. How would I solve this issue?
Upvotes: 0
Views: 54
Reputation: 1612
I agreed with @cybersam. I think it would be optimized if you use the Holder pattern as:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyHolder holder;
View view = convertView;
if (view == null) {
view = this.inflater.inflate(R.layout.list_row, null);
holder = new MyHolder(view);
view.setTag(holder);
}
else
{
holder = (MyHolder) view.getTag();
}
String task = this.tasks.get(position);
holder.taskName.setText(task);
return view;
}
class MyHolder {
TextView taskName;
public MyHolder(View view) {
taskName = (TextView) view.findViewById(R.id.taskName);
}
}
Upvotes: 1
Reputation: 67019
Your getView()
is not setting the state of recycled convertView
objects, so they continue to show the old state. Try this:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (convertView == null) {
view = this.inflater.inflate(R.layout.list_row, null);
}
TextView taskName = (TextView) view.findViewById(R.id.taskName);
String task = this.tasks.get(position);
taskName.setText(task);
return view;
}
Upvotes: 1