ListView中的按钮始终从第一个项目发送数据

时间:2014-05-28 22:31:57

标签: android android-listview

我在ListView中实现了一个“喜欢”的系统。每个列表项都包含一个视频,喜欢的数量,视频的ID和“喜欢”按钮。

video_id 正确显示(每个视频都不同),但无论我点击哪个“喜欢”按钮,它总是从第一个项目中发送 video_id 列表。

总结一下,似乎我的“赞”方法不知道按下了哪个按钮,它会自动选择第一个并发送 video_id < / p>

所有帮助表示赞赏!

单击“喜欢”按钮时,它会调用Like方法:

holder.btnLike.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
new Like().execute();
}
});

这是Like方法:

class Like extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(ThreadActivity.this);
        pDialog.setMessage("Creating Product..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }


    Bundle extras = getIntent().getExtras();

    protected String doInBackground(String... args) {

        String price = fbID;

        TextView videoid = (TextView)findViewById(R.id.video_id);

        String video_id = videoid.getText().toString();

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("price", price));
        params.add(new BasicNameValuePair("description", video_id));

        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_add_like,
                "POST", params);

        // check log cat for response
        Log.d("Create Response", json.toString());

        // check for success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully created product
                Intent i = new Intent(getApplicationContext(), ThreadActivity.class);
                i.putExtra(TAG_PID, pid);
                i.putExtra("EXTRA_FACEBOOK_ID",fbID);
                startActivity(i);

                // closing this screen
                finish();
            } else {
                // failed to create product
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once done
        pDialog.dismiss();
    }

}

1 个答案:

答案 0 :(得分:1)

您的持有者对象需要引用该行的视频文本视图。然后,当单击该按钮时,您需要从该文本视图中获取文本并将其传递给AsyncTask。像这样:

holder.btnLike.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        new Like().execute(holder.videoTextView.getText().toString());
    }
});

您的AsyncTask将如下所示:

class Like extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
    super.onPreExecute();
    pDialog = new ProgressDialog(ThreadActivity.this);
    pDialog.setMessage("Creating Product..");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(true);
    pDialog.show();
}


Bundle extras = getIntent().getExtras();

protected String doInBackground(String... args) {

    String price = fbID;

    String video_id = args[0]; 

    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("price", price));
    params.add(new BasicNameValuePair("description", video_id));

    // getting JSON Object
    // Note that create product url accepts POST method
    JSONObject json = jsonParser.makeHttpRequest(url_add_like,
            "POST", params);

    // check log cat for response
    Log.d("Create Response", json.toString());

    // check for success tag
    try {
        int success = json.getInt(TAG_SUCCESS);

        if (success == 1) {
            // successfully created product
            Intent i = new Intent(getApplicationContext(), ThreadActivity.class);
            i.putExtra(TAG_PID, pid);
            i.putExtra("EXTRA_FACEBOOK_ID",fbID);
            startActivity(i);

            // closing this screen
            finish();
        } else {
            // failed to create product
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return null;
}

/**
 * After completing background task Dismiss the progress dialog
 * **/
protected void onPostExecute(String file_url) {
    // dismiss the dialog once done
    pDialog.dismiss();
}