Cannot use SimpleAdapter on a ListFragment - android

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/

Related

Listview to SimpleAdapter not working

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

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

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

calling listactiviy from tabactivity in android

Is it possible to call listactivity through tab activity? Basically, I am developing an app with 3 tabs, for which I am using tabactivity. Furthermore, in one of the tabs I want a listview, so I have derived from listactivity.
Now I want the click event to be determined in the listview. Am i missing something?
public class Tabissue extends TabActivity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TabHost host = getTabHost();
host.addTab(host.newTabSpec("Tab1").setIndicator("Tab1").setContent(new Intent(this,Tab1.class)));
host.addTab(host.newTabSpec("Tab2").setIndicator("Tab2").setContent(new Intent(this,Tab2.class)));
host.setCurrentTab(1);
}
}
Tab1 Class
public class Tab2 extends ListActivity
{
ListView list;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tab2);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("Test1","####");
map.put("Test2", "India");
map.put("Time", "India time");
mylist.add(map);
map = new HashMap<String, String>();
map.put("Test1", "####");
map.put("Test2", "US");
map.put("Time","US time");
mylist.add(map);
map = new HashMap<String, String>();
map.put("Test1", "####");
map.put("Test2", "UK");
map.put("Time", "UK Time");
mylist.add(map);
ListAdapter mSchedule = new SimpleAdapter( this,
mylist,
R.layout.row,
new String[]
{
"India",
"US",
"UK"
},
new int[]
{
R.id.TRAIN_CELL,
R.id.FROM_CELL,
R.id.TO_CELL,
}
);
list.setAdapter(mSchedule);
}
}
In your ListActivity set onItemClickListener:
getListView().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> av, View v, int position,
long id) {
// Do your stuff here
}
});

How to Customize the Spinner widget in android?

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

Categories

Resources