1

在我尝试添加搜索栏之前,我有一个运行良好的 ListView。我确实必须重新格式化活动并使用不同类型的数组,所以也可能是这样。基本上,当我尝试在模拟器上单击 ListView 中的一个项目时,它会显示“不幸的是,myapp 已停止”。我会发布代码,希望我在某个容易修复的地方犯了一个愚蠢的错误。

SecondScreenActivity.java(ListView 活动)

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

public class SecondScreenActivity extends Activity
{
// List view
private ListView lv;

// Listview Adapter
ArrayAdapter<String> adapter;

// Search EditText
EditText inputSearch;


// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;

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

    // Listview Data
    String products[] = {"Recipe1", "Recipe2", "Recipe3", "Recipe4", "Recipe5",
                         "Recipe6", "Recipe7", "Recipe8", "Recipe9", "Recipe10"};

    lv = (ListView) findViewById(R.id.mainListView);
    inputSearch = (EditText) findViewById(R.id.inputSearch);

    // Adding items to listview
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
    lv.setAdapter(adapter);

    // Enabling search filter
    inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            SecondScreenActivity.this.adapter.getFilter().filter(cs);   
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub                          
        }
    });

 // register onClickListener to handle click events on each item
    lv.setOnItemClickListener(new OnItemClickListener()
       {
                // argument position gives the index of item which is clicked
            public void onItemClick(AdapterView<?> parent, View itemClicked,
            int position, long id) {

        TextView textView = (TextView) itemClicked;
        String strText = textView.getText().toString();

        Intent intenttwo = new Intent(SecondScreenActivity.this, ThirdScreenActivity.class);
        intenttwo.putExtra("position", strText);
        startActivity(intenttwo);
    }
    });
}
}

ThirdScreenActivity.java(应在单击项目时打开)

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class ThirdScreenActivity extends Activity {

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

    //Sets the title to recipe name
    TextView t = ((TextView)findViewById(R.id.textviewPosition));

    Intent intent = getIntent();
    String position = intent.getStringExtra("position");

    t.setText(position);  

    //Sets the correct recipe
if ("Recipe1".equals(position)) {
    TextView RecipeTextView = ((TextView)findViewById(R.id.RecipeTextView));

    RecipeTextView.setText(R.string.recipe1);  
    }

else if ("Recipe2".equals(position)) {
    TextView RecipeTextView = ((TextView)findViewById(R.id.RecipeTextView));

    RecipeTextView.setText(R.string.recipe2); 
    }

else if ("Recipe3".equals(position)) {
    TextView RecipeTextView = ((TextView)findViewById(R.id.RecipeTextView));

    RecipeTextView.setText(R.string.recipe3); 
    }

else if ("Recipe4".equals(position)) {
    TextView RecipeTextView = ((TextView)findViewById(R.id.RecipeTextView));

    RecipeTextView.setText(R.string.recipe4); 
    }

else if ("Recipe5".equals(position)) {
    TextView RecipeTextView = ((TextView)findViewById(R.id.RecipeTextView));

    RecipeTextView.setText(R.string.recipe5); 
    }

else if ("Recipe6".equals(position)) {
    TextView RecipeTextView = ((TextView)findViewById(R.id.RecipeTextView));

    RecipeTextView.setText(R.string.recipe6); 
    }

else if ("Recipe7".equals(position)) {
    TextView RecipeTextView = ((TextView)findViewById(R.id.RecipeTextView));

    RecipeTextView.setText(R.string.recipe7); 
    }

else if ("Recipe8".equals(position)) {
    TextView RecipeTextView = ((TextView)findViewById(R.id.RecipeTextView));

    RecipeTextView.setText(R.string.recipe8); 
    }

else if ("Recipe9".equals(position)) {
    TextView RecipeTextView = ((TextView)findViewById(R.id.RecipeTextView));

    RecipeTextView.setText(R.string.recipe9); 
    }

else if ("Recipe10".equals(position)) {
    TextView RecipeTextView = ((TextView)findViewById(R.id.RecipeTextView));

    RecipeTextView.setText(R.string.recipe10); 
    }
}   

Main.xml(ListView 布局)

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:text="Recipes"
    android:textColor="#33ccff"
    android:textSize="40sp"
    android:gravity="center_vertical|center_horizontal"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
     android:id="@+id/inputSearch"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Search recipes..."
    android:inputType="textVisiblePassword"/>

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

</LinearLayout>

screen2.xml(应在项目单击时打开的页面布局)

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

<TextView
    android:id="@+id/textviewPosition"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:textSize="40sp"
    android:textColor="#33ccff"
    android:gravity="center" />

<ScrollView
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">

<TextView
    android:id="@+id/RecipeTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="20dp"
    android:textColor="#000000"
    android:textSize="15sp" />
</ScrollView>
</LinearLayout>

最后是logcat。

06-24 23:11:33.350: E/AndroidRuntime(939): FATAL EXCEPTION: main
06-24 23:11:33.350: E/AndroidRuntime(939): java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
06-24 23:11:33.350: E/AndroidRuntime(939):  at com.leopenrose.cookbook.SecondScreenActivity$2.onItemClick(SecondScreenActivity.java:80)
06-24 23:11:33.350: E/AndroidRuntime(939):  at android.widget.AdapterView.performItemClick(AdapterView.java:298)
06-24 23:11:33.350: E/AndroidRuntime(939):  at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
06-24 23:11:33.350: E/AndroidRuntime(939):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
06-24 23:11:33.350: E/AndroidRuntime(939):  at android.widget.AbsListView$1.run(AbsListView.java:3423)
06-24 23:11:33.350: E/AndroidRuntime(939):  at android.os.Handler.handleCallback(Handler.java:725)
06-24 23:11:33.350: E/AndroidRuntime(939):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-24 23:11:33.350: E/AndroidRuntime(939):  at android.os.Looper.loop(Looper.java:137)
06-24 23:11:33.350: E/AndroidRuntime(939):  at android.app.ActivityThread.main(ActivityThread.java:5041)
06-24 23:11:33.350: E/AndroidRuntime(939):  at java.lang.reflect.Method.invokeNative(Native Method)
06-24 23:11:33.350: E/AndroidRuntime(939):  at java.lang.reflect.Method.invoke(Method.java:511)
06-24 23:11:33.350: E/AndroidRuntime(939):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-24 23:11:33.350: E/AndroidRuntime(939):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-24 23:11:33.350: E/AndroidRuntime(939):  at dalvik.system.NativeStart.main(Native Method)

我在 logcat 中看到了致命的异常,但我真的不知道该怎么办!我很抱歉发布了这么多代码,但我认为没有任何可能没有帮助。谢谢您的帮助!

4

1 回答 1

0

列表中的每个项目都是 LinearLayout 类型。在您的 onItemClick 中,您将其类型转换为 TextView,因此是 ClassCastException。以下代码应该可以工作

 lv.setOnItemClickListener(new OnItemClickListener()
   {
            // argument position gives the index of item which is clicked
        public void onItemClick(AdapterView<?> parent, View itemClicked,
        int position, long id) {

    LinearLayout view = (LinearLayout) itemClicked;
    TextView textView = (TextView)view.findViewById(R.id.textViewPosition);
    String strText = textView.getText().toString();

    Intent intenttwo = new Intent(SecondScreenActivity.this, ThirdScreenActivity.class);
    intenttwo.putExtra("position", strText);
    startActivity(intenttwo);
}
});
于 2013-08-02T20:50:02.097 回答