在BaseAdapter中更新适配器

时间:2015-11-27 18:15:37

标签: android android-arrayadapter baseadapter notifydatasetchanged

当我按"投票"我试图更新我的适配器我的getView()EntertainerAdapter方法内定义的按钮,其范围为BaseAdapter

所以这是我的例子,首先是我的EntertainerListActivity的片段,当我设置适配器时从Parse.com重新获得一些值:

// some code to retrive my objects from Parse.com
for (ParseObject ent : entertainers) {
String name = ent.getString("name");
int vote = ent.getInt("vote");
aList.add(new Entertainer(name, vote));}
EntertainerAdapter adapter = new EntertainerAdapter(EntertainerListActivity.this, aList);
list.setAdapter(adapter);

而不是我的EntertainerAdapter,我只是报告getView()的简化版本,以强调我的问题:

        vote.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            ParseQuery<ParseObject> query = ParseQuery.getQuery("Entertainer");
            query.whereEqualTo("name", name.getText());
            query.whereEqualTo("surname", surname.getText());
            query.getFirstInBackground(new GetCallback<ParseObject>() {
                public void done(ParseObject object, ParseException e) {
                    if (object == null) {
                        Log.d("ZIG", "The getFirst request failed.");
                    } else {
                        object.put("vote", object.getInt("vote") + 1);
                        object.saveInBackground();
                        // OK Now i have to refresh my adapter
                        // I tried
                        notifyDataSetChanged(); // with no results

                        // than intent that start again my Activity
                        //Intent i = new Intent(mActivity, EntertainerListActivity.class);
                        //mActivity.startActivity(i);
                        // This works but i have a big issue when i press back button
                    }
                }
            });
        }
    });

1 个答案:

答案 0 :(得分:1)

问题是,您正在解析数据并调用notifyDataSetChanged();但是您传递给适配器的数据仍然相同,这就是您没有看到任何更改的原因。您还应该更新适配器中的数据。

vote.setOnClickListener(new View.OnClickListener()
{
     public void onClick(View v)
     {
          ParseQuery<ParseObject> query = ParseQuery.getQuery("Entertainer");
          query.whereEqualTo("name", name.getText());
          query.whereEqualTo("surname", surname.getText());
          query.getFirstInBackground(new GetCallback<ParseObject>()
          {
              public void done(ParseObject object, ParseException e)
              {
                  if (object == null)
                  {
                       Log.d("ZIG", "The getFirst request failed.");
                  }
                  else
                  {
                        object.put("vote", object.getInt("vote") + 1);
                        object.saveInBackground();

                        // Update the local Entertainer object here
                        // entertainerObject.setVote(entertainerObject.getVote() + 1);
                        notifyDataSetChanged();
                  }
               }
          });
     }
});