Koogund
Koogund

Reputation: 1

get a view from previous activity

I am doing a quiz app in which there are Act1 and Act2. Act1 shows the view for each question answers to select.

 public class ACT1 extends Activity 
{
 EditText question=null;
  RadioGroup choices = null;
 -------
 ------
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.question);

            /* //---get the Bundle object passed in---    
    Bundle bundle = getIntent().getExtras();

    //---get the data using the getInt() method---     
    int qId = bundle.getInt("questionIndex");

//dont know what to do here

             question = (EditText) findViewById(R.id.question);
           RadioGroup questionLayout = (RadioGroup)findViewById(R.id.answers);

 ------

    this.getQuestionView(questionNo);
    FrameLayout quizLayout = (FrameLayout) findViewById(R.id.quizLayout);
    quizLayout.setVisibility(android.view.View.VISIBLE);
    }

and in the method getQuestionView() the rest of code for getting questions and answers next submit buttons everything is there.

private void getQuestionView(questionNo)
   {
   ------
   ------
  //next and previous buttons OnClicklisteners
   ------
   private OnClickListener finishListener = new OnClickListener() 
{
    public void onClick(View v) 
    {
        Intent intent = new Intent(Act1.this,Act2.class);
           }
    }

And Act2 shows a view for results which includes a table for questionlinks. on clicking the question link the respective question view shall be shown which is from Act1 and on back button clicked it goes back to Act2. i am new to android so anybody please help. public class Act2 extends Activity { -------- ------- TableLayout questionsTable;

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


    int totalQues = Act1.getQuestions().length;
    questionsTable =(TableLayout)findViewById(R.id.questions);
    -------
            -------     
    for(int i=0;i<totalQues;i++)
    {
        ------
                  --------

        TableRow tr = new TableRow(this);

        TextView queText = new TextView(this);
               tr.addView(queText,LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
        tr.setClickable(true);
        tr.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent  intent = new Intent(this,Act1.class);
                 //---use a Bundle object to add new key/values pairs---         
                Bundle extras = new Bundle(); 
                            //here i wanna check whether 2nd question is displaying     
                extras.putInt("questionIndex",2 );                
                //---attach the Bundle object to the Intent object---       
                intent.putExtras(extras); 
                startActivity(intent);
            }
        });

thanks in advance.

Upvotes: 0

Views: 546

Answers (1)

Malcolm
Malcolm

Reputation: 41510

If I'm not mistaken, you need is to pass some data from one activity to another. This is done through the Intent class, it can contain "extras" which are actually simply key-value pairs which can be written by the calling activity and later read by the called activity.

For example, I can write the code like this:

public static final String EXTRA_QUESTION = "question";

// When you need to create the intent:
Intent intent = new Intent(this, Act2.class);
// questionId is whatever identifies the question in your code
intent.putExtra(EXTRA_QUESTION, questionId);

And in the other activity you write:

Intent intent = getIntent();
// In this example questionId is int, but it could be something else
int questionId = intent.getIntExtra(Act1.EXTRA_QUESTION, 0);

Upvotes: 1

Related Questions