在我的listview中,我设置了两个按钮和一个edittext,但它无法正常工作,我的按钮视图和listview中的edittext看起来像这样
Button1 Edittext Button2
在我的edittext中我设置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>
答案 0 :(得分:1)
编写onClick
侦听器并在if-else块之外标记分配代码。
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);
}
}
});
答案 1 :(得分:0)
您需要将逻辑基于点击的位置,因为适配器可能会在点击后自行刷新并更改位置;
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;
}
}
答案 2 :(得分:0)
尝试实现如下所示的适配器
@Override
public void onBackPressed() {
mediaPlayer.stop();
}
希望这有帮助
答案 3 :(得分:0)
在自定义视图父版面中添加此行。
android:descendantFocusability="blocksDescendants"