带复选框的 Android ListView:如何捕获选中的项目?
我必须使用 CheckBoxes
执行 ListView
,然后在用户按下按钮时显示选中和未选中的项目。问题是我不知道该怎么做,我设法用以下代码显示 ListView
和 CheckBox
:
package guestCheckBox.com;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class GuestCheckBox extends Activity {
private ListView lView;
ArrayList<String> guest= new ArrayList <String>();
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
lView = (ListView) findViewById(R.id.ListView01);
try{
URL url = new URL(
"http://tanjungrhu.jigsy.com/files/documents/Check.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList words = doc.getElementsByTagName("Guest");
for(int i=0;i<words.getLength();i++){
Node node = words.item(i);
Element fstElmnt = (Element) node;
NodeList NameList = fstElmnt.getElementsByTagName("Name");
Element NameElement = (Element) NameList.item(0);
NameList = NameElement.getChildNodes();
guest.add((((Node) NameList.item(0)).getNodeValue()));
}
}
catch (Throwable t){
Toast.makeText(this, "Exeption:" + t.toString(), 2000).show();
}
//Set option as Multiple Choice. So that user can able to select more the one option from list
lView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, guest));
lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}
这里是我的 main.xml< /code>:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
有人会告诉我如何执行按钮以及当用户按下该按钮时如何显示选中和未选中的项目吗?
I have to do a ListView
with CheckBoxes
and then display the checked and unchecked items when user presses the button. The problem is I don't know how to do it, I manage to display the ListView
with the CheckBox
with this code:
package guestCheckBox.com;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class GuestCheckBox extends Activity {
private ListView lView;
ArrayList<String> guest= new ArrayList <String>();
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
lView = (ListView) findViewById(R.id.ListView01);
try{
URL url = new URL(
"http://tanjungrhu.jigsy.com/files/documents/Check.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList words = doc.getElementsByTagName("Guest");
for(int i=0;i<words.getLength();i++){
Node node = words.item(i);
Element fstElmnt = (Element) node;
NodeList NameList = fstElmnt.getElementsByTagName("Name");
Element NameElement = (Element) NameList.item(0);
NameList = NameElement.getChildNodes();
guest.add((((Node) NameList.item(0)).getNodeValue()));
}
}
catch (Throwable t){
Toast.makeText(this, "Exeption:" + t.toString(), 2000).show();
}
//Set option as Multiple Choice. So that user can able to select more the one option from list
lView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, guest));
lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}
and here my main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
Would somebody show me how can I do a buttons and how will I show the checked and unchecked items when the user presses that buttons?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不完全明白问题是什么,但我可以为您提供 ListView 的一个有用的扩展,它可以帮助您操作字符串数据(设置\获取检查值):
}
使用示例:
I didn't completely understand what the problem is but I can provide you one useful extension of ListView which helps you manipulate with string data (set\get checked values):
}
Example of using: