jordan jordan - 12 days ago 6
Android Question

Why won't my listView clear?

I am having a problem with my listView, when I click the clear button I expect my list to clear (obviously) but for some reason my list is not clearing here is the code below, any help would be appreciated. Thank You

MainClass

public class MainActivity extends AppCompatActivity {
ArrayList<SearchResults> searchResults;
private FloatingActionButton add;
private FloatingActionButton clear;
ArrayList<SearchResults> results = new ArrayList<SearchResults>();
final Context context = this;
String time1, people1, description1, mDate, hour, minute, MeridiemIndicator, dateNumber;
int Year, Month, Day, Hour, Minute;


@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
searchResults = new ArrayList<SearchResults>();
setContentView(R.layout.activity_main);
final ListView lv1 = (ListView) findViewById(R.id.ListView);
final CustomAdapter myAdapter = new CustomAdapter(context, R.layout.custom_row_view, results);
lv1.setAdapter(myAdapter);


add = (FloatingActionButton) findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(final View view) {

dialogMethod(lv1);
}


});

lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CustomAdapter adapter = (CustomAdapter) parent.getAdapter();
adapter.remove(results.get(position));
adapter.notifyDataSetChanged();


}
});
lv1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long l) {
CustomAdapter adapter = (CustomAdapter) parent.getAdapter();
adapter.remove(results.get(position));
adapter.notifyDataSetChanged();
dialogMethod(lv1);

return false;
}


});
clear = (FloatingActionButton) findViewById(R.id.clear);
clear.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(final View view) {
results.clear();
myAdapter.notifyDataSetChanged();
}


});
}

private ArrayList<SearchResults> GetSearchResults() {


SearchResults sr1 = new SearchResults();
sr1.setName(" Meeting With: " + people1);
sr1.setTimeOfMeeting(" Starts At: " + time1);
sr1.setDescription(" Details: " + description1);
sr1.setDate(" Date: " + mDate);
sr1.setDateNumber(dateNumber);
results.add(sr1);
Collections.sort(results, new Comparator<SearchResults>() {
public int compare(SearchResults x, SearchResults y) {
return Integer.parseInt(x.getDateNumber()) - Integer.parseInt(y.getDateNumber());
}
});


return results;
}


Here is my custom adapter

public class CustomAdapter extends ArrayAdapter {
private static ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;


public CustomAdapter(Context context, int custom_row_view, ArrayList<SearchResults> results) {
super(context,custom_row_view,results);

searchArrayList = results;
mInflater = LayoutInflater.from(context);

}

public int getCount() {
return searchArrayList.size();
}

public Object getItem(int position) {
return searchArrayList.get(position);
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.name);
holder.txtTimeOfMeeting = (TextView) convertView.findViewById(R.id.timeOfMeeting);
holder.txtDescription = (TextView) convertView.findViewById(R.id.description);
holder.txtDate = (TextView) convertView.findViewById(R.id.date);

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.txtName.setText(searchArrayList.get(position).getName());
holder.txtTimeOfMeeting.setText(searchArrayList.get(position).getTimeOfMeeting());
holder.txtDescription.setText(searchArrayList.get(position).getDescription());
holder.txtDate.setText(searchArrayList.get(position).getDate());

return convertView;
}

static class ViewHolder {
TextView txtName;
TextView txtTimeOfMeeting;
TextView txtDescription;
TextView txtDate;
}
}


The problem more specifically is in the main method just below the floating action button "clear". Again thank you

Answer

After clear list setadapter again.

  clear.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(final View view) {
        results.clear();
        CustomAdapter myAdapter = new CustomAdapter(context, R.layout.custom_row_view, results);
lv1.setAdapter(myAdapter);
        myAdapter.notifyDataSetChanged();
    }


});

OR

Clear adapter

myAdapter.clear();
myAdapter.notifyDataSetChanged();
Comments