Listview to SimpleAdapter not working - android

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

Related

How to assign value to each item in listView?

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

how to input data from Object into SimpleAdapter

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

Using simple_list_item_2 and can't figure out how to use setOnItemClickListener

Please excuse me, I'm new:new at this. I use a simple_list_item_2 to display 11 items. These 11 items have been loaded by using HashMap and then SimpleAdapter. This works fine in displaying everything. The problem is that I cannot get setOnItemClickListener going. The code:
public class TwoLineActivity extends ListActivity
{
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(2);
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.scrolllist);
// enter valid data, these 2 are the same as the remaining 9
HashMap<String, String> maplist;
maplist = new HashMap<String, String>();
maplist.put("line1", "a11 data");
maplist.put("line2", "asd asd ad 1234569780");
list.add(maplist);
maplist = new HashMap<String, String>();
maplist.put("line1", "a12 data");
maplist.put("line2", "asd asd ad 1234569781");
list.add(maplist);
String[] from = { "line1", "line2" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(this, list, android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);`
So up to here things are great, I get my list. Now I want to be able to select an item from the list, so I coded the next 2 lines
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener()
{ .....
I get the following errors
The method setAdapter(SimpleAdapter) is undefined for the type ArrayList<HashMap<String,String>>
and
The method setOnItemClickListener(new AdapterView.OnItemClickListener(){}) is undefined for the type ArrayList<HashMap<String,String>>
If your activity extends ListActivity, you should override
protected void onListItemClick(ListView l, View v, int position, long id) {
}

Cannot use SimpleAdapter on a ListFragment

I'm developing a ListView in an Android Fragment. The class extends ListFragment.
I tried with this example:
http://www.heikkitoivonen.net/blog/2009/02/15/multicolumn-listview-in-android/
but the problem is that constructor SimpleAdapter is not defined if the class extends ListFragment, changing it to ListActivity would make SimpleAdapter work, but then application won't.
Here's the code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View tmp_view = inflater.inflate(R.layout.clients_list, container, false);
ListView list = (ListView) tmp_view.findViewById(R.id.list);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("train", "101");
map.put("from", "6:30 AM");
map.put("to", "7:40 AM");
mylist.add(map);
map = new HashMap<String, String>();
map.put("train", "103(x)");
map.put("from", "6:35 AM");
map.put("to", "7:45 AM");
mylist.add(map);
// ...
SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.clients_list_item,
new String[] {"train", "from", "to"}, new int[] {R.id.TRAIN_CELL, R.id.FROM_CELL, R.id.TO_CELL});
list.setAdapter(mSchedule);
ListFragment.setListAdapter(mSchedule);
return tmp_view;
}
So I will have no problem if this was an Activity, but it's a Fragment :S Any solution?
ListFragment is not a subclass of Context, which the constructor needs. Try replacing
SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, ...
with
SimpleAdapter mSchedule = new SimpleAdapter(getActivity(), mylist, ...
Finally found solution by using a custom adapter:
http://codehenge.net/blog/2011/05/customizing-android-listview-item-layout/

repeating ListView

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

Categories

Resources