带有复选框的Android列表

时间:2012-06-06 05:13:55

标签: android

我的列表中有一个复选框,当我通过滚动列表勾选复选框时,我将值输入数组。 但现在的问题是我有一个搜索框来搜索和勾选。但是当我搜索时,我得到的元素最后被勾选到数组中。 这是我的代码。

public class Person extends ListActivity {
private TreeMap<String, String> map = new TreeMap<String, String>();
String SEARCH_STRING = "";
private String METHOD_NAME;
private String SERVICENAME;
ArrayList<String[]> DISPLAY = new ArrayList<String[]>();
ArrayList<String[]> SEARCH = new ArrayList<String[]>();
ArrayList<Boolean> ok = new ArrayList<Boolean>();
SoapIntractions intractions;
public static ArrayList<String[]> dataList=new ArrayList<String[]>();
boolean FLAG;

EditText search;
Button search_btn, back_btn;
LinearLayout tab1, tab2;
public Context c;

static class Viewholder {
    TextView name, number;
    CheckBox check;
}

public class EfficientAdapter extends BaseAdapter {
    LayoutInflater inflater;
    Context c;

    public EfficientAdapter(Context addNote, int itemadd, Object object,
            int[] to) {
        // TODO Auto-generated constructor stub
        inflater = LayoutInflater.from(addNote);
        c = addNote;
    }

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

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final Viewholder holder;
        final int var = position;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.person, null);
            holder = new Viewholder();
            holder.name = (TextView) convertView
                    .findViewById(R.id.tv_name_prsnLI);
            holder.number = (TextView) convertView
                    .findViewById(R.id.tv_nmbr_prsnLI);
            holder.check = (CheckBox) convertView.findViewById(R.id.check);
            convertView.setTag(holder);

        } else {
            holder = (Viewholder) convertView.getTag();
        }
        CheckBox box=new CheckBox(c);
        String[] argg = SEARCH.get(var);
        holder.name.setText(argg[0]);
        holder.number.setText(argg[1]);

        if (ok.get(position)){
            holder.check.setChecked(true);
        }
        else{
            holder.check.setChecked(false);
        }

        holder.check.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (ok.get(var)) {
                    ok.set(var, false);
                } else {
                    ok.set(var, true);
                }
            }
        });

        return convertView;
    }
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.person_list);

    StateListDrawable bkBtn = new StateListDrawable(), srchBtn = new StateListDrawable();

    bkBtn.addState(new int[] { android.R.attr.state_pressed },
            getResources().getDrawable(R.drawable.back_click));
    bkBtn.addState(new int[] {}, getResources()
            .getDrawable(R.drawable.back));

    srchBtn.addState(new int[] { android.R.attr.state_pressed },
            getResources().getDrawable(R.drawable.srch_click));
    srchBtn.addState(new int[] {},
            getResources().getDrawable(R.drawable.srch));

    search = (EditText) findViewById(R.id.et_search_prsnL);
    search_btn = (Button) findViewById(R.id.btn_search_prsnL);
    search_btn.setBackgroundDrawable(srchBtn);
    back_btn = (Button) findViewById(R.id.btn_back_prsnL);
    back_btn.setBackgroundDrawable(bkBtn);
    tab1 = (LinearLayout) findViewById(R.id.lyt_tab1_prsnL);
    tab2 = (LinearLayout) findViewById(R.id.lyt_tab2_prsnL);

    tab1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            FLAG = false;
            tab1.setBackgroundDrawable(getResources().getDrawable(
                    R.drawable.list_head_click));
            tab2.setBackgroundDrawable(getResources().getDrawable(
                    R.drawable.list_head));
        }
    });

    tab2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            FLAG = true;
            tab2.setBackgroundDrawable(getResources().getDrawable(
                    R.drawable.list_head_click));
            tab1.setBackgroundDrawable(getResources().getDrawable(
                    R.drawable.list_head));
        }
    });

    search_btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            SEARCH_STRING = search.getText().toString();
            SEARCH.clear();
            SearchPageTask srch_task = new SearchPageTask();
            srch_task.execute(new String[] { null });
            Log.d("SEARCH", "Searchable");

        }
    });

    back_btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            int len=ok.size();
            for(int i=0;i<len;i++){
                if(ok.get(i)){
                    dataList.add(new String[]{SEARCH.get(i)[0],SEARCH.get(i)[1],SEARCH.get(i)[2],SEARCH.get(i)[3] });
                }
            }
            Intent in=new Intent(getApplicationContext(),Multiple_view.class);
            in.putExtra("ARRAY", dataList);
            setResult(RESULT_OK,in);
            finish();
            for (int i = 0; i < dataList.size(); i++) {
                Log.d("ARRAY1", ""+dataList.get(i)[0]);
                Log.d("ARRAY1", ""+dataList.get(i)[1]);
                Log.d("ARRAY1", ""+dataList.get(i)[2]);
                Log.d("ARRAY1", ""+dataList.get(i)[3]);
            }

        }
    });

}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    FLAG = false;
    tab1.setBackgroundDrawable(getResources().getDrawable(
            R.drawable.list_head_click));
    tab2.setBackgroundDrawable(getResources().getDrawable(
            R.drawable.list_head));

    METHOD_NAME = "ZaplxTmrDisplayPernr";
    SERVICENAME = "ZAPLX_WEB_TMR_DISPLAY_PERNR/ZaplxTmrDisplayPernrRequest";
    map = new TreeMap<String, String>();
    map.put("IDate", "2012-12-04");
    intractions = new SoapIntractions(METHOD_NAME, SERVICENAME, map,
            Person.this);

    DownloadWebPageTask task = new DownloadWebPageTask();
    task.execute(new String[] { null });

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);

    Log.d("POSITION", "" + position);
    // CheckBox cb = (CheckBox)
    // l.getChildAt(position).findViewById(R.id.check);
    // if (cb.isChecked()) {
    // cb.setChecked(false);
    //
    // }
    // else
    // {
    // cb.setChecked(true);

    // }
    // Intent in1=new Intent();
    // String[] argg1 = SEARCH.get(position);
    //
    // in1.putExtra("number", SEARCH.get(position)[0]);
    // in1.putExtra("name", SEARCH.get(position)[1]);
    // in1.putExtra("area", SEARCH.get(position)[2]);
    // in1.putExtra("group", SEARCH.get(position)[3]);
    // setResult(RESULT_OK,in1);
    // finish();

}

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {

    String[][] data;
    int value = 0;
    ProgressBar pb;

    public DownloadWebPageTask() {
        // TODO Auto-generated constructor stub
        // flag = string;
        value = 0;
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        try {
            data = intractions.GetSoapData1();
            String[][] subtypeData = data;
            int n = subtypeData[0].length;
            value = n;

            for (int i = 0; i < value; i++) {
                String Pernr = subtypeData[0][i];
                String Ename = subtypeData[1][i];
                String Earea = subtypeData[3][i];
                String Egroup = subtypeData[5][i];
                ok.add(false);
                DISPLAY.add(new String[] { Pernr, Ename, Earea, Egroup });
            }

            for (int i = 0; i < DISPLAY.size(); i++)
                SEARCH.add(DISPLAY.get(i));

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        // pb = (ProgressBar) findViewById(R.id.pb);
        // pb.setVisibility(View.VISIBLE);
        search_btn.setClickable(false);
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        // pb = (ProgressBar) findViewById(R.id.pb);
        // pb.setVisibility(View.INVISIBLE);

        setListAdapter(new EfficientAdapter(Person.this, R.layout.person,
                null, null));
        search_btn.setClickable(true);
        Log.d("POST_TIME", "Inside post");
        super.onPostExecute(result);
    }

}

private class SearchPageTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        Log.d("SEARCH", "List Size" + DISPLAY.size());
        Log.d("SEARCH", "List Size" + SEARCH.size());
        int n = DISPLAY.size();

        if (!FLAG) {
            for (int i = 0; i < n; i++) {

                if (DISPLAY.get(i)[1].toLowerCase().startsWith(
                        SEARCH_STRING.toLowerCase())) {

                    SEARCH.add(DISPLAY.get(i));
                    Log.d("SEARCH_ATSK", "result size " + SEARCH.size());
                }

                Log.d("SEARCH_ATSK", "" + i + " " + n);

            }
        } else {

            for (int i = 0; i < n; i++) {

                if (DISPLAY.get(i)[0].toLowerCase().endsWith(
                        SEARCH_STRING.toLowerCase())) {

                    SEARCH.add(DISPLAY.get(i));
                    Log.d("SEARCH_ATSK", "result size " + SEARCH.size());
                }

                Log.d("SEARCH_ATSK", "" + i + " " + n);

            }

        }

        return null;
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub

        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub

        setListAdapter(new EfficientAdapter(Person.this, R.layout.person,
                null, null));

        super.onPostExecute(result);
    }

}

}

0 个答案:

没有答案