当我点击查看按钮时,它会在listview-Sr,Item,Qty,Prize中显示4个字段。 我能够在Listview中获取数据,但问题是,当我增加3次时,它显示增量的所有值。对于1项价格的假设是5.它显示 1-5,2-10,3-15。但我直接想要显示3-15,如果它增加3次。我也添加了xml。谢谢你提前帮忙。 以下是我的代码 -
MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends Activity {
Button show;
ListView list_item;
private ArrayList<Integer> sr;
private ArrayList<String> item;
private ArrayList<Integer> quantity;
private ArrayList<Integer> price;
private ArrayList<Integer> sr1;
private ArrayList<String> item1;
private ArrayList<Integer> price1;
private ArrayList<Integer> data1;
SearchView searchView;
private ArrayList<String> filterlist;
public Custom custom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list_item = (ListView) findViewById(R.id.listdetails);
show=(Button)findViewById(R.id.btnview);
sr = new ArrayList<>();
item = new ArrayList<>();
quantity = new ArrayList<>();
price = new ArrayList<>();
sr1 = new ArrayList<>();
item1 = new ArrayList<>();
data1= new ArrayList<>();
price1= new ArrayList<>();
filterlist = new ArrayList<>();
item = filterlist;
sr.add(1);
sr.add(2);
sr.add(3);
sr.add(4);
item.add("Book");
item.add("pen");
item.add("pencil");
item.add("Eraser");
quantity.add(0);
quantity.add(0);
quantity.add(0);
quantity.add(0);
price.add(20);
price.add(30);
price.add(5);
price.add(5);
Custom c = new Custom(this, sr, item, quantity, price,sr1,item1,data1,price1);
list_item.setAdapter(c);
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,Trial.class);
intent.putExtra("sr",sr1);
intent.putExtra("item",item1);
intent.putExtra("qty",data1);
intent.putExtra("price",price1);
startActivity(intent);
}
});
}
}
Custom.java
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import java.util.ArrayList;
public class Custom extends BaseAdapter {
Activity a;
public ArrayList<Integer> sr;
public ArrayList<String> item;
public ArrayList<Integer> quantity;
public ArrayList<Integer> price;
public ArrayList<Integer> sr1;
public ArrayList<String> item1;
public ArrayList<Integer> data1;
public ArrayList<Integer> price1;
public ArrayList<String> filterlist;
int bookCount = 0;
int penCount = 0;
int pencilCount = 0;
int eraserCount=0;
int bookPrice = 0;
int penPrice = 0;
int pencilPrice = 0;
int eraserPrice=0;
private String data, pricedata;
public Custom(Activity a, ArrayList<Integer> sr, ArrayList<String> item, ArrayList<Integer> quantity, ArrayList<Integer> price, ArrayList<Integer> sr1, ArrayList<String> item1, ArrayList<Integer> data1, ArrayList<Integer> price1) {
this.a = a;
this.sr = sr;
this.item = item;
this.quantity = quantity;
this.price = price;
this.sr1 = sr1;
this.item1 = item1;
this.data1 = data1;
this.price1 = price1;
}
@Override
public int getCount() {
return item.size();
}
@Override
public Object getItem(int position) {
return item.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder{
TextView sr1,item1,qty1,price1;
Button pl,min;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Holder holder=new Holder();
LayoutInflater li=a.getLayoutInflater();
final View view=li.inflate(R.layout.customlist,parent,false);
holder.sr1=(TextView)view.findViewById(R.id.s_no);
holder.item1=(TextView)view.findViewById(R.id.i_name);
holder.qty1=(TextView)view.findViewById(R.id.qty);
holder.price1=(TextView)view.findViewById(R.id.pr);
holder.pl=(Button) view.findViewById(R.id.pl);
holder.min=(Button)view.findViewById(R.id.min);
holder.sr1.setText(String.valueOf(sr.get(position)));
holder.item1.setText(item.get(position));
holder.qty1.setText(String.valueOf(quantity.get(position)));
holder.price1.setText(String.valueOf(price.get(position)));
holder.pl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (position) {
case 0:
if (bookCount >= 0) {
bookCount++;
bookPrice = bookPrice + 20;
sr.get(position);
item.get(position);
}
break;
case 1:
if (penCount >= 0) {
penCount++;
penPrice += 30;
sr.get(position);
item.get(position);
}
break;
case 2:
if (pencilCount >= 0) {
pencilCount++;
pencilPrice += 5;
sr.get(position);
item.get(position);
}
break;
case 3:
if(eraserCount>=0){
eraserCount++;
eraserPrice +=5;
sr.get(position);
item.get(position);
}
break;
}
if(position==0)
{
holder.qty1.setText(String.valueOf(bookCount));
holder.price1.setText(String.valueOf(bookPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(bookCount);
price1.add(bookPrice);
}
else if(position==1)
{
holder.qty1.setText(String.valueOf(penCount));
holder.price1.setText(String.valueOf(penPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(penCount);
price1.add(penPrice);
}
else if(position==2)
{
holder.qty1.setText(String.valueOf(pencilCount));
holder.price1.setText(String.valueOf(pencilPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(pencilCount);
price1.add(pencilPrice);
}
else if(position==3)
{
holder.qty1.setText(String.valueOf(eraserCount));
holder.price1.setText(String.valueOf(eraserPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(eraserCount);
price1.add(eraserPrice);
}
}
});
holder.min.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (position) {
case 0:
if (bookCount > 0) {
bookCount--;
bookPrice -= 20;
sr.get(position);
item.get(position);
}
break;
case 1:
if (penCount > 0) {
penCount--;
penPrice -= 30;
sr.get(position);
item.get(position);
}
break;
case 2:
if (pencilCount > 0) {
pencilCount--;
pencilPrice -= 5;
sr.get(position);
item.get(position);
}
break;
case 3:
if(eraserCount > 0){
eraserCount--;
eraserPrice-=5;
sr.get(position);
item.get(position);
}
break;
}
if(position==0)
{
holder.qty1.setText(String.valueOf(bookCount));
holder.price1.setText(String.valueOf(bookPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(bookCount);
price1.add(bookPrice);
}
else if(position==1)
{
holder.qty1.setText(String.valueOf(penCount));
holder.price1.setText(String.valueOf(penPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(penCount);
price1.add(penPrice);
}
else if(position==2)
{
holder.qty1.setText(String.valueOf(pencilCount));
holder.price1.setText(String.valueOf(pencilPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(pencilCount);
price1.add(pencilPrice);
}
else if(position==3)
{
holder.qty1.setText(String.valueOf(eraserCount));
holder.price1.setText(String.valueOf(eraserPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(eraserCount);
price1.add(eraserPrice);
}
}
});
return view;
}
}
Trial.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class Trial extends Activity {
ListView listnew;
ArrayList<Integer> sr1;
ArrayList<String> item1;
ArrayList<Integer> data;
ArrayList<Integer> price;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.trial);
data=new ArrayList<>();
price=new ArrayList<>();
listnew=(ListView)findViewById(R.id.newlist);
Bundle bundle=getIntent().getExtras();
sr1=bundle.getIntegerArrayList("sr");
item1=bundle.getStringArrayList("item");
data=bundle.getIntegerArrayList("qty");
price=bundle.getIntegerArrayList("price");
Custom_Trial ct=new Custom_Trial(this,sr1,item1,data,price);
listnew.setAdapter(ct);
}
}
CustomTrial.java
package com.example.ram.logic;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class Custom_Trial extends BaseAdapter {
Activity a;
ArrayList<Integer> sr1;
ArrayList<String> item1;
ArrayList<Integer> data;
ArrayList<Integer> price;
public Custom_Trial(Activity a, ArrayList<Integer> sr1, ArrayList<String> item1, ArrayList<Integer> data, ArrayList<Integer> price) {
this.a = a;
this.sr1 = sr1;
this.item1 = item1;
this.data = data;
this.price = price;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder{
TextView sr,it,qty,amt;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder=new Holder();
LayoutInflater li=a.getLayoutInflater();
View view=li.inflate(R.layout.customtrial,parent,false);
holder.sr=(TextView)view.findViewById(R.id.head1);
holder.it=(TextView)view.findViewById(R.id.head2);
holder.qty=(TextView)view.findViewById(R.id.head3);
holder.amt=(TextView)view.findViewById(R.id.head4);
holder.sr.setText(String.valueOf(sr1.get(position)));
holder.it.setText(item1.get(position));
holder.qty.setText(String.valueOf(data.get(position)));
holder.amt.setText(String.valueOf(price.get(position)));
return view;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.abc.logic.MainActivity">
<LinearLayout
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="150dp"
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:id="@+id/qtity"
android:orientation="horizontal"
android:layout_toRightOf="@+id/item_name"
>
<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_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_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_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="350dp"
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
<?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">
<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="150dp"
android:layout_height="50dp"
android:gravity="center"
android:textColor="#000"
android:textStyle="bold"
android:id="@+id/i_name"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/s_no"
/>
<Button
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/i_name"
android:gravity="center"
android:textColor="#000"
android:textStyle="bold"
android:layout_marginTop="5dp"
/>
<Button
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" />
</RelativeLayout>
customtrial.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:id="@+id/head1"
android:paddingLeft="10dp"
android:gravity="center"
android:textSize="20dp"
/>
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:id="@+id/head2"
android:layout_toRightOf="@+id/head1"
android:textSize="20dp"
android:gravity="center"
/>
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:id="@+id/head3"
android:layout_toRightOf="@+id/head2"
android:textSize="20dp"
android:gravity="center"
/>
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:id="@+id/head4"
android:layout_toRightOf="@+id/head3"
android:textSize="20dp"
android:gravity="center"
/>
</RelativeLayout>
trial.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:id="@+id/head1"
android:text="SrNo"
android:paddingLeft="10dp"
android:gravity="center"
android:textSize="20dp"
/>
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:id="@+id/head2"
android:layout_toRightOf="@+id/head1"
android:textSize="20dp"
android:gravity="center"
android:text="Item"
/>
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:id="@+id/head3"
android:text="Qty"
android:layout_toRightOf="@+id/head2"
android:paddingLeft="10dp"
android:gravity="center"
android:textSize="20dp"
/>
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:id="@+id/head4"
android:layout_toRightOf="@+id/head3"
android:textSize="20dp"
android:gravity="center"
android:text="Price"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/newlist"
android:scrollbars="vertical"
android:layout_marginTop="10dp"
android:layout_below="@+id/head1"
>
</ListView>
</RelativeLayout>
答案 0 :(得分:1)
最大数量的变化: MainActivityList.class
package com.demostudies;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Set;
import com.demostudies.adapter.Custom;
public class MainActivityList extends Activity {
Button show;
ListView list_item;
private ArrayList<Integer> sr;
private ArrayList<String> item;
private ArrayList<Integer> quantity;
private ArrayList<Integer> price;
private ArrayList<Integer> sr1;
private ArrayList<String> item1;
private ArrayList<Integer> price1;
private ArrayList<Integer> data1;
SearchView searchView;
private ArrayList<String> filterlist;
public Custom custom;
public int bookCount = 0;
public int penCount = 0;
public int pencilCount = 0;
public int eraserCount=0;
public int bookPrice = 0;
public int penPrice = 0;
public int pencilPrice = 0;
public int eraserPrice=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list);
list_item = (ListView) findViewById(R.id.listdetails);
show=(Button)findViewById(R.id.btnview);
sr = new ArrayList<>();
item = new ArrayList<>();
quantity = new ArrayList<>();
price = new ArrayList<>();
sr1 = new ArrayList<>();
item1 = new ArrayList<>();
data1= new ArrayList<>();
price1= new ArrayList<>();
filterlist = new ArrayList<>();
item = filterlist;
sr.add(1);
sr.add(2);
sr.add(3);
sr.add(4);
item.add("Book");
item.add("pen");
item.add("pencil");
item.add("Eraser");
quantity.add(0);
quantity.add(0);
quantity.add(0);
quantity.add(0);
price.add(20);
price.add(30);
price.add(5);
price.add(5);
Custom c = new Custom(this, sr, item, quantity, price,sr1,item1,data1,price1);
list_item.setAdapter(c);
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivityList.this,Trial.class);
Set<Integer> s = new LinkedHashSet<>();
s.addAll(sr);
sr.clear();
sr.addAll(s);
Set<String> ss = new LinkedHashSet<>();
ss.addAll(item);
item.clear();
item.addAll(ss);
data1.clear();
price1.clear();
for(Integer srn: sr){
if(srn==1){
data1.add(bookCount);
price1.add(bookPrice);
}
if(srn==2){
data1.add(penCount);
price1.add(penPrice);
}
if(srn==3){
data1.add(pencilCount);
price1.add(pencilPrice);
}
if(srn==4){
data1.add(eraserCount);
price1.add(eraserPrice);
}
}
intent.putExtra("sr",sr);
intent.putExtra("item",item);
intent.putExtra("qty",data1);
intent.putExtra("price",price1);
startActivity(intent);
}
});
}
}
Custom.class
package com.demostudies.adapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.demostudies.MainActivityList;
import com.demostudies.R;
import java.util.ArrayList;
public class Custom extends BaseAdapter {
MainActivityList a;
public ArrayList<Integer> sr;
public ArrayList<String> item;
public ArrayList<Integer> quantity;
public ArrayList<Integer> price;
public ArrayList<Integer> sr1;
public ArrayList<String> item1;
public ArrayList<Integer> data1;
public ArrayList<Integer> price1;
public ArrayList<String> filterlist;
private String data, pricedata;
public Custom(MainActivityList a, ArrayList<Integer> sr, ArrayList<String> item, ArrayList<Integer> quantity, ArrayList<Integer> price, ArrayList<Integer> sr1, ArrayList<String> item1, ArrayList<Integer> data1, ArrayList<Integer> price1) {
this.a = a;
this.sr = sr;
this.item = item;
this.quantity = quantity;
this.price = price;
this.sr1 = sr1;
this.item1 = item1;
this.data1 = data1;
this.price1 = price1;
}
@Override
public int getCount() {
return item.size();
}
@Override
public Object getItem(int position) {
return item.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder{
TextView sr1,item1,qty1,price1;
Button pl,min;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Holder holder=new Holder();
LayoutInflater li=a.getLayoutInflater();
final View view=li.inflate(R.layout.customlist,parent,false);
holder.sr1=(TextView)view.findViewById(R.id.s_no);
holder.item1=(TextView)view.findViewById(R.id.i_name);
holder.qty1=(TextView)view.findViewById(R.id.qty);
holder.price1=(TextView)view.findViewById(R.id.pr);
holder.pl=(Button) view.findViewById(R.id.pl);
holder.min=(Button)view.findViewById(R.id.min);
holder.sr1.setText(String.valueOf(sr.get(position)));
holder.item1.setText(item.get(position));
holder.qty1.setText(String.valueOf(quantity.get(position)));
holder.price1.setText(String.valueOf(price.get(position)));
holder.pl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (position) {
case 0:
if (a.bookCount >= 0) {
a.bookCount++;
a.bookPrice = a.bookPrice + 20;
sr.get(position);
item.get(position);
}
break;
case 1:
if (a.penCount >= 0) {
a.penCount++;
a.penPrice += 30;
sr.get(position);
item.get(position);
}
break;
case 2:
if (a.pencilCount >= 0) {
a.pencilCount++;
a.pencilPrice += 5;
sr.get(position);
item.get(position);
}
break;
case 3:
if(a.eraserCount>=0){
a.eraserCount++;
a.eraserPrice +=5;
sr.get(position);
item.get(position);
}
break;
}
if(position==0)
{
holder.qty1.setText(String.valueOf(a.bookCount));
holder.price1.setText(String.valueOf(a.bookPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(a.bookCount);
price1.add(a.bookPrice);
}
else if(position==1)
{
holder.qty1.setText(String.valueOf(a.penCount));
holder.price1.setText(String.valueOf(a.penPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(a.penCount);
price1.add(a.penPrice);
}
else if(position==2)
{
holder.qty1.setText(String.valueOf(a.pencilCount));
holder.price1.setText(String.valueOf(a.pencilPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(a.pencilCount);
price1.add(a.pencilPrice);
}
else if(position==3)
{
holder.qty1.setText(String.valueOf(a.eraserCount));
holder.price1.setText(String.valueOf(a.eraserPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(a.eraserCount);
price1.add(a.eraserPrice);
}
}
});
holder.min.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (position) {
case 0:
if (a.bookCount > 0) {
a.bookCount--;
a.bookPrice -= 20;
sr.get(position);
item.get(position);
}
break;
case 1:
if (a.penCount > 0) {
a.penCount--;
a.penPrice -= 30;
sr.get(position);
item.get(position);
}
break;
case 2:
if (a.pencilCount > 0) {
a.pencilCount--;
a.pencilPrice -= 5;
sr.get(position);
item.get(position);
}
break;
case 3:
if(a.eraserCount > 0){
a.eraserCount--;
a.eraserPrice-=5;
sr.get(position);
item.get(position);
}
break;
}
if(position==0)
{
holder.qty1.setText(String.valueOf(a.bookCount));
holder.price1.setText(String.valueOf(a.bookPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(a.bookCount);
price1.add(a.bookPrice);
}
else if(position==1)
{
holder.qty1.setText(String.valueOf(a.penCount));
holder.price1.setText(String.valueOf(a.penPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(a.penCount);
price1.add(a.penPrice);
}
else if(position==2)
{
holder.qty1.setText(String.valueOf(a.pencilCount));
holder.price1.setText(String.valueOf(a.pencilPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(a.pencilCount);
price1.add(a.pencilPrice);
}
else if(position==3)
{
holder.qty1.setText(String.valueOf(a.eraserCount));
holder.price1.setText(String.valueOf(a.eraserPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(a.eraserCount);
price1.add(a.eraserPrice);
}
}
});
return view;
}
}
Trial.class
package com.demostudies;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import com.demostudies.adapter.Custom_Trial;
import java.util.ArrayList;
public class Trial extends Activity {
ListView listnew;
ArrayList<Integer> sr1;
ArrayList<String> item1;
ArrayList<Integer> data;
ArrayList<Integer> price;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.trial);
data = new ArrayList<>();
price = new ArrayList<>();
listnew = (ListView) findViewById(R.id.newlist);
Bundle bundle = getIntent().getExtras();
sr1 = bundle.getIntegerArrayList("sr");
item1 = bundle.getStringArrayList("item");
data = bundle.getIntegerArrayList("qty");
price = bundle.getIntegerArrayList("price");
ArrayList<Integer> qty = new ArrayList<>();
ArrayList<String> name = new ArrayList<>();
ArrayList<Integer> srn = new ArrayList<>();
ArrayList<Integer> ps = new ArrayList<>();
for(int i=0;i<data.size();i++){
if(data.get(i)>0){
qty.add(data.get(i));
name.add(item1.get(i));
srn.add(sr1.get(i));
ps.add(price.get(i));
}
}
sr1.clear();
sr1.addAll(srn);
item1.clear();
item1.addAll(name);
data.clear();
data.addAll(qty);
price.clear();
price.addAll(ps);
Custom_Trial ct = new Custom_Trial(this, sr1, item1, data, price);
listnew.setAdapter(ct);
}
}
Custom_Trial.class
package com.demostudies.adapter;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.demostudies.R;
import java.util.ArrayList;
public class Custom_Trial extends BaseAdapter {
Activity a;
ArrayList<Integer> sr1;
ArrayList<String> item1;
ArrayList<Integer> data;
ArrayList<Integer> price;
public Custom_Trial(Activity a, ArrayList<Integer> sr1, ArrayList<String> item1, ArrayList<Integer> data, ArrayList<Integer> price) {
this.a = a;
this.sr1 = sr1;
this.item1 = item1;
this.data = data;
this.price = price;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder{
TextView sr,it,qty,amt;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder=new Holder();
LayoutInflater li=a.getLayoutInflater();
View view=li.inflate(R.layout.customtrial,parent,false);
holder.sr=(TextView)view.findViewById(R.id.head1);
holder.it=(TextView)view.findViewById(R.id.head2);
holder.qty=(TextView)view.findViewById(R.id.head3);
holder.amt=(TextView)view.findViewById(R.id.head4);
holder.sr.setText(String.valueOf(sr1.get(position)));
holder.it.setText(item1.get(position));
holder.qty.setText(String.valueOf(data.get(position)));
holder.amt.setText(String.valueOf(price.get(position)));
return view;
}
}
答案 1 :(得分:0)
使用此活动:
MainActivityList.class:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MainActivityList extends Activity {
Button show;
ListView list_item;
ArrayList<Model> models = new ArrayList<>();
public Custom custom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list);
list_item = (ListView) findViewById(R.id.listdetails);
show = (Button) findViewById(R.id.btnview);
Model model = new Model();
model.setItemName("Book");
model.setPrice("10");
model.setQty("0");
model.setSrNo("1");
model.setTotalSum("0");
models.add(model);
model = new Model();
model.setItemName("Eraser");
model.setPrice("5");
model.setQty("0");
model.setSrNo("1");
model.setTotalSum("0");
models.add(model);
model = new Model();
model.setItemName("Pen");
model.setPrice("5");
model.setQty("0");
model.setSrNo("1");
model.setTotalSum("0");
models.add(model);
model = new Model();
model.setItemName("Pencil");
model.setPrice("5");
model.setQty("0");
model.setSrNo("1");
model.setTotalSum("0");
models.add(model);
Custom c = new Custom();
list_item.setAdapter(c);
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivityList.this,Trial.class);
intent.putExtra("data",models);
startActivity(intent);
}
});
}
public class Custom extends BaseAdapter {
@Override
public int getCount() {
return models.size();
}
@Override
public Object getItem(int position) {
return models.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder {
TextView sr1, item1, qty1, price1;
Button pl, min;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Holder holder = new Holder();
LayoutInflater li = MainActivityList.this.getLayoutInflater();
final View view = li.inflate(R.layout.customlist, parent, false);
holder.sr1 = (TextView) view.findViewById(R.id.s_no);
holder.item1 = (TextView) view.findViewById(R.id.i_name);
holder.qty1 = (TextView) view.findViewById(R.id.qty);
holder.price1 = (TextView) view.findViewById(R.id.pr);
holder.pl = (Button) view.findViewById(R.id.pl);
holder.min = (Button) view.findViewById(R.id.min);
final Model model = models.get(position);
holder.sr1.setText(String.valueOf(model.getSrNo()));
holder.item1.setText(model.getItemName());
holder.qty1.setText(String.valueOf(model.getQty()));
holder.price1.setText(String.valueOf(model.getTotalSum()));
holder.pl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int temp = Integer.parseInt(model.getQty()) + 1;
int tempPrice = Integer.parseInt(model.getPrice()) * temp;
model.setTotalSum(tempPrice + "");
model.setQty(temp + "");
notifyDataSetChanged();
}
});
holder.min.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int temp = Integer.parseInt(model.getQty()) - 1;
if (temp > 0) {
int tempPrice = Integer.parseInt(model.getPrice()) * temp;
model.setTotalSum(tempPrice + "");
model.setQty(temp + "");
} else {
int tempPrice = Integer.parseInt(model.getPrice()) * 0;
model.setTotalSum(tempPrice + "");
model.setQty(0 + "");
}
notifyDataSetChanged();
}
}
);
return view;
}
}
}
Trial.class
package com.demostudies;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class Trial extends Activity {
ListView listnew;
private ArrayList<Model> data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.trial);
listnew = (ListView) findViewById(R.id.newlist);
data.addAll((ArrayList<Model>) getIntent().getSerializableExtra("data"));
Custom_Trial ct = new Custom_Trial();
listnew.setAdapter(ct);
}
class Custom_Trial extends BaseAdapter {
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder {
TextView sr, it, qty, amt;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
LayoutInflater li = Trial.this.getLayoutInflater();
View view = li.inflate(R.layout.customtrial, parent, false);
holder.sr = (TextView) view.findViewById(R.id.head1);
holder.it = (TextView) view.findViewById(R.id.head2);
holder.qty = (TextView) view.findViewById(R.id.head3);
holder.amt = (TextView) view.findViewById(R.id.head4);
Model m = data.get(position);
holder.sr.setText(String.valueOf(m.getSrNo()));
holder.it.setText(m.getItemName());
holder.qty.setText(String.valueOf(m.getQty()));
holder.amt.setText(String.valueOf(m.getTotalSum()));
return view;
}
}
}
Model.class
import java.io.Serializable;
public class Model implements Serializable
{
String qty, itemName, price, srNo, totalSum;
public String getQty() {
return qty;
}
public void setQty(String qty) {
this.qty = qty;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getSrNo() {
return srNo;
}
public void setSrNo(String srNo) {
this.srNo = srNo;
}
public String getTotalSum() {
return totalSum;
}
public void setTotalSum(String totalSum) {
this.totalSum = totalSum;
}
}
答案 2 :(得分:0)
您必须在此处对CustomAdapter进行更改,而不是模型类:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Set;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MainActivityList extends Activity {
Button show;
ListView list_item;
private ArrayList<Integer> sr;
private ArrayList<String> item;
private ArrayList<Integer> quantity;
private ArrayList<Integer> price;
private ArrayList<Integer> sr1;
private ArrayList<String> item1;
private ArrayList<Integer> price1;
private ArrayList<Integer> data1;
SearchView searchView;
private ArrayList<String> filterlist;
public Custom custom;
int bookCount = 0;
int penCount = 0;
int pencilCount = 0;
int eraserCount=0;
int bookPrice = 0;
int penPrice = 0;
int pencilPrice = 0;
int eraserPrice=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list);
list_item = (ListView) findViewById(R.id.listdetails);
show=(Button)findViewById(R.id.btnview);
sr = new ArrayList<>();
item = new ArrayList<>();
quantity = new ArrayList<>();
price = new ArrayList<>();
sr1 = new ArrayList<>();
item1 = new ArrayList<>();
data1= new ArrayList<>();
price1= new ArrayList<>();
filterlist = new ArrayList<>();
item = filterlist;
sr.add(1);
sr.add(2);
sr.add(3);
sr.add(4);
item.add("Book");
item.add("pen");
item.add("pencil");
item.add("Eraser");
quantity.add(0);
quantity.add(0);
quantity.add(0);
quantity.add(0);
price.add(20);
price.add(30);
price.add(5);
price.add(5);
Custom c = new Custom(this, sr, item, quantity, price,sr1,item1,data1,price1);
list_item.setAdapter(c);
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivityList.this,Trial.class);
Set<Integer> s = new LinkedHashSet<>();
s.addAll(sr);
sr.clear();
sr.addAll(s);
Set<String> ss = new LinkedHashSet<>();
ss.addAll(item);
item.clear();
item.addAll(ss);
data1.clear();
price1.clear();
for(Integer srn: sr){
if(srn==1){
data1.add(bookCount);
price1.add(bookPrice);
}
if(srn==2){
data1.add(penCount);
price1.add(penPrice);
}
if(srn==3){
data1.add(pencilCount);
price1.add(pencilPrice);
}
if(srn==4){
data1.add(eraserCount);
price1.add(eraserPrice);
}
}
intent.putExtra("sr",sr);
intent.putExtra("item",item);
intent.putExtra("qty",data1);
intent.putExtra("price",price1);
startActivity(intent);
}
});
}
public class Custom extends BaseAdapter {
Activity a;
public ArrayList<Integer> sr;
public ArrayList<String> item;
public ArrayList<Integer> quantity;
public ArrayList<Integer> price;
public ArrayList<Integer> sr1;
public ArrayList<String> item1;
public ArrayList<Integer> data1;
public ArrayList<Integer> price1;
public ArrayList<String> filterlist;
private String data, pricedata;
public Custom(Activity a, ArrayList<Integer> sr, ArrayList<String> item, ArrayList<Integer> quantity, ArrayList<Integer> price, ArrayList<Integer> sr1, ArrayList<String> item1, ArrayList<Integer> data1, ArrayList<Integer> price1) {
this.a = a;
this.sr = sr;
this.item = item;
this.quantity = quantity;
this.price = price;
this.sr1 = sr1;
this.item1 = item1;
this.data1 = data1;
this.price1 = price1;
}
@Override
public int getCount() {
return item.size();
}
@Override
public Object getItem(int position) {
return item.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder{
TextView sr1,item1,qty1,price1;
Button pl,min;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Holder holder=new Holder();
LayoutInflater li=a.getLayoutInflater();
final View view=li.inflate(R.layout.customlist,parent,false);
holder.sr1=(TextView)view.findViewById(R.id.s_no);
holder.item1=(TextView)view.findViewById(R.id.i_name);
holder.qty1=(TextView)view.findViewById(R.id.qty);
holder.price1=(TextView)view.findViewById(R.id.pr);
holder.pl=(Button) view.findViewById(R.id.pl);
holder.min=(Button)view.findViewById(R.id.min);
holder.sr1.setText(String.valueOf(sr.get(position)));
holder.item1.setText(item.get(position));
holder.qty1.setText(String.valueOf(quantity.get(position)));
holder.price1.setText(String.valueOf(price.get(position)));
holder.pl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (position) {
case 0:
if (bookCount >= 0) {
bookCount++;
bookPrice = bookPrice + 20;
sr.get(position);
item.get(position);
}
break;
case 1:
if (penCount >= 0) {
penCount++;
penPrice += 30;
sr.get(position);
item.get(position);
}
break;
case 2:
if (pencilCount >= 0) {
pencilCount++;
pencilPrice += 5;
sr.get(position);
item.get(position);
}
break;
case 3:
if(eraserCount>=0){
eraserCount++;
eraserPrice +=5;
sr.get(position);
item.get(position);
}
break;
}
if(position==0)
{
holder.qty1.setText(String.valueOf(bookCount));
holder.price1.setText(String.valueOf(bookPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(bookCount);
price1.add(bookPrice);
}
else if(position==1)
{
holder.qty1.setText(String.valueOf(penCount));
holder.price1.setText(String.valueOf(penPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(penCount);
price1.add(penPrice);
}
else if(position==2)
{
holder.qty1.setText(String.valueOf(pencilCount));
holder.price1.setText(String.valueOf(pencilPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(pencilCount);
price1.add(pencilPrice);
}
else if(position==3)
{
holder.qty1.setText(String.valueOf(eraserCount));
holder.price1.setText(String.valueOf(eraserPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(eraserCount);
price1.add(eraserPrice);
}
}
});
holder.min.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (position) {
case 0:
if (bookCount > 0) {
bookCount--;
bookPrice -= 20;
sr.get(position);
item.get(position);
}
break;
case 1:
if (penCount > 0) {
penCount--;
penPrice -= 30;
sr.get(position);
item.get(position);
}
break;
case 2:
if (pencilCount > 0) {
pencilCount--;
pencilPrice -= 5;
sr.get(position);
item.get(position);
}
break;
case 3:
if(eraserCount > 0){
eraserCount--;
eraserPrice-=5;
sr.get(position);
item.get(position);
}
break;
}
if(position==0)
{
holder.qty1.setText(String.valueOf(bookCount));
holder.price1.setText(String.valueOf(bookPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(bookCount);
price1.add(bookPrice);
}
else if(position==1)
{
holder.qty1.setText(String.valueOf(penCount));
holder.price1.setText(String.valueOf(penPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(penCount);
price1.add(penPrice);
}
else if(position==2)
{
holder.qty1.setText(String.valueOf(pencilCount));
holder.price1.setText(String.valueOf(pencilPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(pencilCount);
price1.add(pencilPrice);
}
else if(position==3)
{
holder.qty1.setText(String.valueOf(eraserCount));
holder.price1.setText(String.valueOf(eraserPrice));
sr1.add(sr.get(position));
item1.add(item.get(position));
data1.add(eraserCount);
price1.add(eraserPrice);
}
}
});
return view;
}
}
}
试用活动:
package com.demostudies;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class Trial extends Activity {
ListView listnew;
ArrayList<Integer> sr1;
ArrayList<String> item1;
ArrayList<Integer> data;
ArrayList<Integer> price;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.trial);
data = new ArrayList<>();
price = new ArrayList<>();
listnew = (ListView) findViewById(R.id.newlist);
Bundle bundle = getIntent().getExtras();
sr1 = bundle.getIntegerArrayList("sr");
item1 = bundle.getStringArrayList("item");
data = bundle.getIntegerArrayList("qty");
price = bundle.getIntegerArrayList("price");
ArrayList<Integer> qty = new ArrayList<>();
ArrayList<String> name = new ArrayList<>();
ArrayList<Integer> srn = new ArrayList<>();
ArrayList<Integer> ps = new ArrayList<>();
for(int i=0;i<data.size();i++){
if(data.get(i)>0){
qty.add(data.get(i));
name.add(item1.get(i));
srn.add(sr1.get(i));
ps.add(price.get(i));
}
}
sr1.clear();
sr1.addAll(srn);
item1.clear();
item1.addAll(name);
data.clear();
data.addAll(qty);
price.clear();
price.addAll(ps);
Custom_Trial ct = new Custom_Trial(this, sr1, item1, data, price);
listnew.setAdapter(ct);
}
public class Custom_Trial extends BaseAdapter {
Activity a;
ArrayList<Integer> sr1;
ArrayList<String> item1;
ArrayList<Integer> data;
ArrayList<Integer> price;
public Custom_Trial(Activity a, ArrayList<Integer> sr1, ArrayList<String> item1, ArrayList<Integer> data, ArrayList<Integer> price) {
this.a = a;
this.sr1 = sr1;
this.item1 = item1;
this.data = data;
this.price = price;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder{
TextView sr,it,qty,amt;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder=new Holder();
LayoutInflater li=a.getLayoutInflater();
View view=li.inflate(R.layout.customtrial,parent,false);
holder.sr=(TextView)view.findViewById(R.id.head1);
holder.it=(TextView)view.findViewById(R.id.head2);
holder.qty=(TextView)view.findViewById(R.id.head3);
holder.amt=(TextView)view.findViewById(R.id.head4);
holder.sr.setText(String.valueOf(sr1.get(position)));
holder.it.setText(item1.get(position));
holder.qty.setText(String.valueOf(data.get(position)));
holder.amt.setText(String.valueOf(price.get(position)));
return view;
}
}
}