How to add subitem in Listview - android

i have a working ListView I'm trying to add subitems in it. but there's seems to be a problem on list.setAdapter(new SimpleAdapter(this, list, label, wala));
String[] resName = new String[(int) dataSnapshot.getChildrenCount()];
String[] resLoc;
ListView list = (ListView) findViewById(R.id.listViewRest);
restauList = new ArrayList<HashMap<String, String>>();
resLoc = new String[]{"TRY", "TRY2"};
int i = 0;
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
resName[i] = dataSnapshot1.child("resname").getValue().toString();
HashMap<String, String> listahan = new HashMap<String, String>();
listahan.put("Restaurant Name", String.valueOf(resName));
listahan.put("Location", String.valueOf(resLoc));
restauList.add(listahan);
i++;
}
String [] label = new String[] {"Name", "Location"};
int [] wala = new int []{ android.R.id.text1, android.R.id.text2 };
int layout = android.R.layout.two_line_list_item;
list.setAdapter(new SimpleAdapter(this, list, label, wala));

You should use from ExpandableListView for add subItem in listView.

Related

Android ListView with SQL id

I'm working on an app where user will be able to view a list with names, and when he clicks on one, another activity pops up with more details regarding the chosen profile. I've got a ListView. I create it using this code:
listContacts = (ListView) findViewById(R.id.listContacts);
data = new ArrayList<HashMap<String, String>>();
db = new DatabaseHandler(this);
List<Contact> contacts = db.getAllContacts();
List<String> names = new ArrayList<>();
List<String> sex = new ArrayList<>();
List<String> age = new ArrayList<>();
List<Integer> id = new ArrayList<>();
List<String> sexnage = new ArrayList<>();
for (Contact cn : contacts) {
names.add(cn.getName() + " " + cn.getSurname());
sex.add(cn.getSex());
age.add(cn.getAge());
id.add(cn.getID());
sexnage.add(cn.getSex() + " " + cn.getAge());
}
titleArray = new String[names.size()];
titleArray = names.toArray(titleArray);
subItemArray = new String[sexnage.size()];
subItemArray = sexnage.toArray(subItemArray);
for(int i=0;i<titleArray.length;i++){
HashMap<String,String> datum = new HashMap<String, String>();
datum.put("Name", titleArray[i]);
datum.put("Sex and Age", subItemArray[i]);
data.add(datum);
}
SimpleAdapter subAdapter = new SimpleAdapter(this, data, android.R.layout.simple_list_item_2,
new String[] {"Name", "Sex and Age"}, new int[]
{android.R.id.text1, android.R.id.text2});
listContacts.setAdapter(subAdapter);
How can I set ID of each element in List, so that it corresponds to Database one?
Use a SimpleCursorAdapter as listview adapter.

Set ListAdapter values to String

I have the following function which creates a ListAdapter;
ListAdapter adapter = new SimpleAdapter(
AllProductsActivity.this, productsList,
R.layout.list_item, new String[] { TAG_PID, TAG_NAME, TAG_PHONE, TAG_ADDRESS, TAG_SIZE, TAG_CHES, TAG_PE, TAG_PRICE},
new int[] { R.id.pid, R.id.name, R.id.phone, R.id.address, R.id.Small, R.id.XC, R.id.Pep, R.id.price});
// updating listview
setListAdapter(adapter);
Once the listview is updated, I want to get the 1st listview row which will have the highest pid value, and assign its values to some String values. To give you an idea of what I want, pseudo code as follows;
String nam = highest pid.name;
String pho = highest pid.phone;
...
Any thoughts on how to achieve that?
I think you have to get back the adapter from the listView and check all the items in that adapter to find out which has the highest pid.
It will be look like this:
SimpleAdapter mAdapter = (SimpleAdapter)listView.getAdapter();
Map<String, String> maxIdMap = null;
for(int i=0; i<mAdapter.getCount(); i++){
Map<String, String> map = (Map<String, String>)mAdapter.getItem(i);
Integer pid = Integer.parseInt(map.get(TAG_PID));
if(maxIdMap==null){
maxIdMap = map;
} else if(pid>Integer.parseInt(maxIdMap.get(TAG_PID))){
maxIdMap = map;
}
}
if(maxIdMap!=null) {
// There is at least one record
String name = maxIdMap.get(TAG_NAME);
String phone = maxIdMap.get(TAG_PHONE);
Log.d("Test", "Name: " + name + " Phone: " + phone);
}

Set Text of TextView in ListView

In my ListView, I save my data like this:
public void listview_fuellen(){
DBHelper db = new DBHelper(this);
ListView lv = (ListView) findViewById(R.id.lvKinder);
Cursor c = db.select();
int count = c.getCount();
TextView tv = (TextView) findViewById(R.id.secondLine);
List<String> auswahl = new ArrayList<String>();
List<String> auswahl1 = new ArrayList<String>();
int i = 0;
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$1" + c.getCount());
while(c.moveToNext())
{
auswahl.add(c.getString(c.getColumnIndex("name")));
auswahl1.add(" " + i);
System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS" + auswahl.get(i).toString());
i++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_black_text,R.id.firstLine, auswahl);
//ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, R.layout.list_black_text,R.id.secondLine, auswahl1);
lv.setAdapter(adapter);
}
Now, I tried the layout for my listview from here:
http://android-developers.blogspot.co.at/2009/02/android-layout-tricks-1.html
Now, how can I set values for the second line in each ListView-Item or the icon?
EDIT:
I tried it like this now, but there are no entries in my listview then.
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();;
int i = 0;
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$1" + c.getCount());
while(c.moveToNext())
{
//map = new HashMap<String, String>();
map.put("name",c.getString(c.getColumnIndex("name")));
map.put("datum", c.getString(c.getColumnIndex("zeit")));
i++;
}
mylist.add(map);
SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.list_black_text, new String[] { "datum",
"name" }, new int[] { R.id.firstLine, R.id.secondLine });
lv.setAdapter(mSchedule);
setListAdapter(mSchedule);
What have I done wrong?
for that you can make a custom adapter and then you can set values for the second line in each ListView-Item.
You can check this link
http://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/

