Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带复选框的Android ListView:如何捕获选中项?_Android_Checkbox_Android Listview_Android Button - Fatal编程技术网

带复选框的Android ListView:如何捕获选中项?

带复选框的Android ListView:如何捕获选中项?,android,checkbox,android-listview,android-button,Android,Checkbox,Android Listview,Android Button,我必须使用复选框执行列表视图,然后在用户按下按钮时显示选中和未选中的项目。问题是我不知道怎么做,我设法用复选框显示列表视图,代码如下: package guestCheckBox.com; import java.net.URL; import java.util.ArrayList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w

我必须使用
复选框执行
列表视图
,然后在用户按下按钮时显示选中和未选中的项目。问题是我不知道怎么做,我设法用
复选框显示
列表视图
,代码如下:

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);
    }
}

有没有人能告诉我,当用户按下按钮时,我如何显示选中和未选中的项目

我不完全理解问题所在,但我可以为您提供一个有用的ListView扩展,它可以帮助您处理字符串数据(set\get checked value):


我不完全理解问题是什么,但我可以为您提供一个有用的ListView扩展,它可以帮助您处理字符串数据(set\get checked value):


实际上,我试着在按钮上添加一些代码,当我按下按钮时,它会列出我在listview上检查的所有项目,但它不起作用。无论如何,非常感谢。我将对此进行研究:)事实上,我正试图在按钮上添加一些代码,当我按下按钮时,这些代码将列出我在listview上检查的所有项目,但它不起作用……无论如何,非常感谢。我将对此进行研究:)您可以通过以下[link][1][1]简单地获取选中项目:您可以通过以下[link][1][1]简单地获取选中项目:
<?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>
public class MultipleChoiceListView extends ListView {

public MultipleChoiceListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public MultipleChoiceListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MultipleChoiceListView(Context context) {
    super(context);
}

@Override
public void setAdapter(ListAdapter adapter) {
    throw new RuntimeException(
        "This component doesn't support custom adapter. Use setData method to supply some data to show.");
}

/**
 * Sets the data that should be displayed for choosing
 * 
 * @param data List<String>
 */
public void setData(List<String> data) {
    super.setAdapter(new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_multiple_choice, data));
    super.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}

/**
 * Sets the data that should be choosen by default
 * 
 * @param checkedData List<String>
 */
public void setCheckedData(List<String> checkedData) {
    for (int index = 0; index < getCount(); index++) {
        if (checkedData.contains(getItemAtPosition(index))) {
            setItemChecked(index, true);
        }
    }
}

/**
 * Returns checked by the user data passed in {@link #setData(List)}
 * 
 * @return List<String>
 */
public List<String> getCheckedData() {
    SparseBooleanArray checked = getCheckedItemPositions();
    List<String> checkedResult = new ArrayList<String>();

    for (int i = 0; i < checked.size(); i++) {
        if (checked.valueAt(i)) {
            checkedResult.add(getAdapter().getItem(checked.keyAt(i)).toString());
        }
    }
    return checkedResult;
}
    private void initListMultiple() {
    String[] data = new String[] {"first", "second", "third", "forth"};
    String[] checkedData = new String[] {"second", "forth"};

    multipleChoiceListView.setData(Arrays.asList(data));
    multipleChoiceListView.setCheckedData(Arrays.asList(checkedData));
}

private void onTestListButtonClicked(View view) {
    listResultTextView.setText(Arrays.toString(listView.getCheckedData().toArray()));
}