Я использую пользовательский класс адаптера. И получение данных из базы данных с помощью курсора. Когда пользователь нажимает кнопку, он открывает настраиваемое диалоговое окно с одним списком выбора, а когда пользователь выбирает элемент из него, появляется список. каждый раз, когда пользователь выбирает элемент, список должен обновляться.
Моя проблема в том, что элементы списка добавляются в предыдущий список, а не обновляются.
код ->
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;
}
}
}
Любая помощь будет принята с благодарностью!