0

Consider am having list of items in my android listview.

if i have to change the value on my 3rd item means it's getting the last item value.

What's wrong in my code ? please give me a solution .

i have used belo code in my adapter file :

    public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;

    if(convertView==null) {
        vi = inflater.inflate(R.layout.list_profile_griditem, null);
        followphotobtn = (Button)vi.findViewById(R.id.followphoto);


 HashMap<String, String> Order = new HashMap<String, String>();
    Order = data.get(position);
    final String photofollowstatus = Order.get(Profile.TAG_PHOTO_FOLLOW_STATUS);
     if(photofollowstatus.equalsIgnoreCase("Following")){
        followphotobtn.setText("UnFollow");
            System.out.println("FollowBtn"+" "+"UnFollow");
     }
     else {
        followphotobtn.setText("Follow");
                   System.out.println("FollowBtn"+" "+"Follow");
        }

it is working well..here am getting the output correctly.

02-03 19:44:57.276: I/System.out(19871): FollowBtn UnFollow
02-03 19:44:57.276: I/System.out(19871): FollowBtn UnFollow
02-03 19:44:57.284: I/System.out(19871): FollowBtn UnFollow
02-03 19:44:57.292: I/System.out(19871): FollowBtn Follow

But i didn't get the correct result in onclicklister :

  followphotobtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {


             if(photofollowstatus.equals("Following")){
                 new UnFollowPhoto().execute(photo_id);

            }
            else {

                 new FollowingPhoto().execute(photo_id);


                 }
                    }
        });
        });
    return vi;
 }

  class FollowingPhoto extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //showDialog(DIALOG_LOADING);
    }
    protected String doInBackground(String... args) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://dev.xxx.com/xxx/client/follow.php");
        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            System.out.println("photo_id"+" "+args[0]);
            nameValuePairs.add(new BasicNameValuePair("userid", LoginPage.userid));
            nameValuePairs.add(new BasicNameValuePair("photo_id", args[0]));
            nameValuePairs.add(new BasicNameValuePair("photofollow_status", "Following"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            result_status = EntityUtils.toString(entity);
            //res = response.toString();
            } catch (ClientProtocolException e) {
            } catch (IOException e) {
            }
            return result_status;

            }


    protected void onPostExecute(String file_url) {
        super.onPostExecute(file_url);
        //dismissDialog(DIALOG_LOADING);
        Toast.makeText(activity.getApplicationContext(), "Following", Toast.LENGTH_LONG).show();
        followphotobtn.setText("UnFollow");
        Profile.myphotolistAdapter.notifyDataSetChanged();
        followphotobtn.setText("UnFollow");
        System.out.println(followphotobtn.getText().toString());
        }
}

But i am getting the output is :

  02-03 19:45:10.534: I/System.out(19871):  UnFollow

But this value is not update in android adapter file while clicking this button..

But i have wrote the code like below means :

protected void onPostExecute(String file_url) {
        super.onPostExecute(file_url);
        //dismissDialog(DIALOG_LOADING);
        Toast.makeText(activity.getApplicationContext(), "Following", Toast.LENGTH_SHORT).show();
        followprofilephotobtn.setText("UnFollow");
        Intent intent = new Intent(activity,Profile.class);
        activity.startActivity(intent);
        }

The value is changed once reloaded the activity . But i need to update the value without reload the activity.

What's wrong in my code ? why i have facing this issue ?

Please give me a idea and suggestion ?

user2218667
  • 607
  • 6
  • 25
  • 46
  • is it a proper decleration?: followphotobtn = (Button)vi.findViewById(R.id.followphoto); – Umit Kaya Feb 03 '14 at 13:24
  • data.get(position) what is this?and what's come in data. – Harshid Feb 06 '14 at 04:36
  • i got your solution please give response. – Harshid Feb 06 '14 at 04:38
  • @Harshid position of the selected list item. – user2218667 Feb 06 '14 at 08:48
  • Sorry if its late entry but i have confusion - first at adapter side u were setting the followphotobtn button text which u get from R.id.followphoto id and in followPhotobtn.onclicklistner u were calling the async task and at onPostexcute u setting text of followprofilephotobtn ..so where this come from? – Neha Feb 12 '14 at 08:56
  • 1
    I think you're missing a `{` after `if(convertView==null)`. – thomasg Feb 12 '14 at 13:50
  • @Neha Please check my updated code and give me a idea to update the adapter file button value on runtime. Still now am facing the same issue . – user2218667 Feb 13 '14 at 08:34
  • @user2218667 how u accessing the followphotobtn in asynctask onPostexcute () ? and if u getting that value in if(convertView==null) { } then how its accessible in that async class? – Neha Feb 13 '14 at 09:00

2 Answers2

0

You have to bind the button click listener inside adapter class in this way

public View getView(int position, View convertView, ViewGroup parent){
        View view = super.getView(position, convertView, parent);
        Button button = (Button)view.findViewById(R.id.followphoto);
        button.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                Log.i(TAG, "clicked");
            }
        }); 
        return view;
 }
Mukesh Y
  • 762
  • 6
  • 16
0

Set a tag to your button in the getView method

followphotobtn.setTag(position);

and then as @Mukesh said in onClickListener use getTag to get the state of item that was clicked. See below similar link here.

Community
  • 1
  • 1
Atul O Holic
  • 6,692
  • 4
  • 39
  • 74