0

我的代码有问题 iv 一直在尝试修复此错误,但仍然没有运气。我想在单击列表视图中的项目时开始一个新活动我找了一段时间发现了这个代码

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

现在我把它放在我的项目中,问题出在哪里,但由于我是新手,所以需要一些时间来解决小问题,希望你能提供帮助?

 package com.mybasicapp;

  import android.app.Dialog;
  import android.app.ListActivity;
  import android.content.DialogInterface;
  import android.content.DialogInterface.OnCancelListener;
  import android.content.DialogInterface.OnDismissListener;
  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.Button;
  import android.widget.ImageButton;
  import android.widget.ListView;
  import android.widget.Toast;

  public class menu extends ListActivity implements onListItemClickListner{


  ImageButton ImagebuttonOpenDialog;

   String KEY_TEXTPSS = "TEXTPSS";
   static final int CUSTOM_DIALOG_ID = 0;

    ListView dialog_ListView;

    String[] listContent = {
 "FORD", "BMW", "AUDI", "JAGUAR",
 "MERCEDES", "RENAULT", "ROVER", "SAAB", "BENTLEY",
 "VOLVO", "VAUXHALL", "SEAT"};


  protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

  ListView lv = getListView();
  lv.setTextFilterEnabled(true);

  lv.setOnListItemClickListener(new OnListItemClickListener() {
  public void onListItemClick(AdapterView<?> parent, View view,
          int position, long id) {
        switch( position )
        {
           case 0:  Intent newActivity = new Intent(this, 
         Pickmodel.class);     
                    startActivity(newActivity);
                    break;
           case 1:  Intent newActivity = new Intent(this, 
         Pickmodel.class);     
                    startActivity(newActivity);
                    break;
           case 2:  Intent newActivity = new Intent(this, 
         Pickmodel.class);     
                    startActivity(newActivity);
                    break;
           case 3:  Intent newActivity = new Intent(this, 
         Pickmodel.class);     
                    startActivity(newActivity);
                    break;
           case 4:  Intent newActivity = new Intent(this, 
         Pickmodel.class);     
                    startActivity(newActivity);
                    break;
        }
      }




   ImagebuttonOpenDialog = (ImageButton)findViewById(R.id.imgbtn1);
   ImagebuttonOpenDialog.setOnClickListener(new Button.OnClickListener(){

    public void onClick(View arg0) {
        showDialog(CUSTOM_DIALOG_ID);
       }});

        }

     @Override
     protected Dialog onCreateDialog(int id) {

      Dialog dialog = null;

      switch(id) {
         case CUSTOM_DIALOG_ID:
          dialog = new Dialog(menu.this);
          dialog.setContentView(R.layout.custom);
          dialog.setTitle("Custom Dialog");

          dialog.setCancelable(true);
          dialog.setCanceledOnTouchOutside(true);

          dialog.setOnCancelListener(new OnCancelListener(){

        public void onCancel(DialogInterface dialog) {
         // TODO Auto-generated method stub
         Toast.makeText(menu.this,
           "OnCancelListener",
           Toast.LENGTH_LONG).show();
        }});

          dialog.setOnDismissListener(new OnDismissListener(){

        public void onDismiss(DialogInterface dialog) {
         // TODO Auto-generated method stub
         Toast.makeText(menu.this,
           "OnDismissListener",
           Toast.LENGTH_LONG).show();
        }});

          //Prepare ListView in dialog
          dialog_ListView = (ListView)dialog.findViewById(R.id.dialoglist);
          ArrayAdapter<String> adapter
           = new ArrayAdapter<String>(this,
             android.R.layout.simple_list_item_1, listContent);
          dialog_ListView.setAdapter(adapter);
          dialog_ListView.setOnItemClickListener(new OnItemClickListener(){

        public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
         // TODO Auto-generated method stub
         Toast.makeText(menu.this,
           parent.getItemAtPosition(position).toString() + " clicked",
           Toast.LENGTH_LONG).show();
         dismissDialog(CUSTOM_DIALOG_ID);
        }});

             break;
         }

      return dialog;
     }

     @Override
     protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
      // TODO Auto-generated method stub
      super.onPrepareDialog(id, dialog, bundle);

      switch(id) {
         case CUSTOM_DIALOG_ID:
          //
             break;
         }

     }

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub

    }

    }


    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
     // TODO Auto-generated method stub

      }

请让我知道是否遗漏了什么也谢谢 ps 我不能运行这个项目所以我不能打印日志猫

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/customdialog"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:padding="20dp"
  android:minWidth="300dp">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/popcar" />

<ListView
    android:id="@+id/dialoglist"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/kj" />

</LinearLayout>
4

2 回答 2

0

由于您正在扩展 ListActivity,因此 onItemClickListener 将不起作用。你必须使用 onListItemClickListner 然后它才会起作用。

于 2013-05-20T19:14:15.340 回答
0

您正在使用 ListActivity 但您的视图中没有任何列表。

你应该有一个元素

<ListView
  android:id="@android:id/list"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >
</ListView> 

在您的布局中,以便能够使用 ListActivity。

但看起来你已经削减了一些 xml,因为我没有看到关闭 RelativeLayout 标记

于 2013-05-20T23:32:41.373 回答