Set ListAdapter values to String - android

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

Related

How to add subitem in Listview

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.

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.

How do I add a BitMap to ListView using SimpleAdapter?

This is what I've tried :
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<len;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("tit", "Title : " + title[i]);
hm.put("init","Initial Price : " + cur_price[i]);
hm.put("cur","Current Price : " + init_price[i]);
hm.put("img", bitmap[i].toString() );
aList.add(hm); //I'm getting an error here
}
// Keys used in Hashmap
String[] from = { "tit","init","cur","img"};
// Ids of views in listview_layout
int[] to = { R.id.tit,R.id.init,R.id.cur,R.id.img};
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
aList, R.layout.listview_layout, from, to);
ListView listView = ( ListView ) findViewById(R.id.listview);
listView.setAdapter(adapter);
}
I think to assign an imageview with Bitmap, one might use something like :
ImageView logoimg = (ImageView) findViewById(R.id.logo);
logoimg.setImageBitmap(bitmap);
1) But How can I do the same in SimpleAdapter?
2) Moreover, why is that I'm getting an error at HashMap's add() method?

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

Categories

Resources