Reputation: 14808
I am using a custom Adapter class. And retrieving data from database using cursor. When user click a button it will open a custom dialog with single selection list and when user will choose a item from it a list will pop up. every time user will select a item the list must be refreshed.
My problem is that list items are appending to the previous list instead of refreshing.
Code-->
public class ChannelListActivity extends Activity {
private Cursor countryCursor;
private Cursor channelCursor;
public DbH db;
private ArrayList <HashMap<String, Object>> channelAndRating;
private HashMap<String, Object> hm;
private static final String CHANNEL_NAME_KEY = "channel_names";
private static final String RATINGKEY = "ratings";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageButton tv_button = (ImageButton)findViewById(R.id.tv_menu);
final ListView listView = (ListView)findViewById(R.id.list);
channelAndRating = new ArrayList<HashMap<String,Object>>();
final myBaseAdapter baseadapter = new myBaseAdapter(channelAndRating, ChannelListActivity.this);
try {
db=new DbH(this);
} catch (IOException e2) {
e2.printStackTrace();
}
try {
db.createdatabase();
} catch (IOException e) {
e.printStackTrace();
}
db.opendatabase();
countryCursor= db.dataCountryName();
countryCursor.moveToFirst();
final String country_names[] = new String[countryCursor.getCount()];
final String country_id [] = new String [countryCursor.getCount()];
for(int icount = 0 ; icount < countryCursor.getCount(); icount++) {
country_names[icount] = countryCursor.getString(countryCursor.getColumnIndex("country"));
country_id[icount] = countryCursor.getString(countryCursor.getColumnIndex("_id"));
countryCursor.moveToNext();
}
tv_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder alt_bld = new AlertDialog.Builder(ChannelListActivity.this);
alt_bld.setIcon(R.drawable.favourate_icon);
alt_bld.setTitle("Select the Country ...");
alt_bld.setSingleChoiceItems(country_names, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
listView.setAdapter(null);
baseadapter.notifyDataSetChanged();
listView.setAdapter(baseadapter);
listView.setTextFilterEnabled(true);
Log.v("LIST VIEW ", "adapter is null fdf");
channelCursor = db.dataChannelsName(country_id[item]);
channelCursor.moveToFirst();
final String channels_name[] = new String[channelCursor.getCount()];
final int channel_rating[] = new int [channelCursor.getCount()]; ;
for(int icount =0 ; icount<channelCursor.getCount(); icount++) {
channels_name[icount] = channelCursor.getString(channelCursor.getColumnIndex("name"));
channel_rating[icount] = Integer.parseInt(channelCursor.getString(channelCursor.getColumnIndex("votes")));
channelCursor.moveToNext();
}
for(int icount = 0 ;icount <channelCursor.getCount(); icount++)
{
hm = new HashMap<String, Object>();
hm.put(CHANNEL_NAME_KEY,channels_name[icount]);
hm.put(RATINGKEY, channel_rating[icount]);
channelAndRating.add(hm);
}
Toast.makeText(getApplicationContext(), "Phone Model = "+country_id[item] +" " +country_names[item], Toast.LENGTH_SHORT).show();
dialog.dismiss();
baseadapter.notifyDataSetChanged();
baseadapter.notifyDataSetInvalidated();
}
});
AlertDialog alert = alt_bld.create();
alert.show();
}
});
}
class myBaseAdapter extends BaseAdapter{
private ArrayList<HashMap<String,Object>> cAndr;
private LayoutInflater mInflater;
public myBaseAdapter(ArrayList<HashMap<String, Object>> channelAndRating,
Context channelListActivity) {
cAndr = channelAndRating;
mInflater = LayoutInflater.from(channelListActivity);
}
public int getCount() {
return cAndr.size();
}
public Object getItem(int position) {
return cAndr.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.list_box, null);
holder = new ViewHolder();
holder.tv = (TextView)convertView.findViewById(R.id.text1);
holder.mRatingBar = (RatingBar)convertView.findViewById(R.id.star);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setText((String)cAndr.get(position).get(CHANNEL_NAME_KEY));
holder.mRatingBar.setRating((Integer)cAndr.get(position).get(RATINGKEY));
return convertView;
}
class ViewHolder {
RatingBar mRatingBar;
TextView tv;
}
}
}
Any help would be highly appreciated!
Upvotes: 0
Views: 568
Reputation: 21419
You forgot to clear the list before filling it up again.
When a refresh is required, do a channelAndRating.clear();
first.
You can update the myBaseAdapter
class and add a clear method to it:
class myBaseAdapter extends BaseAdapter{
private ArrayList<HashMap<String,Object>> cAndr;
private LayoutInflater mInflater;
//.... Other stuff here
public void clear(){
cAndr.clear();
}
}
Side note: in Java, class names always have the first letter in capital. I would rename myBaseAdapter
and call it MyBaseAdapter
instead
Upvotes: 1