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
};
Related
I am working on health related app. In ListView I want to assign value to each item. For example milk contains 21 calories so I want to assign 21 to ListView item milk.
Here is my activity code containing ListView.
public class FoodEntry extends AppCompatActivity {
// Array of strings...
String[] food = {"Naan","Pav bhaji","chole tikiya", "rice", "soybean", "milk", "curd"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_entry);
ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.activity_item, food);
ListView listViewFoodItems = (ListView)findViewById(R.id.listViewFood);
listViewFoodItems.setAdapter(adapter);
}
}
Instead of using a String[] to store your data like you're doing here:
// Array of strings...
String[] food = {"Naan","Pav bhaji","chole tikiya", "rice", "soybean", "milk", "curd"};
Use a new object, such as CalorieCount.
// Array of CalorieCount
CalorieCount[] food = { new CalorieCount("Naan", 20) ... };
You can use SimpleAdapter with Hashmap
foodItems= new ArrayList<HashMap<String, String>>();
//create first item object
HashMap<String, String> item1= new HashMap<String, String>();
item1.put("name", "Naan");
item1.put("calories", "21");
foodItems.add(item1)//add multiple items like this
String[] from = { "name"};
// view id's to which data to be binded
int[] to = { R.id.name};
//Creating Adapter
ListAdapter adapter = new SimpleAdapter(MainActivity.this, foodItems,
R.layout.activity_item, from, to);
//Setting Adapter to ListView
listViewFoodItems.setAdapter(adapter);
To know more visit How SimpleAdapter Binds Hashmap Data to items of ListView
I'm working on android application that shows number and name of persons , using ListView .But the compilers doesn't recognize "champs1" and "champs2".
public class Second extends Activity {
ListView vue;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
vue=(ListView) findViewById(R.id.listV2);
String [][] rep=new String[][]{{"1","aaaaaaa" },
{"2","bbbbbbb" },
{ "3","ccccccc" } };
List<HashMap<String,String> > list= new ArrayList<HashMap<String, String>>();
HashMap<String,String> element;
for(int i=0 ;i<rep.length;i++ ){
element =new HashMap<String, String>();
element.put("chmaps1",rep[i][0]);
element.put("champs2",rep[i][1]);
list.add(element);
}
ListAdapter adapter=new SimpleAdapter(this,
list,
android.R.layout.simple_list_item_2,
new String[] {"chmaps1","champs2"},
new int[] {android.R.id.champs1, android.R.id.champs2 });
vue.setAdapter(adapter);
}
}
Error:(39, 71) error: cannot find symbol variable champs1
Error:(39, 71) error: cannot find symbol variable champs2
change android.R.id.champs1 to android.R.id.text1
change android.R.id.champs2 to android.R.id.text2
public class Second extends Activity {
ListView vue;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchable);
vue = (ListView) findViewById(R.id.listView1);
String[][] rep = new String[][] { { "1", "aaaaaaa" },
{ "2", "bbbbbbb" }, { "3", "ccccccc" } };
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> element;
for (int i = 0; i < rep.length; i++) {
element = new HashMap<String, String>();
element.put("chmaps1", rep[i][0]);
element.put("champs2", rep[i][1]);
list.add(element);
}
ListAdapter adapter = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2, new String[] { "chmaps1",
"champs2" }, new int[] { android.R.id.text1,
android.R.id.text2 });
vue.setAdapter(adapter);
}
}
You are refferring to:
android.R.id.champs1
instead of:
R.id.champs1
Also be careful that you ar calling variables chmaps1 sometimes and champs1 some other times.
put this code in for loop
HashMap<String, String> element = new HashMap<String, String>();
and there is no problem i think android.R.id.champs1 or R.id.champs1 both are correct .
change this one instead of this vue.setAdapter(adapter)
try like this vue.setListAdapter(adapter);
I have code in one Activity as shown below and it works well.
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, catid);
map.put(TAG_NAME, catname);
oslist.add(map);
list=(ListView)findViewById(R.id.listView1);
ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
R.layout.category_list,
new String[] { TAG_ID,TAG_NAME}, new int[] {
R.id.catid,R.id.catname});
While similar code in another Activity is not working...
ListAdapter adapter = new SimpleAdapter(newsList.this, oslist,
R.layout.category_list,
new String[] { TAG_ID,TAG_NAME}, new int[] {
R.id.catid,R.id.catname});
list.setAdapter(adapter);
It gives error like :
The constructor SimpleAdapter(newsList, ArrayList<HashMap<String,String>>, int, String[], int[]) is undefined
Where you have newsList.this you should be passing a Context, probably the Activity2.this, assuming your second Activity is called Activity2.
you have to pass context in the constructor of your adapter like:
//Declare global
Contect _ctx = youractivityname.this
and then pass it to your adapter like:
ListAdapter adapter = new SimpleAdapter(_ctx, oslist,R.layout.category_list,new String[] { TAG_ID,TAG_NAME}, new int[] {R.id.catid,R.id.catname});
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
Hey guys I am a working on displaying a list of items on listview, the code I am using is
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_view);
ListView lv= (ListView)findViewById(R.id.listview);
rtrnList = new ArrayList<HashMap<String,String>>();
getmyLocation();
listclass = new listClass(offersobj);
listclass.populate();
rtrnList = listclass.getListArray();
adapter = new SimpleAdapter(
this,
rtrnList,
R.layout.custom_row_view,
new String[] {"Name","Msg","time"},
new int[] {R.id.text1,R.id.text2, R.id.text3}
);
lv.setAdapter(adapter);
}
problem is say I am displaying three names Avinash, Arun, Rajesh. When application starts these three names are displayed on list. When I close and again start the application the values are repeating Avinash, Arun, Rajesh,Avinash, Arun, Rajesh. I am not able to figure out how to solve this.
The code you show seems fine. My guess is that listclass.populate() modifies offersobj and that offersobj is reused over several creations of your activity. So, whenever the activity is created, additional data is populated.
public class ListViewA extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv= (ListView)findViewById(R.id.listview);
// create the grid item mapping
String[] from = new String[] {"rowid", "col_1", "col_2", "col_3"};
int[] to = new int[] { R.id.item1, R.id.item2, R.id.item3, R.id.item4 };
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < 10; i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("rowid", "" + i);
map.put("col_1", "col_1_item_" + i);
map.put("col_2", "col_2_item_" + i);
map.put("col_3", "col_3_item_" + i);
fillMaps.add(map);
}
// fill in the grid_item layout
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
lv.setAdapter(adapter);
}
}
for more example see this linkthis alsolistview about adapter, for create a hashmap, why bitmap type imported cannot show image in listview?What adapter shall I use to use HashMap in a ListView