2014-02-28 75 views
0

我有一個自定義列表視圖和活動。我從listview自定義調用事件imageclick並調用活動。但adapter.notifyDataSetChanged();不工作。爲什麼mylistview在更改狀態後不能重新加載?

package com.olsvn.studyapp; 

    import java.util.ArrayList; 

    import android.app.Activity; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.view.ViewGroup; 
    import android.widget.ArrayAdapter; 
    import android.widget.ImageView; 
    import android.widget.TextView; 
    import com.olsvn.studyapp.HomeActivity; 

    import com.olsvn.studyapp.dao.Word; 

    public class WordArrayAdapter extends ArrayAdapter<Word> implements 
      OnClickListener { 

     Activity context = null; 
     ArrayList<Word> myArray = null; 
     int layoutId; 

     public WordArrayAdapter(Activity context, int layoutId, ArrayList<Word> arr) { 
      super(context, layoutId, arr); 
      this.context = context; 
      this.layoutId = layoutId; 
      this.myArray = arr; 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
      LayoutInflater inflater = context.getLayoutInflater(); 
      convertView = inflater.inflate(layoutId, null); 

      if (myArray.size() > 0 && position >= 0) { 

       final Word words = myArray.get(position); 
       final TextView txtcontent = (TextView) convertView 
         .findViewById(R.id.txtcontent); 
       final TextView txtspell1 = (TextView) convertView 
         .findViewById(R.id.txtspell1); 
       final TextView txtmeaning = (TextView) convertView 
         .findViewById(R.id.txtmeaning); 

       txtcontent.setText(words.getContent()); 
       txtspell1.setText(words.getSpell1()); 
       txtmeaning.setText(words.getMeaning()); 

       final ImageView imgcheck = (ImageView) convertView 
         .findViewById(R.id.imgcheck); 

       if (words.getStatus() == 0) { 
        imgcheck.setImageResource(R.drawable.start); 
       } else if (words.getStatus() == 1) { 
        imgcheck.setImageResource(R.drawable.start_50); 
       } else { 
        imgcheck.setImageResource(R.drawable.start_100); 
       } 
       imgcheck.setOnClickListener(new OnItemClickListener(position)); 

      } 

      return convertView; 
     } 

     private class OnItemClickListener implements OnClickListener { 
      private int mPosition; 

      OnItemClickListener(int position) { 
       mPosition = position; 
      } 

      @Override 
      public void onClick(View arg0) { 
       Word word_item = (Word) myArray.get(mPosition); 
       int item_id = word_item.getId(); 
       int item_status = word_item.getStatus(); 
       HomeActivity sct = new HomeActivity(); 
       sct.setContext(context); 
       sct.CheckStatus(item_status, item_id,mPosition); 
      } 
     } 

     @Override 
     public Word getItem(int position) { 
      return super.getItem(position); 
     } 

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

     @Override 
     public int getCount() { 
      // TODO Auto-generated method stub 
      return super.getCount(); 
     } 

     public WordArrayAdapter get(int mPosition) { 
      return null; 
     } 

     @Override 
     public void onClick(View arg0) { 
     } 

    } 

和一個活動

package com.olsvn.studyapp; 

import java.util.ArrayList; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.AbsListView; 
import android.widget.AbsListView.OnScrollListener; 
import android.widget.ListView; 
import android.widget.Toast; 

import com.olsvn.studyapp.dao.DatabaseHandler; 
import com.olsvn.studyapp.dao.Word; 

public class HomeActivity extends Activity { 

    Activity context; 
    ArrayList<Word> arrWords = new ArrayList<Word>(); 
    WordArrayAdapter adapter; 
    ListView lvWords; 
    boolean CheckUpdate = false; 

    DatabaseHandler db; 

