0
我正在使用自定義Adapter類。使用遊標從數據庫中檢索數據。當用戶點擊一個按鈕時,它將打開一個具有單選列表的自定義對話框,當用戶從中選擇一個項目時,將彈出一個列表。每次用戶選擇一個項目時,都必須刷新列表。listview不刷新如何清除列表視圖項?
我的問題是列表項添加到前一個列表而不是刷新。
代碼 - >
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;
}
}
}
任何幫助,將不勝感激!
哦,我粘貼了錯誤的代碼,它是基礎適配器而不是陣列適配器。我編輯它。 –
你是一個救星感謝budddy :))) –
@johnsmith啊好吧,只是更新我的答案,請看看它。 – GETah