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?
Related
I have a code here that displays data in custom listview from my database
List<Map<String, String>> data1 = new ArrayList<Map<String, String>>();
while (data.moveToNext()) {
Map<String, String> datum = new HashMap<String, String>(5);
datum.put("Customer Name", data.getString(0));
datum.put("Address", data.getString(1));
datum.put("Remarks", data.getString(2));
datum.put("Date", data.getString(3));
datum.put("Code", data.getString(4));
data1.add(datum);
}
adapter = new SimpleAdapter(getActivity(), data1,
R.layout.listview_plan,
new String[]{"Customer Name", "Address", "Remarks", "Date", "Code"},
new int[]{R.id.list_name,
R.id.list_address,
R.id.list_remarks,
R.id.list_date, R.id.list_code});
listView.setAdapter(adapter);
count.setText("" + listView.getAdapter().getCount());
My question is during population I want to add some condition like hiding a textview. How can I do that?
sample
ListView.Populate{
if (data.getString(3)) == null) {
//Hide the label on that row
}
}
My target is to hide a specific textview in listview
if supplier id is !=0 then
if(user.getString("iSupplierID")!="0")
{
String SUPPLIER_NAME=user.getString(TAG_NAME);
String PRIMARY_SERVICE_CATEGORY = user.getString(TAG_PRIMARY);
String SECONDARY_SERVICE_CATEGORY = user.getString(TAG_SECONDARY);
String SOURCE_DIRECTORY_TYPE = user.getString(TAG_SOURCE);
Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_NAME, SUPPLIER_NAME);
map.put(TAG_PRIMARY, PRIMARY_SERVICE_CATEGORY);
map.put(TAG_SECONDARY, SECONDARY_SERVICE_CATEGORY);
map.put(TAG_SOURCE, SOURCE_DIRECTORY_TYPE);
supplierlist.add(map);
creating an id for list view
lv=(ListView)findViewById(R.id.listView1);
listview adapter
ListAdapter adapter = new SimpleAdapter(SupplierDetails.this, supplierlist,
R.layout.list_view,
new String[] { TAG_NAME,TAG_PRIMARY,TAG_SECONDARY, TAG_SOURCE }, new int[] {
R.id.primary,R.id.secondary, R.id.source});
lv.setAdapter(adapter);
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);
}
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/
i hav to display the thumbnail of the image along with the title of it in a list view by parsing the JSON feed.. My code
public class Main extends ListActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
String url;
Bitmap bitmap;
ImageView img;
int im ;
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = Json_mysamActivity.getJSONfromURL("http://gdata.youtube.com/feeds/mobile/videos?max-results=3&alt=json");
try {
JSONObject jb = json.getJSONObject("feed");
JSONArray jarr = jb.getJSONArray("entry");
for(int i=0;i<jarr.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = jarr.getJSONObject(i);
JSONObject det = e.getJSONObject("title");
map.put("id", String.valueOf(i));
map.put("$t", "t:" + det.getString("$t"));
map.put("type", "type: " + det.getString("type"));
JSONObject det1 = e.getJSONObject("media$group");
JSONArray det12 = det1.getJSONArray("media$thumbnail");
for(int j=0;j<1;j++)
{
JSONObject det12obj = det12.getJSONObject(i);
url = det12obj.getString("url");
map.put("url", "url:"+ det12obj.getString("url"));
mylist.add(map);
}
}
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String[] from = new String[] { "$t", "type","url" };
int[] to = new int[] { R.id.item_title, R.id.item_subtitle, R.id.test_image };
MySimpleCursorAdapter adapter = new MySimpleCursorAdapter(this, R.layout.main, mylist ,
from, to);
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
#SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
public class MySimpleCursorAdapter extends SimpleCursorAdapter {
// SimpleCursorAdapter requires cursor in the place of ArrayList but I need to pass //arraylist, how should I implement it
public MySimpleCursorAdapter(Context context, int layout, ArrayList<HashMap<String, String>> mylist,
String[] from, int[] to) {
super(context, layout, mylist, from, to);
}
#Override
public void setViewImage(ImageView v, String url) {
String path =url ;
Bitmap b = BitmapFactory.decodeFile(path);
v.setImageBitmap(b);
}
}
}
What you need is a SimpleAdapter. A cursor adapter is used for displaying daat in a list that comes from a database. SimpleAdapter is used for data in RAM (your list after parsing the json).
Check this first google entry, it should do.
In case you are using arraylist as datasource you should use ArrayAdapter, so Extend your adapter from ArrayAdapter. see link for Custom Array Adapter:
http://android-er.blogspot.in/2010/06/custom-arrayadapter-with-with-different.html
When you are loading image views in list, its good practice to use LazyLoading, see below link for Lazy Loading in android:
http://ballardhack.wordpress.com/2010/04/05/loading-remote-images-in-a-listview-on-android/
for this you have to use LayoutInflater, write your own adapter class extends ArrayAdapter,
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
adapter = new CustomAdapter(this, R.layout.list_row, R.id.title, data,
mInflater, imgid);