0

我现在似乎无法让这段代码工作。我想通过单击列表视图打开一个新活动做。

  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();
   }   
  }

主要的.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>

自定义.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>

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

4

2 回答 2

1

Intent 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();
    }
};
于 2013-05-31T01:38:20.523 回答
1

当你初始化你的Intents,this指的是onItemClick. 您需要将其更改为 NameOfYourActivity.this

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

此外,您应该Intents为此使用不同的变量,但您也可以简化创建Intents. 这也将解决您的其他问题。看到这个答案。它可能看起来很困难,但我认为它简化了事情并使其更便携。

于 2013-05-31T01:49:36.167 回答