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

This is how you pass parameters to the next intent. (Assuming your value was stored in a variable named "checkboxValue")

Obviously, change NextActivity.class to your activity name.

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

Access it by this code in your new activity:

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

I hope this helps.

于 2013-11-08T16:00:21.950 に答える