OnClick listview里面的对话框修复

时间:2013-05-31 01:22:58

标签: android listview

我似乎无法让此代码工作。我想打开一个新的活动,点击列表视图我知道这个代码是什么,但我已经玩了几天,现在和我不能这样做。

  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>

正如我所说,如果可能的话,我想在点击列表视图时打开一个新活动。 我已经看过并试过但没有运气希望有人可以提供帮助

2 个答案:

答案 0 :(得分:1)

意图newActivity在一个块中多次声明。将其解压缩以仅声明一次。

已修改:替换listView.setOnItemClickListener(...)方法中的popup()部分。如第二个答案所述,您应该使用MainActivity.this,而不是this,因为this会引用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();
    }
};

答案 1 :(得分:1)

初始化Intents时,this引用onItemClick。您需要将其更改为NameOfYourActivity.this

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

此外,您应该以{{1​​}}方式使用不同的变量,但您也可以简化创建Intents的方式。这也将照顾您的其他问题。见this answer。它可能看起来很难,但我认为它简化了事情并使其更具可移植性。