如何在edittext中获取列表选定项目?
Posted
技术标签:
【中文标题】如何在edittext中获取列表选定项目?【英文标题】:How to get list selected item in edittext? 【发布时间】:2015-01-23 00:15:19 【问题描述】:我是android新手,我在右侧有一个带有imageicon的edittext,单击该图标后,下一个活动将打开,我正在使用带有复选框的listview,所以问题是我想在上一页上获取listview的所有选定项目编辑文本,任何机构都可以帮忙吗?谢谢
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();
);
【问题讨论】:
在该列表的 onClickLister 中做你的事情 我知道在哪里做,但不知道该怎么做 【参考方案1】:您需要将该数据保存在数组或其他数据结构中。
例子:
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()
将字符串数组传递给另一个活动
intent.putExtra("selectionArray", selectionArray );
希望这会有所帮助。
【讨论】:
它显示错误,而我按照你所说的设置 if 条件 isChecked 会显示错误。您需要编写代码来检查它。等等,给你看例子。 但为此我需要适配器类,我将在哪里拥有这个 你有你的适配器类。只需在其中进行更改【参考方案2】:在Activity1
中,当您单击编辑文本图标时,您可以使用 startActivityforResult 开始您的活动,这样您就可以像这样获取 onActivityResult 方法的结果..
Intent 意图 = new Intent(Activity1.this, Activity2.class); startActivityForResult(intent, 0);
在同一个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..
-
在 activity2 更新ArrayList时,母亲宣传母亲检查/未选中...
然后单击确定按钮,以这种方式完成您的活动..
Intent intent = new Intent();
intent.putExtra(key, your_arraylist);
setResult(RESULT_OK, intent);
finish();
这将在您的 Activity1 的 onActivityResult() 方法中发回数据
【讨论】:
你能告诉如何检查和取消检查 以及我将edittext与片段一起扩展的地方以上是关于如何在edittext中获取列表选定项目?的主要内容,如果未能解决你的问题,请参考以下文章
如何将一个自定义列表的选定数据显示到同一活动的另一个自定义列表视图
如何从自定义列表视图中获取选定项目并在 toast 消息中打印?