为什么mylistview在更改状态后没有重新加载?

时间:2014-02-28 05:01:44

标签: android android-listview

我有自定义列表视图和活动。我从listview自定义调用事件imageclick并调用活动。但是adapter.notifyDataSetChanged();不工作。

 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):at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) 02-28 13:26:54.788:E / AndroidRuntime(9355):at java.util.ArrayList.get(ArrayList.java:304) 02-28 13:26:54.788:E / AndroidRuntime(9355):at com.olsvn.studyapp.HomeActivity.CheckStatus(HomeActivity.java:41) 02-28 13:26:54.788:E / AndroidRuntime(9355):at com.olsvn.studyapp.WordArrayAdapter $ OnItemClickListener.onClick(WordArrayAdapter.java:81) 02-28 13:26:54.788:E / AndroidRuntime(9355):在android.view.View.performClick(View.java:4114) 02-28 13:26:54.788:E / AndroidRuntime(9355):在android.view.View $ PerformClick.run(View.java:17097) 02-28 13:26:54.788:E / AndroidRuntime(9355):在android.os.Handler.handleCallback(Handler.java:615)

1 个答案:

答案 0 :(得分: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();
        }

    }

您还需要更改列表中单词的状态