Listview in Listview -> which adapter should be used (Android) - android

I have a Listview. In that Listview I have multiple textviews and again a listview.
First I always used the simpleadapter to fill my Listviews with data. But now I have added embedded a listview.
Which Adaplter do I have to use?! Because I think that this is not possible with the simpleadapter.
Main Activity layout:
<ListView
android:id="#+id/lvData"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#drawable/border_ui"
android:divider="#color/dark_grey"
android:dividerHeight="1dp"
android:padding="4dp" />
Listview (lvData) item layout
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tv_deposit_depiting" />
<ListView
android:id="lvDepositDepitingList"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
I always used the simple adapter that way:
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
for (Deposit deposit : getCurrentList()) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", deposit.getName());
map.put("amount", String.valueOf(deposit.getAmount()));
list.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, list,
R.layout.list_item_layout_deposit, new String[] { "name",
"amount" }, new int[] { R.id.tvDepositName,
R.id.tvDepositCurrentMonth });
lvData.setAdapter(adapter);
I think I will have to extend an adapter and write a custom one. If that is what I have to do, which adapter should I extend?
Thanks!

Yes you can create a class extending BaseAdapter and customize the view as you would like, check this link for help and guidelines Custom Adapter Guide

Related

ImageView can not display image using SimpleAdapter

MainActivity.java
public class MainActivity extends AppCompatActivity {
private String[] names = new String[]{"name1", "name2", "name3"};
private String[] says = new String[]{"desc1", "desc2", "desc3"};
private int[] imgIds = new int[]{R.mipmap.head_icon1, R.mipmap.head_icon2, R.mipmap.head_icon3};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();
for (int i = 0; i < names.length; i++) {
Map<String, Object> showitem = new HashMap<String, Object>();
showitem.put("touxiang", imgIds[i]);
showitem.put("name", names[i]);
showitem.put("says", says[i]);
listitem.add(showitem);
}
//create one simpleAdapter
SimpleAdapter myAdapter = new SimpleAdapter(getApplicationContext(), listitem, R.layout.list_item, new String[]{"touxiang", "name", "says"}, new int[]{R.id.imgtou, R.id.name, R.id.says});
ListView listView = (ListView) findViewById(R.id.list_test);
listView.setAdapter(myAdapter);
}
}
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/imgtou"
android:layout_width="64dp"
android:layout_height="64dp"
android:baselineAlignBottom="true"
android:paddingLeft="8dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:textColor="#1D1D1C"
android:textSize="20sp" />
<TextView
android:id="#+id/says"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8px"
android:textColor="#B4B4B9"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
but these images can be recognized by android studio.
anyone can help to find the problem?
You've used SimpleAdapter in your code.
But usage was wrong.
SimpleAdapter haven't image id as parameter.
Please look following URL.
https://developer.android.com/reference/android/widget/SimpleAdapter
public SimpleAdapter (Context context,
List<? extends Map<String, ?>> data,
int resource,
String[] from,
int[] to)
context(Context): The context where the View associated
with this SimpleAdapter is running data List: A List of Maps. Each
entry in the List corresponds to one row in the list. The Maps
contain the data for each row, and should include all the entries
specified in "from"
resource(int): Resource identifier of a view
layout that defines the views for this list item. The layout file
should include at least those named views defined in "to"
from(String): A list of column names that will be added to the Map
associated with each item.
to(int): The views that should display
column in the "from" parameter. These should all be TextViews. The
first N views in this list are given the values of the first N
columns in the from parameter.
But you used image ids array to "to" fields.
//create one simpleAdapter
SimpleAdapter myAdapter = new SimpleAdapter(
getApplicationContext(),
listitem,
R.layout.list_item,
new String[]{"touxiang", "name", "says"},
new int[]{R.id.imgtou, R.id.name, R.id.says} //You used wrong values. This should be textview ids.
);
So you cannot use SimpleAdapter with your purpose.
You should customize Adapter class to your purpose.
SimpleAdapter myAdapter = new SimpleAdapter(
getApplicationContext(),
listitem,
R.layout.list_item,
new String[]{"touxiang", "name", "says"},
new int[]{R.mipmap.head_icon1, R.mipmap.head_icon2, R.mipmap.head_icon3}
);
Or you can use Picasso Library for setting and Loading Images.

