这是Adapter类中的getChildView ...
public View getChildView(int parent, int child, boolean lastChild, View view, ViewGroup viewGroup) {
ArrayList<String> itemDetails= (ArrayList<String>)getChild(parent,child);
if(view==null)
{
LayoutInflater inflater= (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view= inflater.inflate(R.layout.child_layout,viewGroup,false);
}
TextView textView =(TextView)view.findViewById(R.id.textView2);
textView.setText(itemDetails.get(0));
TextView textView1(TextView)view.findViewById(R.id.itemdescription);
textView1.setText(itemDetails.get(1));
TextView textView3= (TextView)view.findViewById(R.id.price);
textView3.setText(itemDetails.get(2));
final TextView textView2 = (TextView)view.findViewById(R.id.counterTextView);
ImageButton add = (ImageButton)view.findViewById(R.id.imageButton);
final ImageButton remove = (ImageButton)view.findViewById(R.id.imageButton1);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textView2.setText(String.valueOf(Integer.parseInt(textView2.getText().toString())+1));
remove.setVisibility(View.VISIBLE);
}
});
remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(Integer.parseInt(textView2.getText().toString())==0)
{
remove.setVisibility(View.INVISIBLE);
}
else {
textView2.setText(String.valueOf(Integer.parseInt(textView2.getText().toString()) - 1));
}
}
});
return view;
}
这是ChildLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:background="#FFFFFF">
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="centerCrop"
app:srcCompat="@drawable/k_meals_rs"
android:id="@+id/imageView6" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textSize="20sp"
android:textColor="#000000"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingRight="10dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_remove_black"
android:id="@+id/imageButton1"
android:background="@drawable/round_button"
android:visibility="invisible"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textColor="#000000"
android:id="@+id/counterTextView" />
<ImageButton
app:srcCompat="@drawable/ic_add_black"
android:id="@+id/imageButton"
android:background="@drawable/round_button"
android:layout_width="29dp"
android:layout_height="29dp" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="10dp">
<TextView
android:id="@+id/itemdescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:maxEms="15"
android:textColor="#000000"/>
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="₹150"
android:textColor="#000000"/>
</RelativeLayout>
</LinearLayout>
我想增加&amp;当按下+和 - 按钮时递减counterTextView的值..但是当我这样做时,它会递增并递减其他父级中具有相同索引值的其他子级中的counterTextView的值...任何想法如何克服此问题。 .thnx