如何在 BaseAdapter 中完成 Asynctask 后修改单个列表项以

How to modify a single list item after Asynctask is done in a BaseAdapter to

在我的主要 activity 中,我显示了一个 ListView,它使用自定义 BaseAdapter(ThoughtListAdapter)。

listView = (ListView) findViewById(R.id.list);
adapter = new ThoughtListAdapter(this, resultingThoughts);
listView.setAdapter(adapter);

ListView 中的每个项目都有一个自定义布局,其中包含一个 TextView 和两个 Button

if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.list_item_thought, null);
}

thoughtText = (TextView) convertView.findViewById(R.id.thought_text_view);
likeButton = (Button) convertView.findViewById(R.id.thought_like_button);
dislikeButton = (Button) convertView.findViewById(R.id.thought_dislike_button);

单击 Button 时,将调用 AsyncTask (AsyncPost),它连接到我的数据库并进行一些更改。

likeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            System.out.println("LIKE CLICKED");
            Thought t = thoughtItems.get(position);
            thoughtId = t.getId();
            opinion = 1;

            AsyncPost asyncPost = new AsyncPost(activity,ThoughtListAdapter.this);
            asyncPost.execute(SHARE_THOUGHT_URL,
                    TAG_PERSON_EMAIL, "m@b.it",
                    TAG_THOUGHT_ID, thoughtId.toString(),
                    TAG_OPINION, opinion.toString());
        }
    });

我需要的是在 AsyncTask 完成并获得成功结果后,使列表项的两个 Button-s 消失。我有一个方法 onComplete(JSONObject json),它详细说明了 AsyncTask 返回的 JSONObject。我尝试使 onComplete 方法中的按钮不可见,但这不起作用,因为 onComplete() 不知道具体单击了哪个按钮。 我怎样才能在 onComplete() 中传递精确点击按钮的实例,并使相关列表项的“喜欢”和“不喜欢”按钮消失?

AsyncPost 是我所有其他活动使用的全局 AsyncTask。我强烈希望不要管它。 onComplete() 方法的作用相当于 AsyncTaskonPostExecute() 方法。 这是我的 BaseAdapter 中的 getView()onComplete() 方法,其中包含上面显示的所有代码。 谢谢。

public View getView(final int position, View convertView, ViewGroup parent) {
    if (layoutInflater == null) {
        layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.list_item_thought, null);
    }


    thoughtText = (TextView) convertView.findViewById(R.id.thought_text_view);
    likeButton = (Button) convertView.findViewById(R.id.thought_like_button);
    dislikeButton = (Button) convertView.findViewById(R.id.thought_dislike_button);

    //thoughtItems is a list of custom ojbects (Thought)
    Thought t = thoughtItems.get(position);

    //Here i set the content of the current TextView
    thoughtText.setText(t.getText());

    //the two buttons do basically the same thing when get clicked
    likeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Thought t = thoughtItems.get(position);
            thoughtId = t.getId();
            opinion = 1;

            AsyncPost asyncPost = new AsyncPost(activity,ThoughtListAdapter.this);
            asyncPost.execute(SHARE_THOUGHT_URL,
                    TAG_PERSON_EMAIL, "m@b.it",
                    TAG_THOUGHT_ID, thoughtId.toString(),
                    TAG_OPINION, opinion.toString());
        }
    });

    dislikeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Thought t = thoughtItems.get(position);
            thoughtId = t.getId();
            opinion = 0;

            AsyncPost asyncPost = new AsyncPost(activity,ThoughtListAdapter.this);
            asyncPost.execute(SHARE_THOUGHT_URL,
                                TAG_PERSON_EMAIL, "m@b.it",
                                TAG_THOUGHT_ID, thoughtId.toString(),
                                TAG_OPINION, opinion.toString());
        }
    });

    return convertView;
}

@Override
public void onComplete(JSONObject json) {
    if (json != null) {
        try {
            if (json.getInt(TAG_SUCCESS) == 0) {
                Toast.makeText(activity, "Operazione non riuscita.", Toast.LENGTH_LONG).show();
            } else {
                //if everything is good i try to make the buttons of that particular list item disappear
                likeButton.setVisibility(View.GONE);
                dislikeButton.setVisibility(View.GONE);
            }
        }
        catch (JSONException e) {
            Log.e(TAG_LOG, "JSONException", e);
        }
    }
    else Toast.makeText(activity, "Errore connessione!", Toast.LENGTH_LONG).show();
}

一个解决方案是在您的 Thought 对象上添加一些东西来指示是否显示按钮。

所以在你的getView()方法中你检查这个

likeButton = (Button) convertView.findViewById(R.id.thought_like_button);
dislikeButton = (Button) convertView.findViewById(R.id.thought_dislike_button);

Thought t = thoughtItems.get(position);
if (t.hideButtons()) {
    likeButton.setVisibility(View.GONE);
    dislikeButton.setVisibility(View.GONE); 
}
else {
    likeButton.setVisibility(View.VISIBLE);
    dislikeButton.setVisibility(View.VISIBLE); 
}

然后你需要让你的 onComplete 方法 return 与它相关的 Thought 对象的 id。然后在你的 onComplete 里面你可以做

int id = //get your id from your JSON response
for(Thought t : thoughtItems) {
    if (t.getId() == id) {
        t.setHideButtons(true);
        notifyDataSetChanged();
        break;
    }
}

通过调用 notifyDataSetChanged(),它会重新绘制您的列表,当它检查是否应该显示按钮时,它不会显示它们,因为它是在那个想法项目上设置的