i have a listview and its data values is separated by blocks( Item1, Item2...) but i want to know how to show (Item 1, Sub Item 1...) ?
well here is the code that shows only the Item, so how do i show the Item and the Sub Item?
code:
//LISTVIEW database CONTATO
ListView user = (ListView) findViewById(R.id.lvShowContatos);
//String = simple value ||| String[] = multiple values/columns
String[] campos = new String[] {"nome", "telefone"};
list = new ArrayList<String>();
Cursor c = db.query( "contatos", campos, null, null, null, null, "nome" + " ASC ");
c.moveToFirst();
String lista = "";
if(c.getCount() > 0) {
while(true) {
list.add(c.getString(c.getColumnIndex("nome")).toString());
if(!c.moveToNext()) break;
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
user.setAdapter(adapter);
the code i putted there but still gives me errors and i dont see where it gets the values from yet.
//LISTVIEW database CONTATO
ListView user = (ListView) findViewById(R.id.lvShowContatos);
//String = simple value ||| String[] = multiple values/columns
String[] campos = new String[] {"nome", "telefone"};
ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String,Object>>();
HashMap<String, Object> listItem;
Cursor c = db.query( "contatos", campos, null, null, null, null, "nome" + " ASC ");
c.moveToFirst();
listItem = new HashMap<String, Object>();
listItem.put("nome", "your_item_text");
listItem.put("telefone", "your_subitem_text");
items.add(listItem);
adapter = new SimpleAdapter(this, items, R.layout.custom_list_layout, new String[]{"item", "subitem"}, new int[]{R.id.text_item, R.id.text_subitem});
user.setAdapter(adapter);
Use SimpleAdapter, not the ArrayAdapter to populate the ListView: http://developer.android.com/reference/android/widget/SimpleAdapter.html
There you will have an array of fields to populate for each item, like:
ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String,Object>>();
HashMap<String, Object> listItem;
listItem = new HashMap<String, Object>();
listItem.put("item", "your_item_text");
listItem.put("subitem", "your_subitem_text");
items.add(listItem);
adapter = new SimpleAdapter(this, items, R.layout.custom_list_layout, new String[]{"item", "subitem"}, new int[]{R.id.text_item, R.id.text_subitem});
listview.setAdapter(adapter);
Remember also to create custom_list_layout.xml for your ListView items, and make sure it contains textViews with proper ids:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/text_item"
... />
<TextView
android:id="#+id/text_subitem"
... />
</RelativeLayout>
Look at ExpandableListView, that is exactly for this user case.
You will have to create your own custom adapter extending either the ArrayAdapter or the BaseAdapter and you will also have to create an xml layout for your items in the list.
If you are new to ListView go through this tutorial on ListView.
Related
Im trying to populate a listview from a list, this is the method that create my list:
public List<produto> buscarProdutoPorNome(String nome){
List<produto> lista = new ArrayList<produto>();
String[] columns = new String[]{
"upc", "nome", "descricao", "qtd", "nec"};
String[] args = new String[]{nome+"%"};
db = dbHelper.getWritableDatabase();
Cursor c = db.query("produto", columns,
"nome like ?", args, null, null, "nome");
c.moveToFirst();
while(!c.isAfterLast()){
produto p = fillProduto(c);
lista.add(p);
c.moveToNext();
}
c.close();
db.close();
return lista;
}
Im wanna list, but i dont know how, someone can help me?
List<produto>lista = produtosDB.buscarProdutoPorNome("");
Why not convert the List into an array as:
producto[] array = lista.toArray(new producto[lista.size()]);
and then populate to the ListView as
setListAdapter(new ArrayAdapter<producto>(this, R.layout.list_item, array));
List<String> array = new ArrayList<String>();
// Populate the array list from the database
final ListView list = (ListView) findViewById(R.id.listview);
final ArrayAdapter<String> = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, array);
list.setAdapter(adapter);
This is the simplest way to do it, but will work for a string list only. To have an adapter with your custom class list you will need to create a custom adapter class and a custom view layout in res/layout. Find more info on custom adapters here:
http://www.vogella.com/tutorials/AndroidListView/article.html#adapterown
I am new with android..I am learning work with database files..I have a simplecurseadapter like this
Cursor c = db.query(TABLE_NAME,columns,null, null, null, null, COLUMN_ID);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_item, c,
new String[] {COLUMN_ID,COLUMN_NAME,COLUMN_order}, new int[] {R.id.list_item_text_id,R.id.list_item_text_main,R.id.list_item_text_sub}, 0);
ListView list = (ListView) findViewById(R.id.list_poet_name);
list.setAdapter(adapter);
I want to know Is there any way to make a custom listview?my column_order has just to value 0,1 and I want rows with 0 value shows in R.id.list_item_text_main and if its value is 1 it shows in R.id.list_item_text_sub Can I define an if statement in SimpleCursorAdapter?I yes How?If no..Whats the best way to do it?
Make your query like this
Cursor c = db.query(TABLE_NAME,columns,COLUMN_order + "=? OR " + COLUMN_order + "=?", new String[]{"0", "1"}, null, null, COLUMN_ID);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_item, c,
new String[] {COLUMN_ID,COLUMN_NAME,COLUMN_order}, new int[] {R.id.list_item_text_id,R.id.list_item_text_main,R.id.list_item_text_sub}, 0);
ListView list = (ListView) findViewById(R.id.list_poet_name);
list.setAdapter(adapter);
I think I find my answer...
http://enjoyandroid.wordpress.com/2012/03/12/customizing-simple-cursor-adapter/
I am working on it..Maybe I am wrong
I've populated my spinner as this:
DatabaseConnector dbConnector = new DatabaseConnector(this);
dbConnector.open();
Cursor c = dbConnector.raw("SELECT _id, nombrecasa FROM casa");
startManagingCursor(c);
String[] from = new String[]{"nombrecasa"};
int[] to = new int[]{android.R.id.text1};
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
casaSpinner = (Spinner) findViewById( R.id.casaspinner);
casaSpinner.setAdapter(adapter);
dbConnector.close();
}
And when I try to get the value as:
String idcasa = casaSpinner.getSelectedItem().toString();
it only returns this:
android.database.sqlite.SQLiteCursor#40539880
Your spinner has taken a SimpleCursorAdapter as an adapter, of which its dataset is a Cursor. When you select an item on the spinner, you've only moved the cursor to a specific row and when you call getSelectedItem() you're still requesting for the dataset object - which is still a cursor object. to do what you're trying you can simply call:
c.getString(1);
once your selection has been made. For something a little more fundamentary illustrative of what i said.
((Cursor) casaSpinner.getSelectedItem()).getString(1);
I cannot refresh my list view after deleting the records.
String str3 = favtitle.get(position);
//position = Integer.parseInt(cur.getString(cur.getColumnIndex("id")));
db.delete("Favorites7","title= '"+str3+"'",null);
//mSchedule.notifyDataSetChanged();
cur.requery();
cur= db.query("Favorites7",
null, null, null, null, null, null);
cur.moveToFirst();
while (cur.isAfterLast() == false) {
//name.append("n" + cur.getString(1));
//mylist.addAll(cur.getString(i));
favtitle.add(cur.getString(1));
favloc.add(cur.getString(3));
favemp.add(cur.getString(2));
lat.add(cur.getString(7));
log.add(cur.getString(8));
//nextScreenData.add(cur.getString(2).toString());
cur.moveToNext();
}
cur.close();
mylist = new ArrayList<HashMap<String,Object>>();
HashMap<String,Object> map = new HashMap<String, Object>();
for(int i=0;i<favtitle.size();i++)
{
map = new HashMap<String, Object>();
map.put("train",favtitle.get(i));
map.put("value",favloc.get(i));
map.put("employer",favemp.get(i));
mylist.add(map);
}
mSchedule.notifyDataSetChanged();
mSchedule = new SimpleAdapter(Favorites.this, mylist, R.layout.listdelete,
new String[] {"train","value","employer"}, new int[] {R.id.dept,R.id.jobloc,R.id.employer});
lv.setAdapter(mSchedule);
lv.setDividerHeight(2);
lv.setCacheColorHint(Color.WHITE);
//cur.requery();
Thanx in advance.
You can just create a new Adapter, and then assign it to the ListView.
You can call notifyDataSetChanged() on your Adapter and it will reflect the changes to the ui
When the delete button is clicked do the following:
Delete the corresponding list item from the database.
Get an new cursor for your list by executing the query which fills the list again.
Bind the new cursor to the list using changeCurosr().
Call notifyDataSetChanged() on the adapter.
I'm new programming with android, and doing my first aplication i got something wrong with the lists.
Here the code:
Cursor c = db.rawQuery("SELECT nombre FROM contactos", null);
ArrayList<String> listaArray = new ArrayList<String>();
ListView listadoContactos = (ListView)findViewById(R.id.listViewListaContactos);
if (c.moveToFirst())
{
do {
listaArray.add(c.getString(0));
} while(c.moveToNext());
}
//Creamos un adaptador y lo asignamos al ListView.
ArrayAdapter<String> adaptadorLista = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listaArray);
listadoContactos.setAdapter(adaptadorLista);
When i see the result, my list its showing only the first item in the db that i'm querying. Can you help me, please?
Thanks in advice! George.
PS: "nombre" is the column 0. That is why i'm writting "getString(0)", because i just want to show 1 column of each row.
Try to use SimpleCursorAdapter
// NOTE your query result should have id-field, named "_id"
Cursor c = db.rawQuery("SELECT nombre, nombre_id as _id FROM contactos", null);
SimpleCursorAdapter adaptorLista = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, c, new String[]{"nombre"}, new int[]{android.R.id.text1} );
listadoContactos.setAdapter(adaptadorLista);
Here android.R.layout.simple_list_item_1 is a layout of each row in ListView,
new String[]{"nombre"} names of fields in cursor which values are set to TextViews in ListView row. TextView ids specified in the last argument new int[]{android.R.id.text1}
Use a CursorAdapter instead of an ArrayListAdapter. You are making extra work for yourself by copying the cursor into an Array just to feed it to the ListView.
Here is a tutorial on CursorAdapters
You could use a SimpleCursorAdapter and pass it the cursor directly:
Adapter adapter = new SimpleCursorAdapter(
this,
c,
android.R.layout.simple_list_item_1,
new String[] { "nombre" },
new int[] { android.R.id.text1 },
0);
listadoContactos.setAdapter(adapter);
and then you don't have to step through and build the list yourself.