当我点击查看按钮时,它会在列表视图的下一页显示数据。但问题是数据正在重复出现。我希望数据不要重复。 如果我增加3次,它会显示1,2和3的详细信息。但我希望它能直接显示3的详细信息。以下是我的代码:
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
<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
<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
<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>