2012-04-17 18 views
1

通常のリストビューは、arrayadapterなどに 'clear'を呼び出すことでクリアできることはわかっていますが、カスタムリストビューであればアイテムをクリアしますか?Android:カスタムリストビューのすべてのアイテムをクリアする方法

は、ここに私のコードです:

活動:

public class GroupList extends Activity{ 
    TextView v_no_groups; 
    Cursor grouplist_cur; 
    Context context = GroupList.this; 
    SQLiteDatabase mydb=null; 
    ListView v_grouplist; 
    ArrayList<String> grpname_arr = new ArrayList<String>(); 
    ArrayList<String> grpid_arr = new ArrayList<String>(); 
    ArrayList<Integer> memcount_arr = new ArrayList<Integer>(); 
    ArrayList<Integer> totaltrans_arr = new ArrayList<Integer>(); 
    M_grplist_adapter mga; 
    Button v_creategrp; 
    List<M_grplist_class> glist; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.grouplist); 
    } 

    @Override 
    protected void onResume() { 

     super.onResume(); 

     //mga.clearlistview(); 

     v_creategrp = (Button)findViewById(R.id.x_creategrp); 
     v_grouplist = (ListView)findViewById(android.R.id.list); 
     registerForContextMenu(v_grouplist); 
     v_no_groups = (TextView)findViewById(R.id.x_no_groups); 

     mydb = this.openOrCreateDatabase("moneydb", 0, null); 
     mydb.execSQL("CREATE TABLE IF NOT EXISTS grouptable(grpname varchar(30),grpid varchar(10),memcount number(3),totaltrans number(3))"); 
     grouplist_cur = mydb.rawQuery("SELECT * from grouptable", null); 

     if(grouplist_cur.getCount()>0) 
     { 

      grouplist_cur.moveToFirst(); 
      do{ 
       grpname_arr.add(grouplist_cur.getString(0)); 
       memcount_arr.add(grouplist_cur.getInt(2)); 
       totaltrans_arr.add(grouplist_cur.getInt(3)); 
      }while(grouplist_cur.moveToNext()); 

      glist = new ArrayList<M_grplist_class>(); 
      for(int i=grpname_arr.size()-1;i>=0;i--){ 
       glist.add(new M_grplist_class(grpname_arr.get(i),memcount_arr.get(i),totaltrans_arr.get(i))); 
      } 

      mga = new M_grplist_adapter(context,glist); 
      v_grouplist.setAdapter(mga); 

      grouplist_cur.close(); 
      mydb.close(); 
     } 
     else 
     { 
      grouplist_cur.close(); 
      mydb.close(); 
     } 

     v_creategrp.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       Intent int_newgrp = new Intent(context,NewGroup.class); 
       startActivity(int_newgrp); 
      } 
     }); 
    } 

grplist_adapterクラス:

public class M_grplist_adapter extends BaseAdapter{ 
    private List<M_grplist_class> items = null; 
    private Context context; 
    private int index; 

    public M_grplist_adapter(Context context,List<M_grplist_class> items){ 
     items.clear(); 
     notifyDataSetChanged(); 
     this.items = items; 
     this.context = context; 
     //notifyDataSetChanged(); 
     Log.i("Dillu", "you are here M_grplist_adapter"); 
    } 

    @Override 
    public int getCount() { 
     Log.i("Dillu", "you are here getcount"); 
     return items.size(); 

    } 

    @Override 
    public Object getItem(int position) { 
     index = position; 
     Log.i("Dillu", "you are here getitem"); 
     return items.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     Log.i("Dillu", "you are here getitemid"); 
     return position; 
    } 

    public String clear() 
    { 
     String name = items.get(index).getgrpname(); 
     items.remove(index); 
     notifyDataSetChanged(); 
     return name; 
    } 

