0

我知道有很多类似的问题,但它们都有一个共同点,即他们的解决方案不适用于我的列表:(

当数据库内容发生更改时,我试图通过自定义 ArrayAdapter 让我的 userList 自行刷新。就我而言,当我 reset();

这是我的片段(部分代码):MainActivity.java

public class MainActivity extends ListActivity {
    private MyUserListAdapter myUserlistAdapter; 
    public ArrayList<User> myUserList = new ArrayList<User>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //shortened the list-filling. but it works properly!
        User user = db.readUser(int);
        myUserList.add(user);
        myUserlistAdapter = new MyUserListAdapter(this, R.layout.row_main, myUserList);
        setListAdapter(myUserlistAdapter);

        //now when reset-button is hit, the listview should refresh itself
         bReset.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                 //this is what is posted in most questions, but it does nothing for me
                 //myUserlistAdapter.notifyDataSetChanged();
                 //getListView().invalidateViews();
            } });

这里是 myUserListAdapter.java:

public class MyUserListAdapter extends ArrayAdapter<User>{

private Context context;
private ArrayList<User> userList;

public MyUserListAdapter(Context context,
        int textViewResourceId, ArrayList<User> userList) {
    super(context, textViewResourceId, userList);

    this.context = context;
    this.userList = userList;
}

public View getView(int position, View v, ViewGroup parent) {

    if (v == null) {
        LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = li.inflate(R.layout.row_main, null);
    }

    User user = getItem(position);

    TextView tvUser = (TextView) v.findViewById(R.id.tvUser);
    ImageView ivVoted = (ImageView) v.findViewById(R.id.ivVoted);

    tvUser.setText(user.getName());

    //abfrage ob hasVoted() = true muss noch eingebaut werden.
    if (user.getVoted().equals("1"))
        ivVoted.setImageResource(R.drawable.redcheck);
    else
        ivVoted.setImageResource(R.drawable.greencheck);
    return v;
}   
}

User.java 只是一个简单的对象类。认为它不是这里的麻烦制造者!

任何帮助表示赞赏!谢谢 :-)

4

2 回答 2

3

当数据库内容发生更改时,我试图通过自定义 ArrayAdapter 让我的 userList 自行刷新。

Since you are using ArrayAdapter and not CursorAdapter when you update data in database your adapter won't refresh itself. Whenever you want to update ListView you need to provide new datasource for Adapter.

One possible solution is to create setter in adapter subclass that will change datasource of adapter.

Pseudo code:

/* setter in adapter subclass */
public void changeDataSource(ArrayList<User> newUserList) {
   this.userList = newUserList;
}

Then call adapter.notifyDataSetChanged(); for ListView update.

于 2013-03-20T15:33:14.633 回答
1

Try this:

arrayAdapter.clear()
for(Object o : objects)
  arrayAdapter.add(o)

clear() and add() call to notifyDataSetChanged() itself.

于 2013-03-20T22:25:07.673 回答