0

我想将列表视图中复选框的值传递给下一个活动。下一个活动将是 android json 解析活动,它将获取复选框的值。问题是如何传递复选框值?

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){

        }




    }
4

1 回答 1

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

我希望这有帮助。

于 2013-11-08T16:00:21.950 回答