0

我正在尝试ListView使用可点击的列表条目。当点击一个条目时,我想要它做的是显示一条 toast 消息。但是,只要我单击一个条目,应用程序就会崩溃。我真的不明白为什么会这样。这是我的代码:

package com.andriesse.android.dailyproductivity;

import java.util.ArrayList;

import com.andriesse.android.dailyproductivity.MyDB;

public class Main extends Activity implements OnItemClickListener, OnKeyListener {

ListView listview;

MyDB mydb;
TaskAdapter myadapter;

private class MyTask {
    public MyTask(String t) {
        task = t;
    }

    public String task;
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);

    listview = (ListView) findViewById(R.id.listview);
    listview.setOnItemClickListener(this);

    mydb = new MyDB(this);
    mydb.open();

    myadapter = new TaskAdapter(this);

    listview.setAdapter(myadapter); 

}

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    String item = ((TextView)view).getText().toString();
    Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}

private class TaskAdapter extends BaseAdapter {
    private LayoutInflater layoutinflater;
    private ArrayList<MyTask> arraylist;

    public TaskAdapter(Context context) {
        layoutinflater = LayoutInflater.from(context);
        arraylist = new ArrayList<MyTask>();
        getdata();
    }

    public void getdata() {
        Cursor c = mydb.getTasks();
        startManagingCursor(c);
        if (c.moveToFirst()) {
            do {
                String task = c.getString(c
                        .getColumnIndex(Constants.TASK_NAME));
                MyTask temp = new MyTask(task);
                arraylist.add(temp);
            } while (c.moveToNext());
        }
    }

    public int getCount() {
        return arraylist.size();
    }

    public MyTask getItem(int i) {
        return arraylist.get(i);
    }

    public long getItemId(int i) {
        return i;
    }

    public View getView(int i, View view, ViewGroup group) {            
        ViewHolder holder;
        if (view == null || view.getTag() == null) {
            view = layoutinflater.inflate(R.layout.rowlayout, null);
            holder = new ViewHolder();
            holder.textview = (TextView) view.findViewById(R.id.textview);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.mytask = getItem(i);
        holder.textview.setText(holder.mytask.task);

        return view;
    }

    public class ViewHolder {
        MyTask mytask;
        TextView textview;
    }
}

private void addItem(String item) {
    if (item.length() > 0) {
        try {
            saveTaskToDB(item);
            myadapter.arraylist.add(new MyTask(item));
            myadapter.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public void saveTaskToDB(String task) {
    mydb.insertTask(task);
}

@Override
public void onDestroy() {
    super.onDestroy();
    mydb.close();
}

public boolean onKey(View v, int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    return false;
}

}

这是我的 LogCat 日志:

06-09 20:50:41.743: E/AndroidRuntime(194): Uncaught handler: thread main exiting due to uncaught exception
06-09 20:50:41.773: E/AndroidRuntime(194): java.lang.ClassCastException: android.widget.RelativeLayout
06-09 20:50:41.773: E/AndroidRuntime(194):  at com.andriesse.android.dailyproductivity.Main.onItemClick(Main.java:57)
06-09 20:50:41.773: E/AndroidRuntime(194):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
06-09 20:50:41.773: E/AndroidRuntime(194):  at android.widget.ListView.performItemClick(ListView.java:3285
06-09 20:50:41.773: E/AndroidRuntime(194):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
06-09 20:50:41.773: E/AndroidRuntime(194):  at android.os.Handler.handleCallback(Handler.java:587)
06-09 20:50:41.773: E/AndroidRuntime(194):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-09 20:50:41.773: E/AndroidRuntime(194):  at android.os.Looper.loop(Looper.java:123)
06-09 20:50:41.773: E/AndroidRuntime(194):  at android.app.ActivityThread.main(ActivityThread.java:4363)
06-09 20:50:41.773: E/AndroidRuntime(194):  at java.lang.reflect.Method.invokeNative(Native Method)
06-09 20:50:41.773: E/AndroidRuntime(194):  at java.lang.reflect.Method.invoke(Method.java:521)
06-09 20:50:41.773: E/AndroidRuntime(194):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-09 20:50:41.773: E/AndroidRuntime(194):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-09 20:50:41.773: E/AndroidRuntime(194):  at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

2

发布堆栈跟踪。如果您添加它,我们更有可能提供帮助。

我可以告诉你,虽然问题可能出在你如何尝试将视图转换为 TextView。作为参数传入的视图可能不是 TextView,它可能是某种父布局。你可能需要使用

TextView mTxt = (TextView)view.findViewById(R.id.yourEdtTxtID); 

获取对 TextView 的引用

此外,尽管您可能不应该像这样在单击侦听器中手动更改 Row 的 TextView 上的文本。如果您需要更改值,您应该使用带有数据结构的适配器,该数据结构允许您将值更改为您喜欢的任何值,一旦在您的 ListView 上设置了适配器,它将自动为您更新屏幕上的视图当您更改结构中的数据时。

于 2012-06-09T18:52:58.020 回答