2012-03-13 154 views
0

爲什麼我的代碼不工作?我已經嘗試使用notifyDataSetChanged(),或使用無效()爲我的列表視圖,或使用.setAdapter()再次爲我的適配器,但它仍然無法正常工作,請幫助我。 公共類ListDel延伸活動{爲什麼notifyDataSetChanged()不能在我的代碼中工作?

ListView mylist_del; 
ListViewAdapter listViewAdapter_del; 
TypedArray images_del; 
ImageView bin_image; 

private static String[] names, ages; 
storageListImplementation lists = new storageListImplementation(); 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.del_layout); 

    names = lists.getNames(); 
    ages = lists.getAges(); 
    images_del = getResources().obtainTypedArray(R.array.icons); 
    bin_image = (ImageView)findViewById(R.id.del_image_icon); 

    listViewAdapter_del = new ListViewAdapter(this); 
    mylist_del = (ListView)findViewById(R.id.listview_delete); 
    mylist_del.setAdapter(listViewAdapter_del); 

} 

private class ListViewAdapter extends BaseAdapter{ 
    private LayoutInflater mInflater; 

    public ListViewAdapter(Context context){ 
     mInflater = LayoutInflater.from(context); 
    } 

    public int getCount() { 
     return names.length; 
    } 

    public Object getItem(int pos) { 
     return pos; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    private OnClickListener binClick(ImageView img, final int position, String name) { 

     return new View.OnClickListener(){ 
      public void onClick(View v) { 
       lists.del(names[position]); 
       mylist_del.invalidate(); 
       //mylist_del.setAdapter(listViewAdapter_del); 
       listViewAdapter_del.notifyDataSetInvalidated(); 
       listViewAdapter_del.notifyDataSetChanged();     

       //newlistviewadap(); 
       //mylist_del.setAdapter(new ListViewAdapter(this)); 
       //listViewAdapter_del.notifyDataSetChanged(); 
       //String addSuccessful = "You have successfully deleted "+names[position]+" and his/her information in you database"; 
       //Toast.makeText(getApplicationContext(), addSuccessful, Toast.LENGTH_LONG).show(); 

       //ListDel.this.finish(); 
      } 
     }; 
    } 

    public void newlistviewadap(){ 

     mylist_del.setAdapter(listViewAdapter_del); 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder textView; 
     if(convertView==null){ 
      convertView = mInflater.inflate(R.layout.del_listview_row, null); 
      textView = new ViewHolder(); 
      textView.text = (TextView) convertView.findViewById(R.id.del_values_row); 
      textView.age = (TextView) convertView.findViewById(R.id.del_age_row); 
      textView.image = (ImageView) convertView.findViewById(R.id.del_image); 
      textView.delbutton = (ImageView) convertView.findViewById(R.id.del_image_icon); 
      convertView.setTag(textView); 
     }else{ 
      textView = (ViewHolder) convertView.getTag(); 
     } 

     textView.text.setText(names[position]); 
     textView.age.setText(ages[position]); 

     textView.image.setImageDrawable(images_del.getDrawable(position)); 
     textView.delbutton.setOnClickListener(binClick(textView.delbutton, position, names[position])); 
     return convertView; 
    } 

} 

static class ViewHolder{ 
    TextView text; 
    TextView age; 
    ImageView image; 
    ImageView delbutton; 
} 

}

回答

0

嘗試刪除

listViewAdapter_del.notifyDataSetInvalidated(); 

一旦你叫它,mylist_del將從listViewAdapter_del獲取更多的數據,所以只請保留以下代碼並重試:

lists.del(names[position]); 
listViewAdapter_del.notifyDataSetChanged(); 
+0

謝謝!我已經嘗試過了,但我仍然無法工作......我不明白...請幫助。 – nielletan 2012-03-19 03:52:05

+0

我已經找到了我的問題的答案,其實只是更新數組名稱。然後再次設置陣列適配器。不管怎麼說,還是要謝謝你。 – nielletan 2012-04-01 11:44:16

相關問題