简体   繁体   English

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

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

In my main activity I display a ListView which uses a custom BaseAdapter (ThoughtListAdapter). 在我的主要活动中,我显示一个使用自定义BaseAdapter (ThoughtListAdapter)的ListView

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

Every item in the ListView has a custom layout containing a TextView and two Button . 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);

When a Button is clicked an AsyncTask (AsyncPost) is called which connects to my database and makes some changes. 单击一个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());
        }
    });

What I need is making both Button -s of a list item disappear after the AsyncTask is done with a successful outcome. 我需要做的是使AsyncTask成功完成后,使列表项的两个Button -s都消失。 I have a method onComplete(JSONObject json) which elaborates the JSONObject returned by the AsyncTask . 我有一个方法onComplete(JSONObject json) ,它详细说明了AsyncTask返回的JSONObject I try to make the buttons non visible inside the onComplete method, but this doesn't work because onComplete() doesn't know which exact button has been clicked. 我尝试使按钮在onComplete方法内部不可见,但这不起作用,因为onComplete()不知道单击了哪个确切的按钮。 How can I pass an instance of the exact clicked button inside onComplete() and make disappear only the Like and Dislike buttons of the concerned list item? 如何传递onComplete()确切单击的按钮的实例,并使仅相关列表项的“喜欢”和“不喜欢”按钮消失?

AsyncPost is a global AsyncTask used by all my other activities. AsyncPost是我所有其他活动使用的全局AsyncTask I would strongly prefer to leave it alone. 我强烈希望不要管它。 The onComplete() method functions as the onPostExecute() method of the AsyncTask . onComplete()方法用作AsyncTaskonPostExecute()方法。 Here are the getView() and onComplete() methods inside my BaseAdapter , which contain all the code shown above. 这是BaseAdapter中的getView()onComplete()方法,其中包含上面显示的所有代码。 Thank you. 谢谢。

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();
}

One solution to this would be to have something on your Thought object to indicate whether or not to show the buttons. 一种解决方案是在Thought对象上放置一些内容,以指示是否显示按钮。

So in your getView() method you check this 因此,请在您的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); 
}

Then you would need to have your onComplete method return the id of the Thought object that it related to. 然后,您需要使onComplete方法返回与之相关的Thought对象的id Then inside your onComplete you could do 然后在您的onComplete内部,您可以执行

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

By calling notifyDataSetChanged() it will redraw your list and when it does the check for whether it should show the buttons or not it will not show them because it was set on that thought item 通过调用notifyDataSetChanged() ,它将重新绘制您的列表,并且当它检查是否应该显示按钮时,将不会显示它们,因为它是在该思考项目上设置的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM