0

我有一个 ListActivity 类。它有一个按钮,可以根据其中一个属性对项目进行排序。但是碰巧该项目按其名称排序,但单击其原始位置的项目时将被执行。这是代码片段:

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

        mButton = (ToggleButton) findViewById(R.id.mButton);

        ArrayList<myObject> ListData = new ArrayList<myObject>();

        back = (ImageButton) findViewById(R.id.backButton);
        label = (TextView) findViewById(R.id.likethis);
        label.setText("List");
        inputSearch = (EditText) findViewById(R.id.inputSearch);

        //Manager -- class that reads from the sdcard
        Manager plm = new Manager();

        // get all files from sdcard
        //getList() function of Manager class
        //List declared outside oncreate()
        this.List = plm.getList(); 

        final ArrayList<myObject> backUp = new ArrayList<myObject>(songsList);

        if(sortCalled) {
            mButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.m_checked));
            mButton.setChecked(true);
            Collections.sort(List);

            notifyDataChanged(List);
        }
        else {
            mButton.setBackground(getResources().getDrawable(R.drawable.m_unchecked));
            mButton.setChecked(false);
            notifyDataChanged(backUp);
        }

        back.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                finish();
            }

        });

        inputSearch.addTextChangedListener(new TextWatcher(){

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
                adapter.notifyDataSetChanged();
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override 
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub
                //ArrayList <Song> temp;
                ((SimpleAdapter) PlayListActivity.this.adapter).getFilter().filter(s, new Filter.FilterListener() {

                    public void onFilterComplete(int count) {
                        // TODO Auto-generated method stub
                        adapter.notifyDataSetChanged();
                    }
                });
                adapter.notifyDataSetChanged();


            }

        });

        mButton.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(mButton.isChecked()){
                    mButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.m_checked));
                    Collections.sort(List);
                    sortCalled = true;
                    notifyDataChanged(List);
                }
                else{
                    sortCalled = false;
                    mButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.m_unchecked));
                    notifyDataChanged(backUp);
                }
            }

        });
    }

    public void settingListAdapter(SimpleAdapter adapter){
        setListAdapter(adapter);
        ListView lv = getListView();
        // listening to single listitem click
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting listitem index
                int Index = position;

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        MainActivity.class);
                // Sending Index to MainActivity
                in.putExtra("Index", Index);
                setResult(100, in);
                // Closing ListView
                finish();
            }
        });
    }

    public void notifyDataChanged(ArrayList<Song> songsList) {
        // TODO Auto-generated method stub
        setListAdapter(null);
        sortCalled = true;
        ArrayList<myObject> songsListData = new ArrayList<myObject>();

        for (int i = 0; i < songsList.size(); i++) {
            // creating new HashMap
            myObject ob = sList.get(i);

            // adding HashList to ArrayList
            ListData.add(ob);
        }

        ArrayList<HashMap<String, String>> array = new ArrayList<HashMap<String,String>>();
       // int j = 0;
        for(myObject i : ListData){
            HashMap<String, String> feed = new HashMap<String, String>();
            feed.put("Title", i.getTitle());
            array.add(feed);

        }
        BaseAdapter adapter1;
       // int i = 0;
        // Adding menuItems to ListView
         adapter1 = new SimpleAdapter(this, array,
                R.layout.list_item, new String[]{"Title" }, new int[] {
                        R.id.Title });

        setListAdapter(adapter1);
        adapter1.notifyDataSetChanged();

        ListView lv = getListView();
        // listening to single listitem click

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting listitem index
                int Index = position;

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        MainActivity.class);
                // Sending songIndex to MainActivity
                in.putExtra("Index", Index);
                setResult(100, in);
                // Closing ListView
                finish();
            }
        });
4

3 回答 3

2

我认为你应该试试这个。当你对列表视图的数据进行排序时。它对我来说很完美

 adapter.notifyDataSetChanged();
于 2012-10-23T04:38:45.363 回答
1

每次对适配器进行更改时,都应该在 UIThread 中调用 notifyDataSetChanged()

请查看此视频以更好地了解 listView

编辑
对于 ArrayAdapter,notifyDataSetChanged 仅在您使用add, insert, remove, and clearAdapter 上的函数时才有效。

构造 ArrayAdapter 时,它保存对传入的 List 的引用。如果您要传入一个作为 Activity 成员的 List,然后更改该 Activity 成员,则 ArrayAdapter 仍然持有对原名单。Adapter 不知道你更改了 Activity 中的 List。

您的选择是:

使用 ArrayAdapter 的功能修改底层 List(add, insert, remove, clear, etc.) 用新的 List 数据重新创建 ArrayAdapter。(使用大量资源和垃圾收集。)创建您自己的从 BaseAdapter 和 ListAdapter 派生的类,允许更改基础 List 数据结构。每次更新列表时使用 notifyDataSetChanged。要在 UI 线程上调用它,请使用 Activity 的 runOnUiThread 方法。然后 notifyDataSetChanged 将起作用。

于 2012-10-29T15:39:49.777 回答
0

You could try the Adapter.notifyDataSetChanged() function. It might work.

If you are using a SimpleCursorAdapter, calling requery() on the cursor attached to the adapter will automatically refresh the adapter and attached view.

If it's BaseAdapter content changes, you call BaseAdapter.notifyDataSetChanged() which will tell the ListView to update itself.

In other words you only need to make the following tiny change:

public void setValue(Object newValue) {
     this.value = newValue;
     notifyDataSetChanged();
}

Actually, since you're changing the state of an existing item (rather than adding new ones etc) then notifyDataSetInvalidated() would be a better choice.

于 2012-10-23T05:29:21.967 回答