Daniel Katz
Daniel Katz

Reputation: 15

Setting Up and Displaying Dynamic Lists

I'm trying to make a notecard application. Basically, the user enters in a term and definition, and is allowed to view the data from a selection displayed in my Main activity. I'm having trouble sending the information through an intent and displaying the sent information. Since the user controls the amount of information, I've attempted to set up an arraylist and arrayadapter to maintain it. Here's my Main Activity code:

import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;

public class Main extends ListActivity {

ArrayList<String> cards;
ArrayAdapter<String> adapter;   

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

  //This will hold the new items
    cards = new ArrayList<String>();
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, cards);


    Button b = (Button) findViewById(R.id.button1);
    View footerView = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listfooter, null, false);
    this.getListView().addFooterView(footerView);

    //Set the adapter
    this.setListAdapter(adapter);

    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Main.this, NewNote.class);
            startActivityForResult(intent,0);


        }

    });


}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

 if (requestCode == 0) {

 }
        if (resultCode == RESULT_OK) {

    String s1 =(String) getIntent().getCharSequenceExtra("term");
    String s2 = (String) getIntent().getCharSequenceExtra("definition");
     cards.add(s1);
     cards.add(s2);

     if(cards != null && cards.size() > 0){
           for(int i=0;i<cards.size();i++)
            adapter.add(cards.get(i));
     }
     adapter.notifyDataSetChanged();
}
}
}

And here's my second activity that retrieves the information from the user:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class NewNote extends Activity {

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

    final EditText et1 = (EditText) findViewById(R.id.editText1);
    final EditText et2 = (EditText) findViewById(R.id.editText2);
    Button b = (Button) findViewById(R.id.button1);

    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.putExtra("term", et1.getText());
            intent.putExtra("definition", et2.getText());
            setResult(0, intent);
            finish();
        }
    });


}
}

This code does not work in sending the information through the intent back to my Main activity and adding it my my arraylist, which should be displayed. I also have no idea how to connect a listview on the layout xml files to my arraylist in the java file. I am not sure how to proceed or if I am even on the right track. Can anyone help?

Upvotes: 1

Views: 642

Answers (1)

Tomasz Gawel
Tomasz Gawel

Reputation: 8520

You have a simple mistake, in NewNote class you call setResult(0, intent); where 0 is RESULT_CANCELED and in Main class'es onActivityResult you try to handle RESULT_OK case.

call setResult(RESULT_OK, intent); and all will be ok :)

As to your comment, what kind of problem?? but try this patches anyway

in Note:

public class NewNote extends Activity {

    public static final String EXTRA_TERM = "com.yourpackage.EXTRA_NOTE_TERM";
    public static final String EXTRA_DEFINITION = "com.yourpackage.EXTRA_NOTE_DEFINITION";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog);
        final EditText et1 = (EditText) findViewById(R.id.editText1);
        final EditText et2 = (EditText) findViewById(R.id.editText2);
        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(EXTRA_TERM, et1.getText().toString());
                intent.putExtra(EXTRA_DEFINITION, et2.getText().toString());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }
}

in Main:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                final Intent intent = getIntent();
                String s;
                if((s = (String) intent.getStringExtra(NewNote.EXTRA_TERM)) != null) {
                    adapter.add(s);
                }
                if((s = (String) intent.getStringExtra(NewNote.EXTRA_DEFINITION)) != null) {
                    adapter.add(s);
                }
                if(s != null) {
                    adapter.notifyDataSetChanged();
                }
            }
        }
    }

Upvotes: 1

Related Questions