如何过滤数据日期明智的位置在同一列表中的给定示例中

时间:2013-09-12 06:52:19

标签: android

     package com.nous.demoexample;

        import static com.nous.demoexample.Constant.EIGTH_COLUMN;
        import static com.nous.demoexample.Constant.FIFTH_COLUMN;
        import static com.nous.demoexample.Constant.FIRST_COLUMN;
        import static com.nous.demoexample.Constant.FOURTH_COLUMN;
        import static com.nous.demoexample.Constant.SECOND_COLUMN;
        import static com.nous.demoexample.Constant.SEVENTH_COLUMN;
        import static com.nous.demoexample.Constant.SIXTH_COLUMN;
        import static com.nous.demoexample.Constant.THIRD_COLUMN;

        import java.util.ArrayList;
        import java.util.HashMap;

        import android.app.Activity;
        import android.app.AlertDialog;
        import android.content.Context;
        import android.content.DialogInterface;
        import android.content.Intent;
        import android.os.Bundle;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.Button;
        import android.widget.GridView;
        import android.widget.ListView;
        import android.widget.TextView;

        import com.paresh.demoexample.R;

        public class MultiColumnActivity extends Activity implements OnClickListener {
            private ArrayList<HashMap<String, String>> list;
            Button filterButton;
            Context context = this;
            ListView lview;
            // GridView lview;
            final CharSequence[] items = { "Select all", " Date ", " Locationto",
                    " Client Name ", " Product code ", "Product Description",
                    "Location from", "Quantity", "Pallets" };

            protected ArrayList<CharSequence> selectedInterests = new ArrayList<CharSequence>();

            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

                init();

            }

            private void init() {
                // lview = (GridView) findViewById(R.id.listview);
                lview = (ListView) findViewById(R.id.listview);

                filterButton = (Button) findViewById(R.id.filterbutton);
                filterButton.setOnClickListener(this);
                populateList();
                listviewAdapter adapter = new listviewAdapter(this, list);
                lview.setAdapter(adapter);

                int lm = lview.getCount();
                System.out.println(lm);

            }

            private void populateList() {

                list = new ArrayList<HashMap<String, String>>();

                HashMap<String, String> temp = new HashMap<String, String>();
                temp.put(FIRST_COLUMN, "DATE");
                temp.put(SECOND_COLUMN, "LOCATION TO");

                temp.put(THIRD_COLUMN, "CLIENT NAME");
                temp.put(FOURTH_COLUMN, "PRODUCT CODE");
                temp.put(FIFTH_COLUMN, "PRODUCT DESCRIPTION");
                temp.put(SIXTH_COLUMN, "LOCATION FROM");
                temp.put(SEVENTH_COLUMN, "QUANTITY");
                temp.put(EIGTH_COLUMN, "PALLETS");

                list.add(temp);

                HashMap<String, String> temp1 = new HashMap<String, String>();
                temp1.put(FIRST_COLUMN, "7 / 2 / 2013");
                temp1.put(SECOND_COLUMN, "SW00-000");
                temp1.put(THIRD_COLUMN, "Mercury Direct");
                temp1.put(FOURTH_COLUMN, "MERCUR-A5WW2013-491-000-000-Loose");
                temp1.put(FIFTH_COLUMN, "A5 WORLDWIDE HOLS 13 C");
                temp1.put(SIXTH_COLUMN, "Warehouse");
                temp1.put(SEVENTH_COLUMN, " 5000");
                temp1.put(EIGTH_COLUMN, "1");

                list.add(temp1);

                HashMap<String, String> temp2 = new HashMap<String, String>();
                temp2.put(FIRST_COLUMN, "7 / 2 / 2013");
                temp2.put(SECOND_COLUMN, "DAH22A");
                temp2.put(THIRD_COLUMN, "Hurtigruten Ltd");
                temp2.put(FOURTH_COLUMN, "HURTI-NORWY13-000-000-000-10");
                temp2.put(FIFTH_COLUMN, "NORWAY 2013");
                temp2.put(SIXTH_COLUMN, "ShrinkWrap");
                temp2.put(SEVENTH_COLUMN, " 4000");
                temp2.put(EIGTH_COLUMN, "1");

                list.add(temp2);

                HashMap<String, String> temp3 = new HashMap<String, String>();
                temp3.put(FIRST_COLUMN, "7 / 2 / 2013");
                temp3.put(SECOND_COLUMN, "DAH22A/02");
                temp3.put(THIRD_COLUMN, "Hurtigruten Ltd");
                temp3.put(FOURTH_COLUMN, "HURTI-NORWY13-000-000-000-10");
                temp3.put(FIFTH_COLUMN, "NORWAY 2013");
                temp3.put(SIXTH_COLUMN, "ShrinkWrap");
                temp3.put(SEVENTH_COLUMN, " 4000");
                temp3.put(EIGTH_COLUMN, "1");

                list.add(temp3);

                HashMap<String, String> temp4 = new HashMap<String, String>();
                temp4.put(FIRST_COLUMN, "7 / 2 / 2013");
                temp4.put(SECOND_COLUMN, "DAH22A/02");
                temp4.put(THIRD_COLUMN, "Hurtigruten Ltd");
                temp4.put(FOURTH_COLUMN, "HURTI-NORWY13-000-000-000-10");
                temp4.put(FIFTH_COLUMN, "NORWAY 2013");
                temp4.put(SIXTH_COLUMN, "ShrinkWrap");
                temp4.put(SEVENTH_COLUMN, " 4000");
                temp4.put(EIGTH_COLUMN, "1");

                list.add(temp4);
            }

            @Override
            public void onClick(View v) {

                switch (v.getId()) {
                case R.id.filterbutton: {

                    showDialog();
                    break;

                }

                default:
                    break;
                }

            }

            private void showDialog() {

                // arraylist to keep the selected items
                final ArrayList seletedItems = new ArrayList();

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Select The Fields");

                /*
                 * boolean[] checkedInterests = new boolean[items.length]; int count =
                 * items.length;
                 * 
                 * for (int i = 0; i < count; i++) { checkedInterests[i] =
                 * selectedInterests .contains(items[i]);
                 * 
                 * }
                 */

                builder.setMultiChoiceItems(items, null,
                        new DialogInterface.OnMultiChoiceClickListener() {
                            // indexSelected contains the index of item (of which
                            // checkbox checked)
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int indexSelected, boolean isChecked) {
                                if (isChecked) {
                                    // If the user checked the item, add it to the
                                    // selected items
                                    // write your code when user checked the checkbox
                                    seletedItems.add(indexSelected);
                                    selectedInterests.add(items[indexSelected]); // by
                                                                                    // anup

                                } else if (seletedItems.contains(indexSelected)) {
                                    // Else, if the item is already in the array, remove
                                    // it
                                    // write your code when user Uchecked the checkbox
                                    seletedItems.remove(Integer.valueOf(indexSelected));

                                }
                                onChangeSelectedSelection();

                            }
                        })
                        // Set the action buttons
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                // Your code when user clicked on OK
                                // You can write the code to save the selected item here

                            }
                        })
                        .setNegativeButton("Cancel",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int id) {
                                        // Your code when user clicked on Cancel

                                    }
                                });
                AlertDialog dialog = builder.create();
                dialog = builder.create();// AlertDialog dialog; create like this
                                            // outside onClick
                dialog.show();
            }

            protected void onChangeSelectedSelection() {
                StringBuilder stringBuilder = new StringBuilder();
                for (CharSequence selection : selectedInterests)
                    stringBuilder.append(selection + ",");

                // filterButton.setText(stringBuilder.toString());
                String input = selectedInterests.toString();
                System.out.println(input);
                if (input.contains("Date")) {

                //here i want to display Date Only Listview 
                }


                if(input.contains("Locationto")){
                    // here i want to display location to only in same list 
                }

            }

        }

