按钮在列表视图中的点击无法正常工作
Button's click in listiview not working properly
在我的列表视图中我设置了两个按钮和一个编辑文本但是它不能正常工作,我的按钮和编辑文本在列表视图中的视图是这样的
Button1 Edittext Button2
在我的编辑文本中,我设置了 1,Button1 用于递减或减号,Button2 用于递增或加号,但问题出在每个列表项中,它无法正常工作,如果我的列表视图中有三个项目,并且如果我在第一项中增加操作在第三项中执行,
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.list_view_addtocart, container, false);
totvalues=(TextView)rootView.findViewById(R.id.totaldisp);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
edtcopupon=(EditText)rootView.findViewById(R.id.edt_coupon);
btn_applycoupon=(Button)rootView.findViewById(R.id.aplycoupoon);
btn_applycoupon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
list=(ListView)rootView.findViewById(R.id.list_addtocart);
plcordr=(Button)rootView.findViewById(R.id.plcordr);
plcordr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Placeorder tf = new Placeorder();
Bundle bundle = new Bundle();
tf.setArguments(bundle);
android.app.FragmentManager fm = getActivity().getFragmentManager();
android.app.FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container_body, tf);
ft.setTransition(android.support.v4.app.FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
});
ArrayList<ContactListItems> contactList = new ArrayList<ContactListItems>();
contactList.clear();
String query = "SELECT * FROM PHONE_CONTACTS ";
Cursor c1 = SqlHandler.selectQuery(query);
if (c1 != null & c1.getCount() != 0) {
if (c1.moveToNext()) {
do {
contactListItems = new ContactListItems();
contactListItems.setSlno(c1.getString(c1
.getColumnIndex("slno")));
contactListItems.setName(c1.getString(c1
.getColumnIndex("name")));
contactListItems.setPhone(c1.getString(c1
.getColumnIndex("phone")));
contactListItems.setSizes(c1.getString(c1
.getColumnIndex("sizes")));
contactListItems.setQtys(c1.getString(c1
.getColumnIndex("qtys")));
contactList.add(contactListItems);
} while (c1.moveToNext());
}
}
c1.close();
contactListAdapter = new ContactListAdapter(getActivity(), contactList);
list.setAdapter(contactListAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
});
return rootView;
}
public class ContactListAdapter extends BaseAdapter {
Context context;
ViewHolder holder;
ArrayList<ContactListItems> contactList;
String dollars = "\u0024";
public ContactListAdapter(Context context, ArrayList<ContactListItems> list) {
this.context = context;
contactList = list;
}
@Override
public int getCount() {
return contactList.size();
}
@Override
public Object getItem(int position) {
return contactList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(getActivity()).inflate(R.layout.list_item_addtocart, null);
holder.propic = (ImageView) convertView.findViewById(R.id.propicaccept);
holder.tvSlNo = (TextView) convertView.findViewById(R.id.txt_size);
holder.tvName = (TextView) convertView.findViewById(R.id.proprice_addtocart);
holder.tvPhone = (TextView) convertView.findViewById(R.id.proname_addtocart);
holder.tvsizes = (TextView) convertView.findViewById(R.id.txt_sizes);
holder._decrease = (Button)convertView.findViewById(R.id.minuss_addtocart);
holder.mBTIncrement = (Button)convertView.findViewById(R.id.plus_addtocart);
holder._value = (EditText)convertView.findViewById(R.id.edt_procount_addtocart);
holder.mBTIncrement.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//increment();
int mValue = Integer.parseInt(holder._value.getText().toString());
mValue++;
quntity++;
tots = pric * quntity;
System.out.println("INCRESE VALUES" + tots);
totvalues.setText(String.valueOf(tots));
holder._value.setText("" + mValue);
}
});
holder._decrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//decrement();
int mValue = Integer.parseInt(holder._value.getText().toString());
mValue--;
quntity--;
tots = pric * quntity;
System.out.println("INCRESE VALUES" + tots);
totvalues.setText(String.valueOf(tots));
if (mValue < 0) {
System.out.println("not valid");
holder._decrease.setEnabled(false);
} else {
holder._value.setText("" + mValue);
}
}
});
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.tvName.setText(dollars+contactListItems.getName());
holder.tvPhone.setText(contactListItems.getPhone());
holder.tvsizes.setText(contactListItems.getSizes());
holder._value.setText(contactListItems.getQtys());
quntity=Float.parseFloat(contactListItems.getQtys());
pric=Float.parseFloat(contactListItems.getName());
tots=pric*quntity;
totvalues.setText(String.valueOf(tots));
System.out.println("QTYSSS>>" + quntity);
System.out.println("PRICCC>>" + pric);
// float aditya=+tots;
System.out.println("TOTTTT>>" + tots);
//aQuery.id(holder.propic).image(listData.get(position).get(TAG_IMAGE),true,true,0,R.drawable.ic_launcher);
return convertView;
}
class ViewHolder{
ImageView propic;
TextView tvSlNo;
TextView tvName;
TextView tvPhone;
Button mBTIncrement;
TextView tvsizes;
Button _decrease;
public EditText _value;
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rectback"
>
<ImageView
android:layout_width="70dp"
android:layout_height="100dp"
android:id="@+id/img_addtocart"
android:src="@drawable/productfirst"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relfirsrt"
android:layout_toRightOf="@+id/img_addtocart"
android:layout_above="@+id/relativeLayout"
android:layout_alignParentTop="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/proname_addtocart"
android:textAppearance="?android:attr/textAppearanceMedium"
android:maxLength="13"
android:textColor="@android:color/black"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt_size"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt_sizes"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/img_addtocart"
android:layout_toRightOf="@+id/img_addtocart"
android:layout_toEndOf="@+id/img_addtocart"
android:id="@+id/relativeLayout">
<Button
android:layout_width="40dp"
android:layout_height="29dp"
android:id="@+id/minuss_addtocart"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="@drawable/minusbox"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/edt_procount_addtocart"
android:layout_toStartOf="@+id/edt_procount_addtocart" />
<EditText
android:layout_width="40dp"
android:layout_height="29dp"
android:id="@+id/edt_procount_addtocart"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="@drawable/inputbox"
android:inputType="number"
android:gravity="center"
android:text="0"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/plus_addtocart"
android:layout_toStartOf="@+id/plus_addtocart" />
<Button
android:layout_width="40dp"
android:layout_height="29dp"
android:id="@+id/plus_addtocart"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="@drawable/addbox"
android:layout_alignParentTop="true"
android:focusable="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/proprice_addtocart"
android:gravity="center_vertical"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</RelativeLayout>
在 if-else 块之外编写您的 onClick
侦听器和标记分配代码。
if (convertView == null) {
}
else {
}
holder.position = position;
holder.mBTIncrement.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//increment();
int mValue = Integer.parseInt(holder._value.getText().toString());
mValue++;
quntity++;
tots = pric * quntity;
System.out.println("INCRESE VALUES" + tots);
totvalues.setText(String.valueOf(tots));
holder._value.setText("" + mValue);
}
});
holder._decrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//decrement();
int mValue = Integer.parseInt(holder._value.getText().toString());
mValue--;
quntity--;
tots = pric * quntity;
System.out.println("INCRESE VALUES" + tots);
totvalues.setText(String.valueOf(tots));
if (mValue < 0) {
System.out.println("not valid");
holder._decrease.setEnabled(false);
} else {
holder._value.setText("" + mValue);
}
}
});
您的逻辑需要基于点击的位置,因为适配器可能会在点击后自行刷新并更改位置;
public class ContactListAdapter extends BaseAdapter {
Context context;
ViewHolder holder;
ArrayList<ContactListItems> contactList;
ArrayList<Integer> values;
String dollars = "\u0024";
public ContactListAdapter(Context context, ArrayList<ContactListItems> list) {
this.context = context;
contactList = list;
values = new ArrayList<>();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
values.add(contactListItems.getQtys());
convertView = LayoutInflater.from(getActivity()).inflate(R.layout.list_item_addtocart, null);
holder.propic = (ImageView) convertView.findViewById(R.id.propicaccept);
holder.tvSlNo = (TextView) convertView.findViewById(R.id.txt_size);
holder.tvName = (TextView) convertView.findViewById(R.id.proprice_addtocart);
holder.tvPhone = (TextView) convertView.findViewById(R.id.proname_addtocart);
holder.tvsizes = (TextView) convertView.findViewById(R.id.txt_sizes);
holder._decrease = (Button)convertView.findViewById(R.id.minuss_addtocart);
holder.mBTIncrement = (Button)convertView.findViewById(R.id.plus_addtocart);
holder._value = (EditText)convertView.findViewById(R.id.edt_procount_addtocart);
holder.mBTIncrement.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = holder.position;
int mValue = Integer.parseInt(holder._value.getText().toString());
mValue++;
quntity++;
tots = pric * quntity;
System.out.println("INCRESE VALUES" + tots);
totvalues.setText(String.valueOf(tots));
values.set(position, mValue);
}
});
holder._decrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = holder.position;
int mValue = Integer.parseInt(holder._value.getText().toString());
mValue--;
quntity--;
tots = pric * quntity;
System.out.println("INCRESE VALUES" + tots);
totvalues.setText(String.valueOf(tots));
values.set(position, mValue);
}
});
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.position = position;
holder.tvName.setText(dollars+contactListItems.getName());
holder.tvPhone.setText(contactListItems.getPhone());
holder.tvsizes.setText(contactListItems.getSizes());
int value = values.get(position);
if (value < 0) {
holder._decrease.setEnabled(false);
} else {
holder._value.setText("" + mValue);
}
quntity=Float.parseFloat(contactListItems.getQtys());
pric=Float.parseFloat(contactListItems.getName());
tots=pric*quntity;
totvalues.setText(String.valueOf(tots));
System.out.println("QTYSSS>>" + quntity);
System.out.println("PRICCC>>" + pric);
// float aditya=+tots;
System.out.println("TOTTTT>>" + tots);
//aQuery.id(holder.propic).image(listData.get(position).get(TAG_IMAGE),true,true,0,R.drawable.ic_launcher);
return convertView;
}
class ViewHolder{
int position;
ImageView propic;
TextView tvSlNo;
TextView tvName;
TextView tvPhone;
Button mBTIncrement;
TextView tvsizes;
Button _decrease;
public EditText _value;
}
}
尝试像下面这样实现适配器
private Context context;
private List<ContactListItems> items;
/**
* @param context
* @param groupList
*/
public TestListAdapter(Context context, List<ContactListItems> items) {
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.li_plus_minus, parent, false);
holder = new ViewHolder();
holder.plus = (Button) convertView.findViewById(R.id.plus);
holder.minus = (Button) convertView.findViewById(R.id.minus);
holder.et = (EditText) convertView.findViewById(R.id.editText);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ContactListItems item = items.get(position);
holder.et.setText(String.valueOf(item.value));
holder.plus.setOnClickListener(new ClickListener(item));
holder.minus.setOnClickListener(new ClickListener(item));
return convertView;
}
class ClickListener implements View.OnClickListener {
private ContactListItems item;
public ClickListener(ContactListItems item) {
this.item = item;
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.minus) {
item.value--;
} else if (v.getId() == R.id.plus) {
item.value++;
}
notifyDataSetChanged();
}
}
class ViewHolder {
Button plus, minus;
EditText et;
}
希望对您有所帮助
在您的自定义视图父布局中添加此行。
android:descendantFocusability="blocksDescendants"
在我的列表视图中我设置了两个按钮和一个编辑文本但是它不能正常工作,我的按钮和编辑文本在列表视图中的视图是这样的
Button1 Edittext Button2
在我的编辑文本中,我设置了 1,Button1 用于递减或减号,Button2 用于递增或加号,但问题出在每个列表项中,它无法正常工作,如果我的列表视图中有三个项目,并且如果我在第一项中增加操作在第三项中执行,
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.list_view_addtocart, container, false);
totvalues=(TextView)rootView.findViewById(R.id.totaldisp);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
edtcopupon=(EditText)rootView.findViewById(R.id.edt_coupon);
btn_applycoupon=(Button)rootView.findViewById(R.id.aplycoupoon);
btn_applycoupon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
list=(ListView)rootView.findViewById(R.id.list_addtocart);
plcordr=(Button)rootView.findViewById(R.id.plcordr);
plcordr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Placeorder tf = new Placeorder();
Bundle bundle = new Bundle();
tf.setArguments(bundle);
android.app.FragmentManager fm = getActivity().getFragmentManager();
android.app.FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container_body, tf);
ft.setTransition(android.support.v4.app.FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
});
ArrayList<ContactListItems> contactList = new ArrayList<ContactListItems>();
contactList.clear();
String query = "SELECT * FROM PHONE_CONTACTS ";
Cursor c1 = SqlHandler.selectQuery(query);
if (c1 != null & c1.getCount() != 0) {
if (c1.moveToNext()) {
do {
contactListItems = new ContactListItems();
contactListItems.setSlno(c1.getString(c1
.getColumnIndex("slno")));
contactListItems.setName(c1.getString(c1
.getColumnIndex("name")));
contactListItems.setPhone(c1.getString(c1
.getColumnIndex("phone")));
contactListItems.setSizes(c1.getString(c1
.getColumnIndex("sizes")));
contactListItems.setQtys(c1.getString(c1
.getColumnIndex("qtys")));
contactList.add(contactListItems);
} while (c1.moveToNext());
}
}
c1.close();
contactListAdapter = new ContactListAdapter(getActivity(), contactList);
list.setAdapter(contactListAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
});
return rootView;
}
public class ContactListAdapter extends BaseAdapter {
Context context;
ViewHolder holder;
ArrayList<ContactListItems> contactList;
String dollars = "\u0024";
public ContactListAdapter(Context context, ArrayList<ContactListItems> list) {
this.context = context;
contactList = list;
}
@Override
public int getCount() {
return contactList.size();
}
@Override
public Object getItem(int position) {
return contactList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(getActivity()).inflate(R.layout.list_item_addtocart, null);
holder.propic = (ImageView) convertView.findViewById(R.id.propicaccept);
holder.tvSlNo = (TextView) convertView.findViewById(R.id.txt_size);
holder.tvName = (TextView) convertView.findViewById(R.id.proprice_addtocart);
holder.tvPhone = (TextView) convertView.findViewById(R.id.proname_addtocart);
holder.tvsizes = (TextView) convertView.findViewById(R.id.txt_sizes);
holder._decrease = (Button)convertView.findViewById(R.id.minuss_addtocart);
holder.mBTIncrement = (Button)convertView.findViewById(R.id.plus_addtocart);
holder._value = (EditText)convertView.findViewById(R.id.edt_procount_addtocart);
holder.mBTIncrement.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//increment();
int mValue = Integer.parseInt(holder._value.getText().toString());
mValue++;
quntity++;
tots = pric * quntity;
System.out.println("INCRESE VALUES" + tots);
totvalues.setText(String.valueOf(tots));
holder._value.setText("" + mValue);
}
});
holder._decrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//decrement();
int mValue = Integer.parseInt(holder._value.getText().toString());
mValue--;
quntity--;
tots = pric * quntity;
System.out.println("INCRESE VALUES" + tots);
totvalues.setText(String.valueOf(tots));
if (mValue < 0) {
System.out.println("not valid");
holder._decrease.setEnabled(false);
} else {
holder._value.setText("" + mValue);
}
}
});
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.tvName.setText(dollars+contactListItems.getName());
holder.tvPhone.setText(contactListItems.getPhone());
holder.tvsizes.setText(contactListItems.getSizes());
holder._value.setText(contactListItems.getQtys());
quntity=Float.parseFloat(contactListItems.getQtys());
pric=Float.parseFloat(contactListItems.getName());
tots=pric*quntity;
totvalues.setText(String.valueOf(tots));
System.out.println("QTYSSS>>" + quntity);
System.out.println("PRICCC>>" + pric);
// float aditya=+tots;
System.out.println("TOTTTT>>" + tots);
//aQuery.id(holder.propic).image(listData.get(position).get(TAG_IMAGE),true,true,0,R.drawable.ic_launcher);
return convertView;
}
class ViewHolder{
ImageView propic;
TextView tvSlNo;
TextView tvName;
TextView tvPhone;
Button mBTIncrement;
TextView tvsizes;
Button _decrease;
public EditText _value;
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rectback"
>
<ImageView
android:layout_width="70dp"
android:layout_height="100dp"
android:id="@+id/img_addtocart"
android:src="@drawable/productfirst"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relfirsrt"
android:layout_toRightOf="@+id/img_addtocart"
android:layout_above="@+id/relativeLayout"
android:layout_alignParentTop="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/proname_addtocart"
android:textAppearance="?android:attr/textAppearanceMedium"
android:maxLength="13"
android:textColor="@android:color/black"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt_size"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt_sizes"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/img_addtocart"
android:layout_toRightOf="@+id/img_addtocart"
android:layout_toEndOf="@+id/img_addtocart"
android:id="@+id/relativeLayout">
<Button
android:layout_width="40dp"
android:layout_height="29dp"
android:id="@+id/minuss_addtocart"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="@drawable/minusbox"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/edt_procount_addtocart"
android:layout_toStartOf="@+id/edt_procount_addtocart" />
<EditText
android:layout_width="40dp"
android:layout_height="29dp"
android:id="@+id/edt_procount_addtocart"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="@drawable/inputbox"
android:inputType="number"
android:gravity="center"
android:text="0"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/plus_addtocart"
android:layout_toStartOf="@+id/plus_addtocart" />
<Button
android:layout_width="40dp"
android:layout_height="29dp"
android:id="@+id/plus_addtocart"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="@drawable/addbox"
android:layout_alignParentTop="true"
android:focusable="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/proprice_addtocart"
android:gravity="center_vertical"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</RelativeLayout>
在 if-else 块之外编写您的 onClick
侦听器和标记分配代码。
if (convertView == null) {
}
else {
}
holder.position = position;
holder.mBTIncrement.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//increment();
int mValue = Integer.parseInt(holder._value.getText().toString());
mValue++;
quntity++;
tots = pric * quntity;
System.out.println("INCRESE VALUES" + tots);
totvalues.setText(String.valueOf(tots));
holder._value.setText("" + mValue);
}
});
holder._decrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//decrement();
int mValue = Integer.parseInt(holder._value.getText().toString());
mValue--;
quntity--;
tots = pric * quntity;
System.out.println("INCRESE VALUES" + tots);
totvalues.setText(String.valueOf(tots));
if (mValue < 0) {
System.out.println("not valid");
holder._decrease.setEnabled(false);
} else {
holder._value.setText("" + mValue);
}
}
});
您的逻辑需要基于点击的位置,因为适配器可能会在点击后自行刷新并更改位置;
public class ContactListAdapter extends BaseAdapter {
Context context;
ViewHolder holder;
ArrayList<ContactListItems> contactList;
ArrayList<Integer> values;
String dollars = "\u0024";
public ContactListAdapter(Context context, ArrayList<ContactListItems> list) {
this.context = context;
contactList = list;
values = new ArrayList<>();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
values.add(contactListItems.getQtys());
convertView = LayoutInflater.from(getActivity()).inflate(R.layout.list_item_addtocart, null);
holder.propic = (ImageView) convertView.findViewById(R.id.propicaccept);
holder.tvSlNo = (TextView) convertView.findViewById(R.id.txt_size);
holder.tvName = (TextView) convertView.findViewById(R.id.proprice_addtocart);
holder.tvPhone = (TextView) convertView.findViewById(R.id.proname_addtocart);
holder.tvsizes = (TextView) convertView.findViewById(R.id.txt_sizes);
holder._decrease = (Button)convertView.findViewById(R.id.minuss_addtocart);
holder.mBTIncrement = (Button)convertView.findViewById(R.id.plus_addtocart);
holder._value = (EditText)convertView.findViewById(R.id.edt_procount_addtocart);
holder.mBTIncrement.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = holder.position;
int mValue = Integer.parseInt(holder._value.getText().toString());
mValue++;
quntity++;
tots = pric * quntity;
System.out.println("INCRESE VALUES" + tots);
totvalues.setText(String.valueOf(tots));
values.set(position, mValue);
}
});
holder._decrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = holder.position;
int mValue = Integer.parseInt(holder._value.getText().toString());
mValue--;
quntity--;
tots = pric * quntity;
System.out.println("INCRESE VALUES" + tots);
totvalues.setText(String.valueOf(tots));
values.set(position, mValue);
}
});
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.position = position;
holder.tvName.setText(dollars+contactListItems.getName());
holder.tvPhone.setText(contactListItems.getPhone());
holder.tvsizes.setText(contactListItems.getSizes());
int value = values.get(position);
if (value < 0) {
holder._decrease.setEnabled(false);
} else {
holder._value.setText("" + mValue);
}
quntity=Float.parseFloat(contactListItems.getQtys());
pric=Float.parseFloat(contactListItems.getName());
tots=pric*quntity;
totvalues.setText(String.valueOf(tots));
System.out.println("QTYSSS>>" + quntity);
System.out.println("PRICCC>>" + pric);
// float aditya=+tots;
System.out.println("TOTTTT>>" + tots);
//aQuery.id(holder.propic).image(listData.get(position).get(TAG_IMAGE),true,true,0,R.drawable.ic_launcher);
return convertView;
}
class ViewHolder{
int position;
ImageView propic;
TextView tvSlNo;
TextView tvName;
TextView tvPhone;
Button mBTIncrement;
TextView tvsizes;
Button _decrease;
public EditText _value;
}
}
尝试像下面这样实现适配器
private Context context;
private List<ContactListItems> items;
/**
* @param context
* @param groupList
*/
public TestListAdapter(Context context, List<ContactListItems> items) {
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.li_plus_minus, parent, false);
holder = new ViewHolder();
holder.plus = (Button) convertView.findViewById(R.id.plus);
holder.minus = (Button) convertView.findViewById(R.id.minus);
holder.et = (EditText) convertView.findViewById(R.id.editText);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ContactListItems item = items.get(position);
holder.et.setText(String.valueOf(item.value));
holder.plus.setOnClickListener(new ClickListener(item));
holder.minus.setOnClickListener(new ClickListener(item));
return convertView;
}
class ClickListener implements View.OnClickListener {
private ContactListItems item;
public ClickListener(ContactListItems item) {
this.item = item;
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.minus) {
item.value--;
} else if (v.getId() == R.id.plus) {
item.value++;
}
notifyDataSetChanged();
}
}
class ViewHolder {
Button plus, minus;
EditText et;
}
希望对您有所帮助
在您的自定义视图父布局中添加此行。
android:descendantFocusability="blocksDescendants"