Database results in a listView is not working

i m trying to get data from my database and present them to a listView.i m using two classes for my database:
1st class is the DBAdapter and the method i use to get data is:
public String[] getData()
{
// String[] columns =new String[]{DBHelper.ROWID, DBHelper.TITLE , DBHelper.AUTHOR, DBHelper.ISBN };
String[] columns =new String[]{DBHelper.TITLE , DBHelper.AUTHOR, DBHelper.ISBN };
Cursor c=ourDatabase.query(DBHelper.DATABASE_TABLE, columns, null, null, null, null, null);
String result="";
String sa = null;
String sb = null;
String sc = null;
//int iRow=c.getColumnIndex(DBHelper.ROWID);
int is1=c.getColumnIndex(DBHelper.TITLE);
int is2=c.getColumnIndex(DBHelper.AUTHOR);
int is3=c.getColumnIndex(DBHelper.ISBN);
for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
//result=result+c.getString(is1)+" "+c.getString(is2)+" "+c.getString(is3)+"\n";
sa=c.getString(is1);
sb=c.getString(is2);
sc=c.getString(is3);
}
//Toast.makeText(HotOrNot.this, sa, Toast.LENGTH_SHORT).show();
return new String[] {sa,sb,sc};
}
2.The secind class in SQLView and thats the way i m trying to create my list
public class SQLView extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
HotOrNot entry2=new HotOrNot(this);
entry2.open();
String[] data2=entry2.getData();
entry2.close();
Toast.makeText(SQLView.this, data2[1].toString(), Toast.LENGTH_SHORT).show();
ListView list = (ListView) findViewById(R.id.list);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map = new HashMap<String, String>();
map.put("name",data2[0].toString());
map.put("address", data2[1].toString());
map.put("address2", data2[2].toString());
mylist.add(map);
// ...
ListAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.row,
data2, new int[] {R.id.rtextView1,R.id.rtextView2,R.id.rtextView3});
list.setAdapter(mSchedule);
What i have to do in order to work?This is my first try to use database in android!
Now i just get an empty background...
EDIT:
With my toast i m getting the last item in position 1 that i added to db
Could anybody help me with the return statement in my DBHelper please?
Below is the fixed code. Try this out.
ListAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.row,
new String[] { "name", "address", "address2"},
new int[] {R.id.rtextView1,R.id.rtextView2,R.id.rtextView3});
list.setAdapter(mSchedule);
Basically you have to specify the columns/key of the map that you added. So it will map key from map to the corresponding view.
For looping issue, this will do the job:
for(int i=0; i<data2.length; i+=3) {
HashMap<String, String> map;
map = new HashMap<String, String>();
map.put("name",data2[i].toString());
map.put("address", data2[i+1].toString());
map.put("address2", data2[i+2].toString());
mylist.add(map);
}
The getData fix:
ArrayList<String> arrForData = new ArrayList<String>();
//int iRow=c.getColumnIndex(DBHelper.ROWID);
int is1=c.getColumnIndex(DBHelper.TITLE);
int is2=c.getColumnIndex(DBHelper.AUTHOR);
int is3=c.getColumnIndex(DBHelper.ISBN);
for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
//result=result+c.getString(is1)+" "+c.getString(is2)+" "+c.getString(is3)+"\n";
arrForData.add(c.getString(is1));
arrForData.add(c.getString(is2));
arrForData.add(c.getString(is3));
}
//Toast.makeText(HotOrNot.this, sa, Toast.LENGTH_SHORT).show();
return arrForData.toArray();

how to get value from database into Expandable List via cursor

I wonder how to get value from database into Expandable List via cursor
public class SmplExpandable extends ExpandableListActivity {
private static final String NAME = "NAME";
private static final String IS_EVEN = "IS_EVEN";
private ExpandableListAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
for (int i = 0; i < 10; i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, "Group " + i);
curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
for (int j = 0; j < 3; j++) {
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(NAME, "Child " + j);
curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
}
childData.add(children);
}
// Set up our adapter
mAdapter = new SimpleExpandableListAdapter(
this,
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 },
childData,
android.R.layout.simple_expandable_list_item_2,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 }
);
setListAdapter(mAdapter);
}
}
Why don't you use the SimpleCursorTreeAdapter : http://developer.android.com/reference/android/widget/SimpleCursorTreeAdapter.html
By the way,if anyone has a good tutorial about SimpleCursorTreeAdapter,please share with us.
Here is sample from Android API demo ExpandableList2 You can import code by create sample project

Categories

Resources