在新活动android中获取复选框的值

时间:2016-06-18 04:52:32

标签: android-activity

我需要一些关于检索在复选框列表中选中或勾选的值的帮助。用户单击“确认”按钮后,所选项目必须出现在新活动中。 我对android非常基础,所以需要一些帮助。

FoodMainActivity.java

package com.example.sailalith.sample1;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.Toast;

public class FoodMainActivity extends ListActivity {
    String[] city= {
        "Indian bread : Rumali roti -   Rs.  20",
        "plain naan                 -   Rs.  15",
        "Paneer Butter Masala       -   Rs. 120",
        "Mushroom Curry             -   Rs. 100",
        "Jeera rice                 -   Rs. 120",
        "Egg Masala                 -   Rs.  80",
        "Green peas masala          -   Rs.  60",
        "Veg. Biryani               -   Rs. 120",
        "Dal fry                    -   Rs.  90",
        "Malai Koftha curry         -   Rs. 120",
        "Veg. Fried rice            -   Rs.  80",
        "Veg. Pulav                 -   Rs.  70",
        "   "
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.food_main);

    // -- Display mode of the ListView
    Button btn= (Button) findViewById(R.id.testbutton);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {



        }
    });

    ListView listview= getListView();
    //  listview.setChoiceMode(listview.CHOICE_MODE_NONE);
    //  listview.setChoiceMode(listview.CHOICE_MODE_SINGLE);
    listview.setChoiceMode(listview.CHOICE_MODE_MULTIPLE);

    //--    text filtering
    listview.setTextFilterEnabled(true);

    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_checked,city));
}


public void onListItemClick(ListView parent, View v, int position, long id){
    CheckedTextView item = (CheckedTextView) v;
    Toast.makeText(this, city[position] + " checked : " +
            item.isChecked(), Toast.LENGTH_SHORT).show();


   } 
}

XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
    android:id="@+id/android:list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true">
</ListView>

<Button android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/testbutton"
    android:text="Confirm"
    android:layout_alignParentBottom="true" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

onListItemClick()功能中,您将获得列表视图中所选选项的位置。您可以使用position检索所选项目。

只需在onListItemClick()函数中添加此行:

String str = listview.getItemAtPosition(position).toString();