As ionel mentioned, you should change your activity's name to
something other than ListView. Maybe something like SelectAnion.

The other thing is that you don't really want to call setContentView
in the onListItemSelect. Instead, you probably should use
onListItemClick to create a new Activity and launch it. Then call
setContentView in the onCreate method of the new Activity.

That's a bit more complicated, but it is more in line with the way the
framework should be used. You can pass the selected item in a Bundle
on the Intent's extras. Your code would look something like:

 protected void onListItemClick(ListView l, View v, int position, long
id)
    {
          Intent anionDetailIntent =  new Intent(this,
AnionDetail.class);
          anionDetailIntent.putExtra("position", position);
          startActivity(anionDetailIntent);
  };

You'll have to build a new class that extends Activity (or a subclass
of Activity) - AnionDetail would be the name of that class in the
above example. Then, in that class, you could fetch the itemSelected
from the Intent's Bundle and implement your setContentView in the
onCreate method of that class:


public class AnionDetail extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int position = getIntent().getExtras().getString("position");

   if (position == 0){
        setContentView(R.layout.acetate);
        };
    if (position == 1){
        setContentView(R.layout.bromine);
        };
    if (position == 2){
        setContentView(R.layout.carbonate);
        };
    if (position == 3){
        setContentView(R.layout.chlorate);
        };

     }


I am away fom my dev machine at the moment, so I haven't tested this
code. Caveat emptor!


On Apr 10, 5:16 pm, Amon Darthir <[email protected]> wrote:
> Hi. I am having trouble figuring out how to get my onListItemClick to
> work. Here is my code.
>
> package list.view;
>
> import android.app.ListActivity;
> import android.os.Bundle;
> import android.view.View;
> import android.widget.ArrayAdapter;
>
> public class ListView extends ListActivity {
>     /** Called when the activity is first created. */
>     @Override
>     public void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState);
>
>         setListAdapter(new ArrayAdapter<String>(this,
>         android.R.layout.simple_list_item_1, ANIONS));
>         }
>
>     public static final String[] ANIONS = new String[] {
>         "Acetate", "Bromine", "Carbonate", "Chlorate", "Chloride",
>         "Chlorite", "Chromate", "Cyanide", "Dychromate",
>     };
>
>     protected void onListItemClick(ListView l, View v, int position,
> long id)
>     {
>     if (position == 0){
>         setContentView(R.layout.acetate);
>         };
>     if (position == 1){
>         setContentView(R.layout.bromine);
>         };
>     if (position == 2){
>         setContentView(R.layout.carbonate);
>         };
>     if (position == 3){
>         setContentView(R.layout.chlorate);
>         };
>                                                 };
>
> }
>
> It seems like it should work but it never brings up the new .xml. Any
> suggestions would be nice. Thanks.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

To unsubscribe, reply using "remove me" as the subject.

Reply via email to