开发者

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

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

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

I ran the project and it works fine, but I was try开发者_Python百科ing 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

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

Where did I make a mistake?


When you call this.setListAdapter this must extend ListActivity probably you class just extends Activity.


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



}


}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