How to get away from SimpleAdapter

I have a listview with three items in it. color_id, color_name, and color_count
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="#+id/color_id"/>
<TextView android:id="#+id/color_name"/>
<TextView android:id="#+id/color_count"/>
</LinearLayout>
I load data from the server into my list. Here I'm adding data to the list manually for demonstration:
ArrayList<HashMap<String, String>> colorsList = new ArrayList();
HashMap<String, String> map = new HashMap();
map.put("id", "1");
map.put("color_name", "red");
map.put("color_count", "10");
colorsList.add(map);
map = new HashMap();
map.put("id", "2");
map.put("color_name","yellow"):
map.put("color_count","15");
colorsList.add(map);
ListAdapter adapter = new SimpleAdapter(
MyActivity.this, colorsList,
R.layout.list_item_colors, new String[] {"id",
"color_name","color_count"}, new int[] {
R.id.color_id, R.id.color_name, R.id.color_count});
setListAdapter(adapter);
Question
All this is working fine for me. However, for other reasons I want to part ways with SimpleAdapter and instead use ArrayAdapter However, with ArrayAdapter
I'm not sure how to set the three items I have : color_id, color_name, and color_count
You will need to create your custom ArrayAdapter and ovveride getView() method , this method which called each time you create item in list view and in it you will inflate your layout (which contain textviews )and attach its data
there is good tutorial about ListView and custom one
http://www.vogella.com/tutorials/AndroidListView/article.html
and feel free to feed me back in any thing not obvious for you
You can use SimpleAdapter.This is my demo.
The MainActivity.java:
public class MainActivity extends Activity{
private ArrayList<HashMap<String, String>> colorsList;
private ListView list;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=(ListView)findViewById(R.id.listcolor);
colorsList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", "1");
map.put("color_name", "red");
map.put("color_count", "10");
colorsList.add(map);
map = new HashMap<String, String>();
map.put("id", "2");
map.put("color_name","yellow");
map.put("color_count","15");
colorsList.add(map);
SimpleAdapter adapter=new SimpleAdapter(this,colorsList,R.layout.list_item_colors,
new String[] {"id","color_name","color_count"},
new int[] {R.id.color_id, R.id.color_name, R.id.color_count});
list.setAdapter(adapter);
}
}
the XMl:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="#+id/listcolor"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
the list_item_colors.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/color_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/color_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/color_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

MultiColumn List with drop down spinner

I have a mutlicolumn list in which I also want one of the columns to include a drop down spinner.
My main.xml is: -
<!-- main.xml -->
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/SCHEDULE" android:layout_width="wrap_content" android:layout_height="wrap_content">
</ListView>
My row xml is
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="#+id/LEVEL_CELL"
android:layout_width="50dip"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/ACTION_CELL"
android:layout_width="50dip"
android:layout_height="wrap_content"/>
<Spinner
android:id="#+id/fromTables"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Here is my logic: -
super.onCreate(savedInstanceState);
setContentView(R.layout.row);
fromTablesSpinner = (Spinner) findViewById(R.id.fromTables);
setContentView(R.layout.main);
ListView list = (ListView) findViewById(R.id.SCHEDULE);
ArrayList<String> tables = new ArrayList<String>();
tables.add("MARA");
tables.add("MARC");
tables.add("MARD");
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("level", "1");
map.put("action", "INNER JOIN");
map.put("tables", "MARA");
map.put("tables", "MARC");
mylist.add(map);
map = new HashMap<String, String>();
map.put("level", "2");
map.put("action", "OUTER JOIN");
map.put("tables", "MARA");
map.put("tables", "MARC");
mylist.add(map);
SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.row,
new String[] { "level", "action" }, new int[] {
R.id.LEVEL_CELL, R.id.ACTION_CELL });
list.setAdapter(mSchedule);
}
The use of SimpleAdapter does not allow me to bind to a spinner view.
Any help is much appreciated.
Thanks
Martin
You are trying two complicated things at once. Follow these steps:
1) Learn to build a custom adapter which shows more than one type of views in a single list. This will help you to show a spinner in the list.
Links : http://www.ezzylearning.com/tutorial.aspx?tid=1763429
Android ListView with different layouts for each row
2) Learn to handle different view and listeners.
Links: Listview with TextView and Button. RowId of the clicked button (btnButtonOFF.setOnClickListener(new ItemClickListener(cur.getInt(idRow)));)
3) Use Multiple ListViews like you are using.