    public void setContext(Activity context) { 
     this.context = context; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 
     getTopRecord(); 
     lvWords.setOnScrollListener(new OnScrollListener() { 

      public void onScrollStateChanged(AbsListView view, int scrollState) { 


      } 

      public void onScroll(AbsListView view, int firstVisibleItem, 
        int visibleItemCount, int totalItemCount) { 

       if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0) 
       { 
        boolean flag_loading = false; 
        if(flag_loading == false) 
        { 
         flag_loading = true; 
        } 
       } 
      } 
     }); 
    } 

    public void CheckStatus(int status, int id,int position) { 
     db = new DatabaseHandler(context); 
     int statusNew = status + 1; 
     if (status != 2) { 
      CheckUpdate = db.updateStatus(id, statusNew); 
      adapter.notifyDataSetChanged(); 
     } 

     if (CheckUpdate = true) { 
      Toast.makeText(context, "update trạng thái thành công", 
        Toast.LENGTH_SHORT).show(); 

     } else { 
      Toast.makeText(context, "update trạng thái không thành công", 
        Toast.LENGTH_SHORT).show(); 
     } 

    } 

    //get list word 
    public void getTopRecord() { 
     String where ="<2"; 
     db = new DatabaseHandler(getApplicationContext()); 
     arrWords = db.getByStatus(20, where); 
     lvWords = (ListView) findViewById(R.id.lvWords); 
     adapter = new WordArrayAdapter(this, R.layout.layout_tu_vung, arrWords); 
     lvWords.setAdapter(adapter); 
     adapter.notifyDataSetChanged(); 
    } 

爲什麼adapter.notifyDataSetChanged();在CheckStatus();不叫myadapter,而不是重新加載

列表錯誤(如果我編輯到...)

CheckUpdate = db.updateStatus(id, statusNew); 
arrWords.get(position).setStatus(statusNew); 
adapter.notifyDataSetChanged(); 

02-28 13:26:54.788:E/AndroidRuntime(9355):致命異常:主要 02 -28 13:26:54.788:E/AndroidRuntime(9355):java.lang.IndexOutOfBoundsException:無效索引3,大小爲0 02-28 13:26:54.788:E/AndroidRuntime(9355):在java.util中。 ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) 02-28 13:26:54.788:E/AndroidRuntime(9355):在java.util.ArrayList.get(ArrayList.java:304) 02-28 13點26: 54.788:E/AndroidRuntime(9355):在com.olsvn.studyapp.HomeActivity.CheckStat我們(HomeActivity.java:41) 02-28 13:26:54.788:E/AndroidRuntime(9355):在com.olsvn.studyapp.WordArrayAdapter $ OnItemClickListener.onClick(WordArrayAdapter.java:81) 02-28 13: 26:54.788:E/AndroidRuntime(9355):at android.view.View.performClick(View.java:4114) 02-28 13:26:54.788:E/AndroidRuntime(9355):at android.view.View $ PerformClick.run(View.java:17097) 02-28 13:26:54.788:E/AndroidRuntime(9355):在android.os.Handler.handleCallback(Handler.java:615)

回答

0

更改爲:

public void CheckStatus(int status, int id,int position) { 
     db = new DatabaseHandler(context); 
     int statusNew = status + 1; 
     if (status != 2) { 
      CheckUpdate = db.updateStatus(id, statusNew); 
      arrWords.get(position).setStatus(statusNew); 
      adapter.notifyDataSetChanged(); 
     } 

     if (CheckUpdate = true) { 
      Toast.makeText(context, "update trạng thái thành công", 
        Toast.LENGTH_SHORT).show(); 

     } else { 
      Toast.makeText(context, "update trạng thái không thành công", 
        Toast.LENGTH_SHORT).show(); 
     } 

    } 

您需要更改列表中單詞的狀態以及

+0

adapter.notifyDataSetChanged();不工作和錯誤 –

+0

是什麼錯誤?你可以發佈你的logcat –

+0

我有添加列表錯誤。謝謝! –

相關問題