如何在edittext中获取列表选择项?

时间:2014-11-25 05:03:12

标签: android checkbox android-listview

enter image description here enter image description here 我是android的新手,我在右侧有一个带有imageicon的edittext,点击该图标后,下一个活动将打开,在那里我使用listview和复选框,所以问题是我想在前一页上获取listview的所有选定项目& #39; s edittext,任何身体都可以帮忙吗?感谢

MyCustomAdapter dataAdapter = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_view_religionlist);

    // Generate list View from ArrayList
    displayListView();

    checkButtonClick();

}

private void displayListView() {

    // Array list of countries
    ArrayList<MotherTongues> stateList = new ArrayList<MotherTongues>();

    MotherTongues _states = new MotherTongues("Sikh", false);
    stateList.add(_states);
    _states = new MotherTongues("Parsi", true);
    stateList.add(_states);
    _states = new MotherTongues("Brahmin", false);
    stateList.add(_states);
    _states = new MotherTongues("Patel", true);
    stateList.add(_states);
    _states = new MotherTongues("Baniya", true);
    stateList.add(_states);
    _states = new MotherTongues("Rajput", false);
    stateList.add(_states);
    _states = new MotherTongues("Christian", false);
    stateList.add(_states);
    _states = new MotherTongues("Darji", false);
    stateList.add(_states);
    _states = new MotherTongues("Muslim", false);
    stateList.add(_states);

    // create an ArrayAdaptar from the String Array
    dataAdapter = new MyCustomAdapter(this, R.layout.list_item_religionlist, stateList);
    ListView listView = (ListView) findViewById(R.id.listView1);
    // Assign adapter to ListView
    listView.setAdapter(dataAdapter);

    listView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // When clicked, show a toast with the TextView text
            MotherTongues state = (MotherTongues) parent.getItemAtPosition(position);
            Toast.makeText(getApplicationContext(),
                    "Clicked on : " + state.getName(), Toast.LENGTH_LONG)
                    .show();
        }
    });
}

private class MyCustomAdapter extends ArrayAdapter<MotherTongues> {

    private ArrayList<MotherTongues> stateList;

    public MyCustomAdapter(Context context, int textViewResourceId,

    ArrayList<MotherTongues> stateList) {
        super(context, textViewResourceId, stateList);
        this.stateList = new ArrayList<MotherTongues>();
        this.stateList.addAll(stateList);
    }

    private class ViewHolder {
        TextView code;
        CheckBox name;
    }

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

        ViewHolder holder = null;

        Log.v("ConvertView", String.valueOf(position));

        if (convertView == null) {

            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = vi.inflate(R.layout.list_item_mothertongue, null);

            holder = new ViewHolder();
            holder.code = (TextView) convertView.findViewById(R.id.code);
            holder.name = (CheckBox) convertView
                    .findViewById(R.id.checkBox1);

            convertView.setTag(holder);

            holder.name.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    MotherTongues _state = (MotherTongues) cb.getTag();

                    Toast.makeText(
                            getApplicationContext(),
                            "Checkbox: " + cb.getText() + " -> "
                                    + cb.isChecked(), Toast.LENGTH_LONG)
                            .show();

                    _state.setSelected(cb.isChecked());
                }
            });

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

        MotherTongues state = stateList.get(position);

        //holder.code.setText(" (" + state.getCode() + ")");
        holder.name.setText(state.getName());
        holder.name.setChecked(state.isSelected());

        holder.name.setTag(state);

        return convertView;
    }

}

private void checkButtonClick() {

    Button myButton = (Button) findViewById(R.id.findSelected);

    myButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            StringBuffer responseText = new StringBuffer();
            responseText.append("Selected Languages are...\n");

            ArrayList<MotherTongues> stateList = dataAdapter.stateList;

            for (int i = 0; i < stateList.size(); i++) {
                MotherTongues state = stateList.get(i);

                if (state.isSelected()) {
                    responseText.append("\n" + state.getName());
                    Intent intent=new Intent(ReligionList.this,PhotosFragment.class);
                    startActivity(intent);

                }
            }

            Toast.makeText(getApplicationContext(), responseText,
                    Toast.LENGTH_SHORT).show();
        }
    });
}

}

2 个答案:

答案 0 :(得分:0)

您需要将该数据保存在数组或其他一些数据结构中。

示例:

List<String> selectionList =new ArrayList<String>();
listView.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        // When clicked, show a toast with the TextView text
        MotherTongues state = (MotherTongues) parent.getItemAtPosition(position);
        Toast.makeText(getApplicationContext(),
                "Clicked on : " + state.getName(), Toast.LENGTH_LONG)
                .show();

        //check if its checkbox is checked, and if so add from list else remove from list
        if(isChecked)
            selectionList.add(state.getName());
        else
            selectionList.remove(state.getName());
    }
});

最后在您点击按钮或您想要发回数据的任何地方:

String [] selectionArray = selectionList.toArray(new String[selectionList.size()]);

您可以使用intent.putExtra()

将String数组传递给另一个活动
intent.putExtra("selectionArray", selectionArray );

希望这有帮助。

答案 1 :(得分:0)

  1. Activity1单击编辑文本图标时,您可以使用startActivityforResult启动活动,以便您可以像这样返回结果onActivityResult方法..

    Intent intent = new Intent(Activity1.this,Activity2.class);       startActivityForResult(intent,0);

  2. 在相同的Activity1中添加

             @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);
    
    
    
    
                 if (requestCode==0&&resultCode == RESULT_OK) {
    
                //recieve the arraylist of mother tounges sent from Activity2 with values set to true       for checked and false for unchecked...like this 
    
              ` data.getExtras().getStringArray(key);`
    
    
               //refresh your list view using arraylist recieved here from Activit2..
    
    
    
                }
         }
    
    1. 在您的 Activity2 更新arraylist时,如果检查/取消选中母亲的话,则会使用true / false值...
    2. 点击确定按钮,以这种方式完成您的活动..

       Intent intent = new Intent();
                  intent.putExtra(key, your_arraylist);
                  setResult(RESULT_OK, intent);
                  finish();
      

      这将发回Activity1的onActivityResult()方法中的数据