我正在使用自定义Adapter类。并使用游标从数据库中检索数据。当用户单击一个按钮时,它将打开一个带有单个选择列表的自定义对话框,当用户从中选择一个项目时,将弹出一个列表。每次用户选择一个项目时,必须刷新列表。
我的问题是列表项会附加到上一个列表而不是刷新。
码 - >
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;
}
}
}
任何帮助都将受到高度赞赏!
答案 0 :(得分:1)
在重新填写之前,您忘了清除列表。
需要刷新时,请先执行channelAndRating.clear();
。
您可以更新myBaseAdapter
类并为其添加一个明确的方法:
class myBaseAdapter extends BaseAdapter{
private ArrayList<HashMap<String,Object>> cAndr;
private LayoutInflater mInflater;
//.... Other stuff here
public void clear(){
cAndr.clear();
}
}
附注:在Java中,类名始终具有首字母。我会重命名myBaseAdapter
并将其称为MyBaseAdapter
而不是