SearchView在搜索过程中没有收费

时间:2017-04-10 10:34:56

标签: android searchview

我有listview,它有Sr,Itemname,Qty和price.I能够增加和减少它的价格。我有ViewView下面的View.When我点击它我能得到所需的itemname但它的价格即使找到2,3或4项也是第一个项目名。另一个问题是它只用第一个字母搜索,所以如果我写两个字母然后它就不会被搜索。我从2天就陷入困境。任何人都可以给我一个合适的code.I想要回答而不使用getter和setter.Thanks提前帮助你。 这是我的代码 -

MainActivity.java

public class MainActivity extends Activity implements SearchView.OnQueryTextListener{

    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;


    int tshirtCount = 0;
    int jeansCount = 0;
    int shirtCount = 0;
    int watchCount=0;

    int tshirtPrice = 0;
    int jeansPrice = 0;
    int shirtPrice = 0;
    int watchPrice=0;



    @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.bfsearch);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        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("Tshirt");
        item.add("Jeans");
        item.add("Shirt");
        item.add("Watch");


        quantity.add(0);
        quantity.add(0);
        quantity.add(0);
        quantity.add(0);


        price.add(200);
        price.add(500);
        price.add(400);
        price.add(650);


        Custom c = new Custom(this, sr, item, quantity, price,sr1,item1,data1,price1);
        list_item.setAdapter(c);
        list_item.setTextFilterEnabled(true);
        setUpview();


        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.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(tshirtCount);
                        price1.add(tshirtPrice);
                    }

                    if(srn==2){
                          data1.add(jeansCount);
                        price1.add(jeansPrice);
                    }

                    if(srn==3){
                        data1.add(shirtCount);
                        price1.add(shirtPrice);
                    }

                    if(srn==4){
                        data1.add(watchCount);
                        price1.add(watchPrice);
                    }
                }


                intent.putExtra("sr",sr);
                intent.putExtra("item",item);
                intent.putExtra("qty",data1);
                intent.putExtra("price",price1);
                startActivity(intent);
            }
        });

    }

    public void setUpview(){
        searchView.setOnQueryTextListener(this);
        searchView.setQueryHint("Search here");

    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        list_item.setFilterText(newText);
        return true;

    }
}




