0

I am implementing a "like" system in a ListView. Each list item contains a video, the number of likes, the video's id, and the "like" button.

The video_id displays correctly(different for each video), but no matter which "like" button I click, it always sends the video_id from the first item in the list.

To summarize, it seems that my 'Like' method does not know which button is being pressed, and it's automatically choosing the first one and sending that video_id.

All help appreciated!

When the "like" button is clicked, it calls the Like method:

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

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

Here is the Like method:

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

}
pkelly
  • 203
  • 2
  • 3
  • 15
  • You say, "The video_id displays correctly", have you debugged to check that you get the correct `video_id ` in `doInBackground()`? Or, where does it "display correctly" at? – codeMagic May 28 '14 at 22:38
  • It displays correctly in the ListView beside the corresponding video. I haven't done debugging, all I know is that in 'doInbackground()', 'findViewById(R.id.video_id)' just picks the first video_id in the list. – pkelly May 28 '14 at 22:51
  • You need to pass that id somehow such as through a constructor of the `AsyncTask` or set it as a member variable if it's an inner class – codeMagic May 28 '14 at 22:52
  • Thanks for your help! After some looking around/trial and error I found this SO post which helped me out a lot. - http://stackoverflow.com/questions/17549042/android-asynctask-passing-a-single-string – pkelly May 29 '14 at 02:08

1 Answers1

1

Your holder object needs to have a reference to the Video Text View for that row. Then when the button is clicked, you need to get the text from that text view and pass it to your AsyncTask. Like so:

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

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

Your AsyncTask would then look like this:

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();
}
mweathers
  • 941
  • 8
  • 11