    @Override 
    public View getView(int index, View convertView, ViewGroup parent) { 
     Log.i("Dillu", "you are here getview"); 
     LinearLayout rowLayout=null; 
     if(convertView==null) 
      rowLayout = (LinearLayout)LayoutInflater.from(context).inflate(R.layout.m_grplist_item,parent,false); 
     else 
      rowLayout = (LinearLayout)convertView; 

     TextView gname = (TextView)rowLayout.findViewById(R.id.grpname); 
     gname.setText(items.get(index).getgrpname()); 

     TextView gmems = (TextView)rowLayout.findViewById(R.id.noofmems); 
     int int_temp = items.get(index).getmemcount(); 
     String str_temp = Integer.toString(int_temp); 
     gmems.setText(str_temp); 

     TextView gtrans = (TextView)rowLayout.findViewById(R.id.nooftrans); 
     int int_temp_1 = items.get(index).gettotaltrans(); 
     String str_temp_1 = Integer.toString(int_temp_1); 
     gtrans.setText(str_temp_1); 

     return rowLayout; 
    } 

} 

M_grplist_class:

public class M_grplist_class { 
    String loc_grpname; 
    int loc_memcount; 
    int loc_totaltrans; 

    public M_grplist_class(String grpname,int memcount,int totaltrans){ 
     loc_grpname = grpname; 
     loc_memcount = memcount; 
     loc_totaltrans = totaltrans; 
    } 

    public String getgrpname(){ 
     return loc_grpname; 
    } 

    public int getmemcount(){ 
     return loc_memcount; 
    } 

    public int gettotaltrans(){ 
     return loc_totaltrans; 
    } 

} 

は、このためのソリューションを提供してください。

+0

で解決しましたか? –

+0

いいえ、私の問題は解決しませんでした。 –

答えて

6

基本的なリストをクリアした後、notifyDataSetChanged()に電話する必要があります。あなたのListViewは自分自身をオブザーバーに登録し、その呼び出しに反応します。あなたのコードで

public void clearlistview() 
{ 
    items.clear(); 
    this.notifyDataSetChanged(); 
} 
+0

clearlistview()でnotifyDataSetChanged()を追加しました。しかし、mga.clearlistview();でエラーを表示しているlogcat。私のコードは正しいですか?コンストラクタでnotifyDataSetChanged()を呼び出してみました。しかし、私の活動は空のリストビューを表示しています。可能であれば、サンプルコードを提供していただけますか? –

+0

本当に新しい 'ArrayList'を作成し、項目を追加してからすべての項目をクリアする必要がありますか?また、 'ListView'で' setAdapter() 'を呼び出した後に' notifyDataSetChanged() 'を呼び出す必要があります。 – wsanville

+0

ええ、現在のアクティビティから新しいアクティビティを呼び出す必要があります。新しいアクティビティにデータベース操作があります。そして、操作を終えた後、古いアクティビティに戻って、古いアクティビティのリストビューをデータベース操作で更新する必要があります。onResume()に古いアクティビティのコードを書き込んでリストビューを更新します(notifyDataSetChanged ()はリストビューのデータベース操作を更新しません)。しかし、リストビューのデータは重複しています。だから私はすべての項目をクリアし、更新された項目を追加する必要があります。 –

0

(あなたが "のGList"、すなわち、glist.clearをクリアする必要がありますが、あなたはmga.clearlistviewを呼び出すことはありませshuld)。

glistはアイテムの元のarraylistですが、 "アイテム"は唯一のglistのコピーです。

カスタムアダプターの詳細については、このリンクを参照してください。

LINK1

+0

私は何を言いたいのですか? –

+0

mga.clearlistview()を削除しました。 glist.clear();を追加しました。しかし、これは何の違いもありません。活動に戻ると、データは複製されています。問題は、grplist_adapterクラスでは、items.clear()をどこに追加するのかわかりません。およびnotifyDataSetChanged();アダプタ内の項目をクリアします。 –

+0

リンクが役立つかどうか –

0

は、私の知識に従ってアダプタをクリアし、アダプタにnotifyDataSetChangedを()に設定する必要はありません。 あなたはリスト、ArrayListを使用していますアイテムまたはアレイのためにそれをクリアして、それを再初期化します。そして、衣装アダプターにデータを渡した後、使用された配列/リストをクリアします。

2

単にlistView.setAdapter(null)を使用してください。

この作品は私のためのものです。

+0

私はそれを試みましたが、うまくいきませんでした。 – Taiti

+0

それは私のために働いた.. –

関連する問題