所有的Android获取文本选中的复选框中的ListView [英] Android get text of all checked checkboxes in listView
问题描述
你好,我已经创造了在它的复选框一个列表视图... 但我不知道怎么去选择它们的复选框文字..这里是activity_main.xml的code
hello i have created a listview with checkboxes in it... but i dont know how to get the check box text which are selected.. here is the code of activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:orientation="vertical"
tools:context=".MygamesActivity" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="20dp" />
</LinearLayout>
另一个布局有复选框中的列表视图main.list_item.xml显示
another layout which has checkboxes to show in the listview main.list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox"
/>
</LinearLayout>
这是一个扩展arrayadapter
and this is the class that extends arrayadapter
package com.wasiff.listview;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
public class CheckboxAdapter extends ArrayAdapter<String> {
private LayoutInflater mInflater;
private String[] mStrings;
private TypedArray mIcons;
private int mViewResourceId;
public CheckboxAdapter(Context ctx,int viewResourceId,String[] strings){
super(ctx,viewResourceId,strings);
mInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mStrings = strings;
mViewResourceId = viewResourceId;
}
public int getCount(){
return mStrings.length;
}
public String getItem(int position){
return mStrings[position];
}
public long getItemId(int position){
return 0;
}
public View getView(int position,View convertView,ViewGroup parent){
convertView = mInflater.inflate(mViewResourceId, null);
CheckBox tv = (CheckBox)convertView.findViewById(R.id.checkBox1);
tv.setText(mStrings[position]);
return convertView;
}
}
这是我mainActivity类
and this is my mainActivity class
package com.wasiff.listview;
import android.app.ListActivity;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Context ctx = getApplicationContext();
Resources res = ctx.getResources();
String[] options = res.getStringArray(R.array.countrynames);
setListAdapter((ListAdapter) new CheckboxAdapter(ctx,R.layout.main_list_item,options));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
最后我都保存在一个countries.xml文件价值观文件夹中的国家
and finally i have all the countries saved in a countries.xml file on values folder
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="countrynames" translatable="false">
<item>Bhutan</item>
<item>Colombia</item>
<item>India</item>
<item>Pakistan</item>
<item>Australia</item>
<item>Srilanka</item>
<item>England</item>
</string-array>
</resources>
它显示在ListView复选框现在我想是要获取的检查,并显示在一个烤面包上的按钮点击(测试)复选框的文字我接着奥莱利在Android上菜谱的教程,但仍我不知道如何设置监听器
it shows the check boxes in the listView now what i want is to get the text of the checkboxes which are checked and show in a toast on a button click(to test) i followed the tutorial on android cookbook by oreilly but still i dont know how to set the listener
推荐答案
加入里面CheckboxAdapter.java
Add inside CheckboxAdapter.java
ArrayList<String> selectedStrings = new ArrayList<String>();
然后在里面getView方法
Then inside getView method
tv.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
selectedStrings.add(tv.getText().toString());
}else{
selectedStrings.remove(tv.getText().toString());
}
}
});
写一个getter将返回selectedStrings
Write a getter which will return selectedStrings
ArrayList<String> getSelectedString(){
return selectedStrings;
}
这篇关于所有的Android获取文本选中的复选框中的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!