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>