Android adding image to dynamic listview

I have already created a dynamic list view that passes text to a SimpleAdapter, however I would like to pass information to make an image appear from the drawable folder. Here is the XML layout for each row:
<ImageView
android:id="#+id/sport_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="2dp"
android:textSize="20dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right" >
<TextView
android:id="#+id/item_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="13dp" />
</LinearLayout>
</LinearLayout>
and the bit of code creating the list:
//fill in the list items from the XML document
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("id", XMLfunctions.getValue(e, "fixture_id"));
map.put("sport", XMLfunctions.getValue(e, "sport"));
map.put("teams", XMLfunctions.getValue(e, "team_2") + " v " + XMLfunctions.getValue(e, "team_2"));
mylist.add(map);
}
//Make a new listadapter
ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.fixture, new String[] { "sport", "teams" }, new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
Now at the moment when I pass the string sport it just displays the text such as 'football' in the item_title text view. But as I know what the sport is I would like to be able to display an icon in the sport_icon imageview. An example of the sport icon location would be #drawable/icon_football.
Many thanks.
The answer here was creating a BaseAdapter with the ListView as suggested by Frank below. If anyone else is looking to do the same thing but has no idea where to start I highly recommend watching http://www.youtube.com/watch?v=pVs4qKmenQM. This video is well put together and explains everything you need to know.
:)
You are going to have to extend something like BaseAdapter and check what your position is in getView() and then accordingly set what image you would like to use.
SimpleAdapter only works nice if ALL the items are have the exact same format and it's very simple.
Here is an ok example.
http://thinkandroid.wordpress.com/2010/01/13/custom-baseadapters/
But I would really recommend watching the Romain and Adam's Google IO Video
http://www.youtube.com/watch?v=wDBM6wVEO70

How to use Multiple list view in android?

I want to display three kind of different datas in list view.How to create a three list view and also how to implement in java code through Iconic Adapter.
I have done like this
ArrayList<Object> routeList = getWmRoute();
ArrayList<HashMap<String,String>> alist=new ArrayList<HashMap<String,String>>();
for(int i = 0; i<routeList.size();i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("RetailerCode", ((WMRoute) routeList.get(i)).getDescription());
map.put("RetailerName", ((WMRoute) routeList.get(i)).getBusinessUnit());
alist.add(map);
}
ListView list= getListView();
sd = new SimpleAdapter(this,alist,R.layout.retalier_rows,new String[]{"RetailerCode","RetailerName"},new int[]{R.id.retailerCode,R.id.retailerName});
list.setAdapter(sd);
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setSelected(true);
list.setSelection(0);
list.setTextFilterEnabled(true);
list.setItemsCanFocus(true);
list.setTextFilterEnabled(true);
list.setItemChecked(positions,true);
and your xml file create(retalier_rows.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="RetailerCode"
android:id="#+id/retailerCode"
android:layout_width="125dp"
android:layout_height="35dp">
<TextView android:text="RetailerName"
android:layout_width="125dp"
android:layout_height="35dp"
android:id="#+id/retailerName"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/retailerCode"
android:layout_marginLeft="15dp"
android:layout_alignParentRight="true"></TextView>
If you need 3 column you can add it new & code also can specify

Categories

Resources