Java “未定义类型创建的方法 setListAdapter(ArrayAdapter)”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3033791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:49:08  来源:igfitidea点击:

"The method setListAdapter(ArrayAdapter) is undefined for the type create"

javaandroiddatabase

提问by Nezir

I am completely new to java & android, so I tried to find useful samples from android & databases. I found this blog with a project:

我对 java 和 android 完全陌生,所以我试图从 android 和数据库中找到有用的示例。我发现这个博客有一个项目:

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

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

I ran the project and it works fine, but I was trying to create a new project to copy & paste the code in it and this is not working :(

我运行了这个项目,它运行良好,但我试图创建一个新项目来复制和粘贴代码,但这是行不通的 :(

I had a problem on this line:

我在这条线上遇到了问题:

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

This is the error I get:

这是我得到的错误:

The method setListAdapter(ArrayAdapter) is undefined for the type create

未定义类型 create 的 setListAdapter(ArrayAdapter) 方法

It looks like a method in C#, but I can find it in the original project.

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

Where did I make a mistake?

我在哪里做错了?

采纳答案by Pentium10

When you call this.setListAdapterthis must extend ListActivityprobably you class just extends Activity.

当您调用this.setListAdapterthis must extend 时,ListActivity您的类可能只是 extends Activity

回答by raabsoft

this code work for me..

这段代码对我有用..

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



}


}