mercredi 19 avril 2017

How to display multiple checkboxes into textView in another activity

This code only manage to display only one checkbox's text on the textview on the another activity. I really appreciate if somebody could help me with this. P/S: I am a newbie.

This is my current code on MainActivity:

package com.example.ujianrisiko;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

public class MainActivity extends Activity {

//protected static final String selectedItemData = null;
Button button1;
CheckBox checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6, checkBox7, checkBox8, checkBox9, checkBox10, checkBox11, checkBox12, checkBox13, checkBox14, checkBox15, checkBox16, checkBox17;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#E71E67")));

    button1 = (Button) findViewById(R.id.button1);
    checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
    checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
    checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
    checkBox4 = (CheckBox) findViewById(R.id.checkBox4);
    checkBox5 = (CheckBox) findViewById(R.id.checkBox5);
    checkBox6 = (CheckBox) findViewById(R.id.checkBox6);
    checkBox7 = (CheckBox) findViewById(R.id.checkBox7);
    checkBox8 = (CheckBox) findViewById(R.id.checkBox8);
    checkBox9 = (CheckBox) findViewById(R.id.checkBox9);
    checkBox10 = (CheckBox) findViewById(R.id.checkBox10);
    checkBox11 = (CheckBox) findViewById(R.id.checkBox11);
    checkBox12 = (CheckBox) findViewById(R.id.checkBox12);
    checkBox13 = (CheckBox) findViewById(R.id.checkBox13);
    checkBox14 = (CheckBox) findViewById(R.id.checkBox14);
    checkBox15 = (CheckBox) findViewById(R.id.checkBox15);
    checkBox16 = (CheckBox) findViewById(R.id.checkBox16);
    checkBox17 = (CheckBox) findViewById(R.id.checkBox17);


        button1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //Start email activity using explicit intent
            //new Intent (From where, To where);

            Intent i=new Intent(MainActivity.this,AdviceActivity.class);
            i.putExtra("selected", checkBox1.getText().toString());
            startActivity(i);

        }
    });


}

}

And here is my code another activity:

package com.example.ujianrisiko;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

public class AdviceActivity extends Activity {

TextView textView1, textView2;


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


    textView1= (TextView) findViewById(R.id.textView1);
    textView2= (TextView) findViewById(R.id.textView2);

    //Bundle b = getIntent().getExtras();
    //String selection = b.getString("selection");
    String selecteditem=getIntent().getExtras().getString("selected");
    textView2.setText(selecteditem);

}

}




Aucun commentaire:

Enregistrer un commentaire