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

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>

カスタム.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

を初期化するときはIntentsthisを参照してくださいonItemClick。NameOfYourActivity.this に変更する必要があります。

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

また、Intentsこの方法では異なる変数を使用する必要がありますが、Intents. これにより、他の問題も処理されます。この回答を参照してください。難しそうに見えるかもしれませんが、物事を単純化し、移植性を高めていると思います。

于 2013-05-31T01:49:36.167 に答える