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 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
It looks like a method in C#, but I can find it in the original project.
Where did I make a mistake?
ArrayAdapter is the most commonly used adapter in android. When you have a list of single type items which are stored in an array you can use ArrayAdapter. Likewise, if you have a list of phone numbers, names, or cities. ArrayAdapter has a layout with a single TextView.
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); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With