如何将listview中的复选框值发送到android上的下一个活动

时间:2013-11-08 15:54:31

标签: checkbox

我想将listview中checkbox的值传递给下一个activity。下一个活动将是android json解析活动,它将获得checkbox的值。问题是如何传递复选框值?

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Button;
import android.widget.AdapterView.OnItemClickListener;

public class Item extends Activity implements OnClickListener{

    String [] builder;
    Button button;
    ListView listView;
    String [] array = new String[] {"Baby Cot ", "Bouncer", "Bottle", "Blanket", "Stroller", "Toy", "Walker", "Thermal Bag", "Storage Bag", "Diaper", "Pacifier", "Potty", "Baby Bath" , "Baby CD", "Baby Book", "Car Seat", "Teethers", "Cooler Bag", "High Chair", "Apparel"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, array);

       listView =  (ListView) findViewById(R.id.list);
       listView.setAdapter(adapter);
       listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

       button = (Button) findViewById (R.id.testbutton);
       button.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        SparseBooleanArray positions = listView.getCheckedItemPositions();

        for (int index = 0; index <array.length; index++){

            if (positions.get (index) == true)
            {
                builder[index] = array[index];

            }
        }

        Intent i = new Intent(this, AndroidJSONParsingActivity.class);
        Bundle  b = new Bundle();
        b.putStringArray("list", builder);
        i.putExtras(b);
        startActivity(i);


        }


        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){

        }




    }

1 个答案:

答案 0 :(得分:0)

这是将参数传递给下一个意图的方法。 (假设您的值存储在名为“checkboxValue”的变量中)

显然,将NextActivity.class更改为您的活动名称。

Intent intent = new Intent(this, NextActivity.class);
Bundle b = new Bundle();
b.putInt("key", 1); //Your checkbox value
intent.putExtras(b); //Put your checkbox to your next Intent
startActivity(intent);

在新活动中通过此代码访问它:

Bundle b = getIntent().getExtras();
int value = b.getInt("key");

我希望这会有所帮助。