2011-12-10 5 views
0

私はカスタムアダプタクラスを使用しています。カーソルを使用してデータベースからデータを取得する。ユーザーがボタンをクリックすると、単一の選択リストを持つカスタムダイアログが開き、そこから項目を選択するとリストがポップアップします。ユーザーが項目を選択するたびに、リストを更新する必要があります。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; 
} 


} 

}

すべてのヘルプは非常にいただければ幸いです!

答えて

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としてください

+0

ああ、私は間違ったコードを貼り付けました。ベースアダプターではありません。私はそれを編集しました。 –

+0

あなたは人生の節約に感謝buddy :))) –

+0

@ johnsmithああ、ちょうど私の答えを更新してくださいそれを見てください。 – GETah

関連する問題