I'm creating a ListView that contains two TextViews and an ImageView.
I'm passing data to my SimpleAdapter using a Hashmap, so I convert the ids of the images to a string and then later in the SimpleAdapter I convert them back to integers to set the image resource.
However that doesn't seem to work.
The relevant code is:
The oncreate of my activity
clubImages = new Integer[] {
R.drawable.pic1, R.drawable.pic2,
R.drawable.pic3, R.drawable.pic4,
R.drawable.pic5
};
ListView lv = (ListView)findViewById(android.R.id.list);
// create the grid item mapping
String[] from = new String[] {"image"};
int[] to = new int[] {R.id.club_image};
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < eventTitles.length; i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("image", getString(clubImages[i]));
fillMaps.add(map);
}
// fill in the grid_item layout
SimpleAdapter adapter = new MySimpleAdapter(this, fillMaps, R.layout.eventview_row, from, to);
lv.setAdapter(adapter);
the simpleadapter class
public class MySimpleAdapter extends SimpleAdapter {
private List<HashMap<String, String>> results;
public MySimpleAdapter(Context context, List<HashMap<String, String>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
this.results = data;
}
public View getView(int position, View view, ViewGroup parent){
Typeface type = Typeface.createFromAsset(getAssets(), "fonts/aircruiser.ttf");
View v = view;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.eventview_row, null);
}
mClubImageImageView = (ImageView) v.findViewById(R.id.club_image);
int ClubImageHelper = Integer.parseInt(results.get(position).get("image").toString());
mClubImageImageView.setImageResource(ClubImageHelper);
return v;
}
}
The error in my logcat
E/AndroidRuntime(19406): java.lang.NumberFormatException: unable to parse 'res/drawable-hdpi/pic1.png' as integer
You can't pass R.drawable.pic1 as a String and then parse it back as an Integer. This will not work, instead simply pass the initial Integer drawable ids directly in the HashMap:
List<HashMap<String, Integer>> fillMaps = new ArrayList<HashMap<String, Integer>>();
for(int i = 0; i < eventTitles.length; i++){
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("image", clubImages[i]);
fillMaps.add(map);
}
I hope I understand what you want. You could make a class to hold the data that you want to pass in to the adapter, for example:
class DataObject {
int imageId;
String firstString;
String secondString; // add here any string that you want to put in your list
public DataObject(String firstString, String secondString, int imageId) {
this.imageId = imageId;
this.firstString = firstString;
this.secondString = secondString;
}
}
Then construct the list for the adapter:
List<HashMap<String, DataObject>> fillMaps = new ArrayList<HashMap<String, DataObject>>();
for(int i = 0; i < eventTitles.length; i++){
HashMap<String, DataObject> map = new HashMap<String, DataObject>();
map.put("image", new DataObject("First String", "second String", clubImages[i]));
fillMaps.add(map);
}
In the getView() method retrieve the DataObject and use its content data as you please.
here is an good example that may help you
Related
May i know how to get HashMap item from a custom adapter
Code:
ArrayList<HashMap<String, String>> feeds_List;
...
...
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("username", usr_name);
map.put("datetime", feed_date);
map.put("pts", point_txt);
map.put("pic", pic);
// adding HashList to ArrayList
feeds_List.add(map);
ListAdapter adapter = new ExtendedSimpleAdapter(
getActivity(), feeds_List,
R.layout.feed_item, new String[]{"username", "datetime", "pts"},
new int[]{R.id.usr_name, R.id.feed_time, R.id.no_stamp});
setListAdapter(adapter);
My Custom Adapter:
public class ExtendedSimpleAdapter extends SimpleAdapter{
Context context2;
public ExtendedSimpleAdapter(Context context, List<? extends Map<String, String>> data, int resource, String[] from, int[] to){
super(context, data, resource, from, to);
context2=context;
}
public View getView(int position, View convertView, ViewGroup parent){
// here you let SimpleAdapter built the view normally.
View v = super.getView(position, convertView, parent);
// Then we get reference for Picasso
ImageView img = (ImageView) v.getTag();
if(img == null){
img = (ImageView) v.findViewById(R.id.usr_pic);
v.setTag(img); // <<< THIS LINE !!!!
}
// get the url from the data you passed to the `Map`
String TAG_IMAGE="pic";
String url = ((Map)getItem(position)).get("pic");
// do Picasso
// maybe you could do that by using many ways to start
Picasso.with(context2)
.load(url)
//.resize(100, 100)
.into(img);
// return the view
return v;
}
}
Here is the problem:
I cannot get the url string in the custom adapter.
always getting errors of
"Incompatible types"(needed String, but found is object)
for this line
String url = ((Map)getItem(position)).get("pic");
The object you are casting against, should have the exact type. Casting to the generic Map doesn't enforce the compile time check, that the generics are meant for.
String url = ((Map<String, String>)getItem(position)).get("pic");
Use
private ArrayList<Map<String, String>> data;
Map<String, String> category = new Map<String, String>();
category = data.get(position);
String url=category.get("pic");
Simply change this
String url = ((Map)getItem(position)).get("pic");
to
String url = list.get(position).get("pic");
Also here
Context context2;
List<HashMap<String, String>> list;
public ExtendedSimpleAdapter(Context context, List<HashMap<String, String>> data, int resource, String[] from, int[] to){
super(context, data, resource, from, to);
context2=context;
this.list = data;
}
I am using BaseAdapter to bind Custom ListView. And I am adding Items dynamically. Each Item contains TextView, EditText, Spinner. It is working fine. But now issue is that if user selects any value from Spinner, add some data in EditText and then if he/she adds new item then it clears all values of filled up data.
So, I want to prevent this refresh of already added Items when add new Item. How to do this ?
My Code :
ArrayList<HashMap<String, String>> aaName = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = null;
map = new HashMap<String, String>();
map.put("Name", Name value);
aaName.add(map);
NameAdapter nameAdapter = new NameAdapter(getActivity(), aaName);
lvName.setAdapter(nameAdapter);
public class NameAdapter extends BaseAdapter {
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.chiefcomment_view, null);
TextView tvName = (TextView) vi.findViewById(R.id.tvName);
Spinner spnTime = (Spinner) vi.findViewById(R.id.spnTime);
EditText etSpecification = (EditText) vi.findViewById(R.id.etSpecification);
HashMap<String, String> name = new HashMap<String, String>();
name = data.get(position);
tvName.setText(name.get("Name"));
// Time Spinner...
ArrayList<String> Time = new ArrayList<String>();
for (int i = 1; i <= 100; i++) {
Time.add(String.valueOf(i));
}
aaTime = new ArrayAdapter<String>(activity, R.layout.small_spinner_view, Time);
spnTime.setAdapter(aaTime);
return vi;
}
}
I want to fill out my ListView with some data and I use SimpleAdapter. But I think SimpleAdapter only works with List<HashMap<String, String>> and I've List<Object> like follow codes:
What can I do?
Is there any way?
List<DSarchive> programs = ...;
String[] from = new String[] {"name", "time", "day", "month"};
int[] to = new int[] { R.id.text1, R.id.text2, R.id.text3, R.id.text4};
SimpleAdapter adapter = new SimpleAdapter(getActivity(), programs, R.layout.my_list_row, from, to);
// ^ how can I use this "programs" List???
lv.setAdapter(adapter);
I want to use adapter List in MyObject.
MyObject class:
public class MyObject{
public ArrayList<String> links;
public String name;
public List<HashMap<String, String>> adapter;
.
.
.
}
Try using an ArrayAdapter
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("temp1");
arrayList.add("temp2");
ListView listView = new ListView(getActivity());
listView.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, arrayList));
EDIT
The SimpleListAdapter is not constrained to only use Strings. It does require a String key, but it can map to any object. For example, I used the following code to create a ListView that contains an Icon with two supporting TextViews. Hope this helps.
List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
Map<String, Object> dataMap = new HashMap<String, Object>(3);
dataMap.put("Item1", dataObject); //icon
dataMap.put("Item2", dataString);
dataMap.put("Item3", dataString2);
data.add(dataMap);
dataMap = new HashMap<String, Object>(2);
dataMap.put("Item1", dataObject); //icon
dataMap.put("Item2", dataString);
dataMap.put("Item3", dataString2);
data.add(dataMap);
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(),
(List<? extends Map<String, ?>>) data,
R.layout.layoutFile2,
new String[] {"Item1", "Item2", "Item3"} ,
new int[] {R.id.item1, R.id.item2, R.id.item3}){
//overload the getChildView or any other Override methods
};
i want to inflate a expandable list with some different text and different images on each row if i use albumCovers.get(i) instead of getResources().getDrawable(R.drawable.icon)then it throws an error, any one help us out? thanks ;p.
public class ExpandActivity extends ExpandableListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Construct Expandable List
final String NAME = "name";
final String IMAGE = "image";
final LayoutInflater layoutInflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ArrayList<HashMap<String, String>> headerData = new ArrayList<HashMap<String, String>>();
final HashMap<String, String> group1 = new HashMap<String, String>();
group1.put(NAME, "Group 1");
headerData.add(group1);
final ArrayList<ArrayList<HashMap<String, Object>>> childData = new ArrayList<ArrayList<HashMap<String, Object>>>();
final ArrayList<HashMap<String, Object>> group1data = new ArrayList<HashMap<String, Object>>();
childData.add(group1data);
Resources res = this.getResources();
Drawable photo = (Drawable) res.getDrawable(R.drawable.icon);
ArrayList<Drawable> albumCovers = new ArrayList<Drawable>();
albumCovers.add(photo);
// Set up some sample data in both groups
for (int i = 0; i < 10; ++i) {
final HashMap<String, Object> map = new HashMap<String, Object>();
map.put(NAME, "Child " + i);
map.put(IMAGE, getResources().getDrawable(R.drawable.icon));
(group1data).add(map);
}
setListAdapter(new SimpleExpandableListAdapter(this, headerData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME }, // the name of the field data
new int[] { android.R.id.text1 }, // the text field to populate
// with the field data
childData, 0, null, new int[] {}) {
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final View v = super.getChildView(groupPosition, childPosition,
isLastChild, convertView, parent);
// Populate your custom view here
((TextView) v.findViewById(R.id.name))
.setText((String) ((Map<String, Object>) getChild(
groupPosition, childPosition)).get(NAME));
((ImageView) v.findViewById(R.id.image))
.setImageDrawable((Drawable) ((Map<String, Object>) getChild(
groupPosition, childPosition)).get(IMAGE));
return v;
}
#Override
public View newChildView(boolean isLastChild, ViewGroup parent) {
return layoutInflater.inflate(
R.layout.expandable_list_item_with_image, null, false);
}
});
}
}
You have only one drawable in albumCovers but loop it ten times in the for loop and try to access it by the index. Change albumCovers.get(i) to albumCovers.get(0) for it to work.
Here is the key location:
Drawable photo = (Drawable) res.getDrawable(R.drawable.icon);
ArrayList<Drawable> albumCovers = new ArrayList<Drawable>();
albumCovers.add(photo); // HERE: photo added only once!
for (int i = 0; i < 10; ++i) {
final HashMap<String, Object> map = new HashMap<String, Object>();
map.put(NAME, "Child " + i);
map.put(IMAGE, getResources().getDrawable(R.drawable.icon)); // HERE: you wanted to use albumCovers.get(i) here. There is only 1 photo in albumCovers but you try to get images 0...9 from it using index i. So, change this to albumCovers.get(0) OR add 10x photo into albumCovers.
(group1data).add(map);
}
I am using Spinner control in my application and my Code is
Spinner s1=new Spinner(this);
s1.setLayoutParams(new LayoutParams(100,30));
ArrayAdapter adapter=new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.add("One");
adapter.add("Two");
adapter.add("Three");
s1.setAdapter(adapter);
This will work fine in linearLayout .when I use the code in the TableLayout, the control is not displayed.I have to generate dynamic layout so i am not using the XML part,only through Java code i am adding the contorls.
TableLayout tl=new TableLayout(this);
TableRow tr=new TableRow(this);
tr.addView(s1);
tl.addView(tr);
All the components gets added other than Spinner.
So please anyone give some solution for this.
Now add your tl to ScrollView using:
ScrollView sv = new Scrollview(this);
sv.addview(tl);
setContentview(sv);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("Name", "One");
map.put("Icon", R.drawable.icon);
list.add(map);
map = new HashMap<String, Object>();
map.put("Name", "Two");
map.put("Icon", R.drawable.icon);
list.add(map);
Spinner spin = (Spinner) findViewById(R.id.spin);
myAdapter adapter = new myAdapter(getApplicationContext(), list,
R.layout.list_layout, new String[] { "Name", "Icon" },
new int[] { R.id.name, R.id.icon });
spin.setAdapter(adapter);
}
private class myAdapter extends SimpleAdapter {
public myAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_layout,
null);
}
HashMap<String, Object> data = (HashMap<String, Object>) getItem(position);
((TextView) convertView.findViewById(R.id.name))
.setText((String) data.get("Name"));
((ImageView) convertView.findViewById(R.id.icon))
.setImageResource(R.drawable.icon);
return convertView;
}
}