Constant.java

package com.nous.demoexample;

public class Constant {
    public static final String FIRST_COLUMN = "First";
    public static final String SECOND_COLUMN = "Second";
    public static final String THIRD_COLUMN = "Third";
    public static final String FOURTH_COLUMN = "Fourth";
    public static final String FIFTH_COLUMN = "Fifth";
    public static final String SIXTH_COLUMN = "Sixth";
    public static final String SEVENTH_COLUMN = "Seventh";
    public static final String EIGTH_COLUMN = "Eighth";

}




 listvieadapter.java
     package com.nous.demoexample;

        import static com.nous.demoexample.Constant.FIRST_COLUMN;
        import static com.nous.demoexample.Constant.FOURTH_COLUMN;
        import static com.nous.demoexample.Constant.SECOND_COLUMN;
        import static com.nous.demoexample.Constant.THIRD_COLUMN;
        import static com.nous.demoexample.Constant.FIFTH_COLUMN;
        import static com.nous.demoexample.Constant.SIXTH_COLUMN;
        import static com.nous.demoexample.Constant.SEVENTH_COLUMN;
        import static com.nous.demoexample.Constant.EIGTH_COLUMN;

        import java.util.ArrayList;
        import java.util.HashMap;

        import com.paresh.demoexample.R;

        import android.app.Activity;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.BaseAdapter;
        import android.widget.TextView;

        public class listviewAdapter extends BaseAdapter {
            public ArrayList<HashMap<String, String>> list;
            Activity activity;

            public listviewAdapter(Activity activity,
                    ArrayList<HashMap<String, String>> list) {
                super();
                this.activity = activity;
                this.list = list;
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return list.size();
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return list.get(position);
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return 0;
            }

            private class ViewHolder {
                TextView txtFirst;
                TextView txtSecond;
                TextView txtThird;
                TextView txtFourth, txtFifth, txtSixth, txtSeventh, txtEigth;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub

                // TODO Auto-generated method stub
                ViewHolder holder;
                LayoutInflater inflater = activity.getLayoutInflater();

                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.listview_row, null);
                    holder = new ViewHolder();
                    holder.txtFirst = (TextView) convertView
                            .findViewById(R.id.FirstText);
                    holder.txtSecond = (TextView) convertView
                            .findViewById(R.id.SecondText);
                    holder.txtThird = (TextView) convertView
                            .findViewById(R.id.ThirdText);
                    holder.txtFourth = (TextView) convertView
                            .findViewById(R.id.FourthText);
                    holder.txtFifth = (TextView) convertView
                            .findViewById(R.id.FifthText);
                    holder.txtSixth = (TextView) convertView
                            .findViewById(R.id.SixthText);
                    holder.txtSeventh = (TextView) convertView
                            .findViewById(R.id.SeventhText);
                    holder.txtEigth = (TextView) convertView
                            .findViewById(R.id.EigthText);

                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }

                HashMap<String, String> map = list.get(position);
                holder.txtFirst.setText(map.get(FIRST_COLUMN));
                holder.txtSe

    cond.setText(map.get(SECOND_COLUMN));
            holder.txtThird.setText(map.get(THIRD_COLUMN));
            holder.txtFourth.setText(map.get(FOURTH_COLUMN));
            holder.txtFifth.setText(map.get(FIFTH_COLUMN));
            holder.txtSixth.setText(map.get(SIXTH_COLUMN));
            holder.txtSeventh.setText(map.get(SEVENTH_COLUMN));
            holder.txtEigth.setText(map.get(EIGTH_COLUMN));

            return convertView;
        }

    }

我能够明智地显示所有数据日期位置和....一起但我想过滤数据 - 日期明智的位置意味着如果我们检查位置明智然后它应该打印位置只有过滤器日期明智它应该显示日期..在列表请帮助我累了多少但无法做到这一点....我已经创建复选框选择日期明智过滤器,位置明智过滤所以请检查我的代码,如果你想我可以发送完整的代码。< / p>

1 个答案:

答案 0 :(得分:0)

如果您使用EditText进行搜索,那么您必须编写自己的过滤方法。这里有一个示例Custom ListView Search

你必须改变filter这样的方法。

public void filter(String charText) {
    charText = charText.toLowerCase(Locale.getDefault());
    worldpopulationlist.clear();
    if (charText.length() == 0) {
        worldpopulationlist.addAll(arraylist);
    } else {
        for (WorldPopulation wp : arraylist) {
            if(isDate)
            {
              if (wp.getDate().toLowerCase(Locale.getDefault()).contains(charText))
              {
                  worldpopulationlist.add(wp);
              }
            }else{
             if(wp.getLocation().toLowerCase(Locale.getDefault()).contains(charText))  
              {
                worldpopulationlist.add(wp);
              }  
            }

        }
    }
    notifyDataSetChanged();
}