“对于类型create,方法setListAdapter(ArrayAdapter)未定义”

时间:2010-06-13 20:31:08

标签: java android database

我对java& android,所以我试图从android&找到有用的样本数据库。我发现这个博客有一个项目:

http://saigeethamn.blogspot.com/2009/10/android-developer-tutorial-part-12.html

我运行该项目并且工作正常,但我正在尝试创建一个新的项目来复制&将代码粘贴到其中,这不起作用:(

我在这一行遇到了问题:

this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));

这是我得到的错误:

  

对于类型create

,方法setListAdapter(ArrayAdapter)未定义

它看起来像是C#中的方法,但我可以在原始项目中找到它。

我在哪里弄错了?

2 个答案:

答案 0 :(得分:65)

当您致电this.setListAdapter时,必须延长ListActivity,您的课程可能会延长Activity

答案 1 :(得分:1)

这段代码对我有用..

package com.Itrack.Mobile;

import java.util.ArrayList;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class listV extends ListActivity {
public SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

       // Check if the database exist  //
    db = openOrCreateDatabase(
            "itrackmobile.sqlite"
            , SQLiteDatabase.CREATE_IF_NECESSARY
            , null
            );

    try
    {
        Cursor c = db.query("basico", new String[]      
         {"_id","codigo","cantidad","fecha"},null,null,null,null,null);

        // rutina de prueba //
                ArrayList<String> mArrayList = new ArrayList<String>();
                c.moveToFirst();
                while(!c.isAfterLast()) {
                     mArrayList.add("ID: " +c.getString(c.getColumnIndex("_id")) + 
           "\nCodigo : " + c.getString(c.getColumnIndex("codigo")) + "\nCantidad : " 
           + c.getString(c.getColumnIndex("cantidad")) + "\nFecha : " +     
           c.getString(c.getColumnIndex("fecha")) );
                      c.moveToNext();
             }

                setListAdapter( new ArrayAdapter<String>  
               (this,R.layout.single_item,mArrayList));
                ListView  list  = getListView();
                list.setTextFilterEnabled(true);
                list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {

                     Toast.makeText(getApplicationContext(), ((TextView) 
                      arg1).getText(), Toast.LENGTH_SHORT).show();
                    }

                });
    }
    catch (RuntimeException e)
    {
        Log.e("basico", e.toString(), e);
    }



}


}