0

I am making an app in which there are posts with comments and description. There are three buttons on each post. 1 description 2 comments and third is like button. I set a button click listener in getview method of custom adapter. When i click on description button the description of that post should be displayed under the button. But when i clicked the description button, the description of some other listview items also displayed. I just want to show the description of that post whose description button is clicked. Here is my code:

getview code:

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

        View view = convertView;
        try
        {
            if (convertView == null) {
                v = new ViewHolder();
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_list_item, parent, false);
                v.imgView = (ImageView) convertView.findViewById(R.id.iv_list_image);
                v.desc = (Button) convertView.findViewById(R.id.btn_desc);
                v.des = (TextView) convertView.findViewById(R.id.tv_list_desc);
                v.ll = (LinearLayout) convertView.findViewById(R.id.ll_desc);


                convertView.setTag(v);
            }
            else {
                v = (ViewHolder) convertView.getTag();
            }
            v.desc.setTag(position);
            //Picasso.with(getContext()).load(a.getFile()).fit().into(v.imgView);
            Glide.with(context).load(a.getFile()).centerCrop().placeholder(R.drawable.dualring).into(v.imgView);

            v.desc.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v1) {
                    Toast.makeText(getContext(), ""+v.desc.getTag(), Toast.LENGTH_SHORT).show();
                    v.des.setText(""+a.getDescription());
                    v.ll.setVisibility(View.VISIBLE);
                }
            });


            return convertView;

        }catch (Exception e)
        {
            return null;
        }

    }

XML code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="35dp">
    <ImageView
        android:id="@+id/iv_list_image"
        android:layout_width="match_parent"
        android:layout_height="300dp" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="6">
        <Button
            android:id="@+id/btn_desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="Description"
            />
        <Button
            android:id="@+id/btn_comment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="Comments"
            />
        <Button
            android:id="@+id/btn_likes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="Likes"
            />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/ll_desc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp"
        android:background="#ffffff"
        android:visibility="invisible"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Description:"
            android:textStyle="bold"/>
        <TextView
            android:id="@+id/tv_list_desc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Default Desc"
            android:textColor="#333"
            />
    </LinearLayout>
</LinearLayout>

2 Answers2

1

That's when you use an int flag for the list item's position as a conditional statement before updating the ListView's items.

So in your case, the Adapter class would look something like this:

...

private int mPosition = -1; // Int flag that doesn't take effect UNTIL it has been set

...

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    a = getItem(position);

    View view = convertView;
    try {
        if (convertView == null) {
            v = new ViewHolder();
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_list_item, parent, false);
            v.imgView = (ImageView) convertView.findViewById(R.id.iv_list_image);
            v.desc = (Button) convertView.findViewById(R.id.btn_desc);
            v.des = (TextView) convertView.findViewById(R.id.tv_list_desc);
            v.ll = (LinearLayout) convertView.findViewById(R.id.ll_desc);

            convertView.setTag(v);
        } else {
            v = (ViewHolder) convertView.getTag();
        }

        v.desc.setTag(position);

        //Picasso.with(getContext()).load(a.getFile()).fit().into(v.imgView);
        Glide.with(context).load(a.getFile()).centerCrop().placeholder(R.drawable.dualring)
                .into(v.imgView);

        // Runs the following functionality if the positions match. Otherwise, hides the layout.
        if (mPosition == position) {
            Toast.makeText(getContext(), ""+v.desc.getTag(), Toast.LENGTH_SHORT).show();
            v.des.setText(""+a.getDescription());
            v.ll.setVisibility(View.VISIBLE);
        } else {
            v.ll.setVisibility(View.INVISIBLE);
        }

        v.desc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v1) {
                mPosition = position; // Sets the flag to the position that was clicked

                notifyDataSetChanged(); // Updates the data instantly
            }
        });

        return convertView;

    } catch (Exception e) {
        return null;
    }
}

Let me know if that works.

DaveNOTDavid
  • 1,753
  • 5
  • 19
  • 37
  • @MuhammadAbdullah I updated the answer. Try that :-) – DaveNOTDavid Jun 16 '17 at 14:51
  • 1
    Thanks a lot. Its working now. You save my day. Thanks again. – Muhammad Abdullah Jun 16 '17 at 16:53
  • @MuhammadAbdullah You're very welcome! Oh, and perhaps you could accept my answer as the best answer then. – DaveNOTDavid Jun 17 '17 at 17:01
  • @MuhammadAbdullah I meant to mark this answer post's checkmark icon green (https://stackoverflow.com/help/accepted-answer) so other people could see this which will potentially help them – DaveNOTDavid Jun 18 '17 at 12:48
  • @MuhammadAbdullah No worries, you're still learning! It's a very useful skill to have here in this Stack Overflow community. Also, we both get reputation points so it's really a win-win situation – DaveNOTDavid Jun 18 '17 at 14:17
0

The best way is to implement the on click listener in adapter and set the tag on button. After that in onclick method perform the desired task through getting tag id of button, this will definitely work.

Harsh Mittal
  • 2,868
  • 1
  • 16
  • 21