coolcat
coolcat

Reputation: 79

OnClick listview inside dialog fix

I cant seem to get this code to work at the moment.I would like to open a new activity with on click the list view I know this code is what am after but i have been playing about with this for days now and i just cant do it.

  public void onItemClick(AdapterView<?> parent, View view,
  int position, long id) {
    switch( position )
 {
 case 0:  Intent newActivity = new Intent(this, superleague.class);     
        startActivity(newActivity);
        break;
 case 1:  Intent newActivity = new Intent(this, youtube.class);     
        startActivity(newActivity);
        break;
  case 2:  Intent newActivity = new Intent(this, olympiakos.class);     
        startActivity(newActivity);
        break;
  case 3:  Intent newActivity = new Intent(this, karaiskaki.class);     
        startActivity(newActivity);
        break;
  case 4:  Intent newActivity = new Intent(this, reservetickets.class);     
        startActivity(newActivity);
        break;
    }
  }

main.java

  import java.util.ArrayList;

   import android.app.Activity;
   import android.app.Dialog;
   import android.content.Intent;
   import android.os.Bundle;
   import android.view.View;
   import android.widget.AdapterView;
   import android.widget.AdapterView.OnItemClickListener;
   import android.widget.ArrayAdapter;
   import android.widget.ListView;

   public class MainActivity extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    }

   public void popUp(View v){

    // Dummy list:
    ArrayList<String> dummies = new ArrayList<String>();

    dummies.add("BMW");
    dummies.add("FORD");
    dummies.add("ROVER");
    dummies.add("BMW");
    dummies.add("FORD");

    final Dialog dialog = new Dialog(MainActivity.this);
    dialog.setContentView(R.layout.customalertdialogdctivity);
    dialog.setTitle("List Title");
    ListView listView = (ListView) dialog.findViewById(R.id.list);

    ArrayAdapter<String> ad = new ArrayAdapter<String>(this, R.layout.listrow ,     
    R.id.singleItem, dummies);
    listView.setAdapter(ad);

    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            //do something on click
            dialog.dismiss();
        }
    });

    dialog.show();
   }   
  }

main.xml

   <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

  <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:onClick="popUp"
    android:text="pop dialog list" />

  </RelativeLayout>

Custom.xml

  <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >

 <ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="#b5b5b5"
    android:dividerHeight="1dp" />
 </LinearLayout>

As i said if possible i would like to open a new activity when the list view is clicked. I have looked and tried but no luck hope someone can help

Upvotes: 0

Views: 562

Answers (2)

codeMagic
codeMagic

Reputation: 44571

When you initialize your Intents, this refers to the onItemClick. You need to change it to the NameOfYourActivity.this

newActivity = new Intent(ActivityName.this, reservetickets.class); 

Also, you should be using different variables for the Intents this way but you also can simplify the way you create the Intents. This will also take care of your other issue. See this answer. It may look difficult but I think it simplifies things and makes it more portable.

Upvotes: 1

Naetmul
Naetmul

Reputation: 15552

Intent newActivity was declared many times in one block. Extract it out to declare only once.

Edited: Replace the listView.setOnItemClickListener(...) part in popup() method. As the second answer noted, you should use MainActivity.this, not this since this will refer to OnItemClickListener.this.

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
        Intent newActivity;
        switch( position )
        {
        case 0:  newActivity = new Intent(MainActivity.this, superleague.class);     
            startActivity(newActivity);
            break;
        case 1:  newActivity = new Intent(MainActivity.this, youtube.class);     
            startActivity(newActivity);
            break;
        case 2:  newActivity = new Intent(MainActivity.this, olympiakos.class);     
            startActivity(newActivity);
            break;
        case 3:  newActivity = new Intent(MainActivity.this, karaiskaki.class);     
            startActivity(newActivity);
            break;
        case 4:  newActivity = new Intent(MainActivity.this, reservetickets.class);     
            startActivity(newActivity);
            break;
        }
        dialog.dismiss();
    }
};

Upvotes: 1

Related Questions