**Custom.java**



 public class Custom  extends BaseAdapter  implements Filterable {
    MainActivity 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;



       public Custom(MainActivity 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;
            }

        @Override
        public Filter getFilter() {
            return new Filter() {
                @Override
                protected FilterResults performFiltering(CharSequence constraint) {
                    FilterResults results = new FilterResults();
                    String filterstr = constraint.toString().toUpperCase();
                    ArrayList<String> data = new ArrayList<>();

                    if (filterlist == null)
                        filterlist = item;
                    int count = filterlist.size();
                    String ft = " ";
                    for (int i = 0; i < count; i++) {
                        ft = filterlist.get(i);

                        if (ft.contains(filterstr)) {

                            data.add(ft);

                        }
                    }
                    results.values = data;
                    results.count = data.size();
                    return results;
                }

                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
                    item = (ArrayList<String>) results.values;

                    notifyDataSetChanged();
                }


            };
        }

        public class Holder{
        TextView sr1,item1,qty1,price1;
        Button pl,min;

    }


        public int getViewTypeCount() {
            return 2;
        }

        @Override
        public int getItemViewType(int position) {
            return position%2;
        }



        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

         final Holder holder;



            if (convertView == null) {

                LayoutInflater inflater = a.getLayoutInflater();
                convertView = inflater.inflate(R.layout.customlist, parent, false);
                holder=new Holder();

                holder.sr1 = (TextView) convertView.findViewById(R.id.s_no);
                holder.item1 = (TextView) convertView.findViewById(R.id.i_name);
                holder.qty1 = (TextView) convertView.findViewById(R.id.qty);
                holder.price1 = (TextView) convertView.findViewById(R.id.pr);
                holder.pl = (Button) convertView.findViewById(R.id.pl);
                holder.min = (Button) convertView.findViewById(R.id.min);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }

            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.tshirtCount >= 0) {
                                a.tshirtCount++;
                                a.tshirtPrice = a.tshirtPrice + 200;
                                sr.get(position);
                                item.get(position);

                            }
                            break;

                        case 1:
                            if (a.jeansCount >= 0) {
                                a.jeansCount++;
                                a.jeansPrice += 500;

                                sr.get(position);
                                item.get(position);

                            }
                            break;
                        case 2:
                            if (a.shirtCount >= 0) {
                                a.shirtCount++;
                                a.shirtPrice += 400;

                                sr.get(position);
                                item.get(position);

                            }
                            break;
                        case 3:
                            if(a.watchCount>=0){
                                a.watchCount++;
                                a.watchPrice +=650;

                                sr.get(position);
                                item.get(position);


                            }
                            break;
                    }
                    if(position==0)
                    {
                       holder.qty1.setText(String.valueOf(a.tshirtCount));
                        holder.price1.setText(String.valueOf(a.tshirtPrice));

                        sr1.add(sr.get(position));
                        item1.add(item.get(position));
                        data1.add(a.tshirtCount);
                        price1.add(a.tshirtPrice);
                    }
                    else if(position==1)
                    {
                        holder.qty1.setText(String.valueOf(a.jeansCount));
                        holder.price1.setText(String.valueOf(a.jeansPrice));

                        sr1.add(sr.get(position));
                        item1.add(item.get(position));
                        data1.add(a.jeansCount);
                        price1.add(a.jeansPrice);
                    }
                    else if(position==2)
                    {
                        holder.qty1.setText(String.valueOf(a.shirtCount));
                        holder.price1.setText(String.valueOf(a.shirtPrice));

                        sr1.add(sr.get(position));
                        item1.add(item.get(position));
                        data1.add(a.shirtCount);
                        price1.add(a.shirtPrice);
                    }
                    else if(position==3)
                    {
                        holder.qty1.setText(String.valueOf(a.watchCount));
                        holder.price1.setText(String.valueOf(a.watchPrice));

                        sr1.add(sr.get(position));
                        item1.add(item.get(position));
                        data1.add(a.watchCount);
                        price1.add(a.watchPrice);

                    }


                }
            });



            holder.min.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    switch (position) {
                        case 0:
                            if (a.tshirtCount > 0) {
                                a.tshirtCount--;
                                a.tshirtPrice -= 200;

                                sr.get(position);
                                item.get(position);

                            }
                            break;
                        case 1:
                            if (a.jeansCount > 0) {
                                a.jeansCount--;
                                a.jeansPrice -= 500;

                                sr.get(position);
                                item.get(position);

                            }
                            break;

                        case 2:
                            if (a.shirtCount > 0) {
                                a.shirtCount--;
                                a.shirtPrice -= 400;

                                sr.get(position);
                                item.get(position);

                            }
                            break;
                        case 3:
                            if(a.watchCount > 0){
                                a.watchCount--;
                                a.watchPrice-=650;

                                sr.get(position);
                                item.get(position);

                            }
                            break;
                    }

                    if(position==0)
                    {
                        holder.qty1.setText(String.valueOf(a.tshirtCount));
                        holder.price1.setText(String.valueOf(a.tshirtPrice));

                        sr1.add(sr.get(position));
                        item1.add(item.get(position));
                        data1.add(a.tshirtCount);
                        price1.add(a.tshirtPrice);
                    }
                    else if(position==1)
                    {
                        holder.qty1.setText(String.valueOf(a.jeansCount));
                        holder.price1.setText(String.valueOf(a.jeansPrice));

                        sr1.add(sr.get(position));
                        item1.add(item.get(position));
                        data1.add(a.jeansCount);
                        price1.add(a.jeansPrice);
                    }
                    else if(position==2)
                    {
                        holder.qty1.setText(String.valueOf(a.shirtCount));
                        holder.price1.setText(String.valueOf(a.shirtPrice));

                        sr1.add(sr.get(position));
                        item1.add(item.get(position));
                        data1.add(a.shirtCount);
                        price1.add(a.shirtPrice);
                    }
                    else if(position==3)
                    {
                        holder.qty1.setText(String.valueOf(a.watchCount));
                        holder.price1.setText(String.valueOf(a.watchPrice));

                        sr1.add(sr.get(position));
                        item1.add(item.get(position));
                        data1.add(a.watchCount);
                        price1.add(a.watchPrice);

                    }


                }

            });


            return convertView;
        }




    }

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="150dp"
        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>



    <android.support.v7.widget.SearchView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/bfsearch"

        android:layout_marginLeft="3dp"
        android:queryHint="Search"
      android:layout_below="@+id/btnview"
        android:layout_gravity="center"
        />

</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>

0 个答案:

没有答案