我创建了一个Listview,我希望在点击+时增加和减少数量和价格。问题是,当我点击+和 - 它没有增加或减少。任何人都可以给我正确的代码plz.Thanks为了你的帮助提前。 这是我的代码 -
MainActivity.java
public class MainActivity extends Activity implements SearchView.OnQueryTextListener {
Button show;
ListView list_item;
ArrayList<Items> itemsArrayList;
SearchView searchview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list_item = (ListView) findViewById(R.id.listdetails);
searchview = (SearchView) findViewById(R.id.searchView);
show = (Button) findViewById(R.id.btnview);
itemsArrayList = new ArrayList<>();
itemsArrayList.add(new Items(1, "Book", 20, 0, 0));
itemsArrayList.add(new Items(2, "Pen", 25, 0, 0));
itemsArrayList.add(new Items(3, "Scale", 10, 0, 0));
itemsArrayList.add(new Items(4, "Eraser", 5, 0, 0));
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Trial.class);
intent.putExtra("data", itemsArrayList);
startActivity(intent);
}
});
Custom c = new Custom(this,itemsArrayList);
list_item.setAdapter(c);
list_item.setTextFilterEnabled(true);
setupSearchView();
}
public void setupSearchView() {
searchview.setOnQueryTextListener(this);
searchview.setQueryHint("Search Here");
}
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
if (TextUtils.isEmpty(s)) {
list_item.clearTextFilter();
} else {
list_item.setFilterText(s);
}
return true;
}
}
Custom.java
public class Custom extends BaseAdapter implements Filterable{
Activity a;
ArrayList<Items> itemsArrayList;
ArrayList<Items> filtered;
public Custom(Activity a, ArrayList<Items> itemsArrayList) {
this.a = a;
this.itemsArrayList = itemsArrayList;
}
@Override
public int getCount() {
return itemsArrayList.size();
}
@Override
public Object getItem(int i) {
return itemsArrayList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults results = new FilterResults();
ArrayList<Items> data = new ArrayList<Items>();
if (filtered == null)
filtered = itemsArrayList;
if (filtered != null && filtered.size() > 0) {
for (final Items g : filtered) {
if (g.getItemname().toLowerCase()
.contains(charSequence.toString()))
data.add(g);
}
results.values = data;
}
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
itemsArrayList = (ArrayList<Items>) filterResults.values;
notifyDataSetChanged();
}
};
}
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
public class Holder{
TextView sr, item, qty, price, pl, min, rate;
}
@Override
public View getView(final int position, final View view, ViewGroup viewGroup) {
final Holder holder = new Holder();
LayoutInflater li = a.getLayoutInflater();
final View view1 = li.inflate(R.layout.customlist, viewGroup, false);
holder.sr = (TextView) view1.findViewById(R.id.s_no);
holder.item = (TextView) view1.findViewById(R.id.i_name);
holder.qty = (TextView) view1.findViewById(R.id.qty);
holder.price = (TextView) view1.findViewById(R.id.pr);
holder.rate = (TextView) view1.findViewById(R.id.frate);
holder.pl=(TextView) view1.findViewById(R.id.pl);
holder.min=(TextView) view1.findViewById(R.id.min);
final Items model = itemsArrayList.get(position);
holder.sr.setText(String.valueOf(itemsArrayList.get(position).getSrno()));
holder.item.setText(String.valueOf(itemsArrayList.get(position).getItemname()));
holder.rate.setText(String.valueOf(itemsArrayList.get(position).getFixedrate()));
holder.qty.setText(String.valueOf(itemsArrayList.get(position).getQuantity()));
holder.price.setText(String.valueOf(itemsArrayList.get(position).getRate()));
holder.pl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int temp = Integer.parseInt(String.valueOf(model.getQuantity())) + 1;
int price1 = Integer.parseInt(String.valueOf(model.getFixedrate())) * temp;
holder.qty.setText(temp + " ");
holder.price.setText(price1 + " ");
notifyDataSetChanged();
}
});
holder.min.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int temp = Integer.parseInt(String.valueOf(model.getQuantity())) - 1;
int price1 = Integer.parseInt(String.valueOf(model.getFixedrate())) * temp;
if (temp > 0) {
int tempPrice = Integer.parseInt(String.valueOf(model.getFixedrate())) * temp;
holder.qty.setText(temp + " ");
holder.price.setText(price1 + " ");
} else {
int tempPrice = Integer.parseInt(String.valueOf(model.getFixedrate())) * 0;
holder.qty.setText(temp + " ");
holder.price.setText(price1 + " ");
}
notifyDataSetChanged();
}
});
return view1;
}
}
Items.java
public class Items implements Serializable {
int srno, fixedrate, quantity, rate;
String itemname;
public Items(int srno, String itemname, int fixedrate, int quantity, int rate) {
this.srno = srno;
this.itemname = itemname;
this.fixedrate = fixedrate;
this.quantity = quantity;
this.rate = rate;
}
public int getSrno() {
return srno;
}
public void setSrno(int srno) {
this.srno = srno;
}
public String getItemname() {
return itemname;
}
public void setItemname(String itemname) {
this.itemname = itemname;
}
public int getFixedrate() {
return fixedrate;
}
public void setFixedrate(int fixedrate) {
this.fixedrate = fixedrate;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public int getRate() {
return rate;
}
public void setRate(int rate) {
this.rate = rate;
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<SearchView
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_height="30dp"
android:id="@+id/searchView"
android:queryHint="Search "
>
</SearchView>
<LinearLayout
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_name"
android:orientation="horizontal"
android:layout_toRightOf="@+id/sr_no"
>
<TextView
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_toRightOf="@+id/srno"
android:layout_below="@+id/breakfast"
android:text="ITEM NAME"
android:id="@+id/itemname"
android:gravity="center"
android:textStyle="bold"
android:textColor="#000"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="@+id/fixrate"
android:orientation="horizontal"
android:layout_toRightOf="@+id/item_name">
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_toRightOf="@+id/srno"
android:layout_below="@+id/breakfast"
android:text="RATE"
android:id="@+id/fixratee"
android:gravity="center"
android:textStyle="bold"
android:textColor="#000"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="@+id/qtity"
android:orientation="horizontal"
android:layout_toRightOf="@+id/fixrate"
>
<TextView
android:layout_width="40dp"
android:layout_height="50dp"
android:layout_toRightOf="@+id/itemname"
android:layout_below="@+id/lunch"
android:text="QTY"
android:id="@+id/quantity"
android:gravity="center"
android:textStyle="bold"
android:textColor="#000"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/count"
android:layout_marginTop="30dp"
android:layout_toRightOf="@+id/qtity"
>
<TextView
android:layout_width="50dp"
android:layout_height="50dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rate"
android:orientation="horizontal"
android:layout_marginTop="30dp"
android:layout_toRightOf="@+id/count"
>
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_toRightOf="@+id/quantity"
android:layout_below="@+id/snacks"
android:text="PRICE"
android:id="@+id/price"
android:gravity="center"
android:textStyle="bold"
android:textColor="#000"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sr_no"
android:orientation="horizontal"
android:layout_marginTop="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@+id/breakfast"
android:layout_margin="2dp"
android:layout_marginTop="10dp"
android:text="SR.NO"
android:gravity="center"
android:textStyle="bold"
android:textColor="#000"
android:id="@+id/srno"
/>
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/listdetails"
android:scrollbars="vertical"
android:layout_below="@+id/item_name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</ListView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnview"
android:text="View"
android:layout_below="@+id/listdetails"
android:clickable="true">
</Button>
</RelativeLayout>
customlist.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/s_no"
android:layout_marginTop="5dp"
android:gravity="center"
android:textColor="#000"
android:textStyle="bold"
/>
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/frate"
android:gravity="center"
android:textColor="#000"
android:textStyle="bold"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/i_name"/>
<TextView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="@+id/pl"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/qty"
android:background="@drawable/plus1"
/>
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/qty"
android:layout_toRightOf="@id/frate"
android:gravity="center"
android:textColor="#000"
android:textStyle="bold"
android:layout_marginTop="5dp"
/>
<TextView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="@+id/min"
android:layout_marginTop="3dp"
android:layout_below="@id/pl"
android:layout_toRightOf="@id/qty"
android:background="@drawable/minus1"
/>
<TextView
android:layout_width="60dp"
android:layout_height="50dp"
android:id="@+id/pr"
android:gravity="center"
android:textColor="#000"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:layout_alignBottom="@+id/min"
android:layout_toRightOf="@+id/pl"
android:layout_toEndOf="@+id/pl" />
<TextView
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center"
android:textColor="#000"
android:textStyle="bold"
android:id="@+id/i_name"
android:layout_alignBaseline="@+id/s_no"
android:layout_alignBottom="@+id/s_no"
android:layout_toRightOf="@+id/s_no"
android:layout_toEndOf="@+id/s_no" />
</RelativeLayout>
答案 0 :(得分:0)
未使用新的递增/递减值更新模型。
holder.pl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
model.setQuantity(model.getQuantity() + 1)
holder.qty.setText(String.valueOf(model.getQuantity()));
holder.price.setText(String.valueOf(model.getQuantity() *
model.getFixedrate()));
notifyDataSetChanged();
}
});
答案 1 :(得分:0)
终于明白了。
holder.pl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
model.setQuantity(model.getQuantity() + 1);
model.setRate(model.getQuantity()*model.getFixedrate());
holder.qty.setText(String.valueOf(model.getQuantity()));
holder.price.setText(String.valueOf(model.getRate()));
notifyDataSetChanged();
}
});
答案 2 :(得分:0)
试一试:
holder.pl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
model.setQuantity(model.getQuantity() + 1);
model.setRate(model.getQuantity() * model.getFixedrate());
holder.qty.setText(String.valueOf(model.getQuantity()));
holder.price.setText(String.valueOf(model.getRate()));
notifyDataSetChanged();
}
});
holder.min.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (model.getQuantity() > 0) {
model.setQuantity(model.getQuantity() - 1);
model.setRate(model.getQuantity() * model.getFixedrate());
holder.qty.setText(String.valueOf(model.getQuantity()));
holder.price.setText(String.valueOf(model.getRate()));
notifyDataSetChanged();
}
}
});