MultiColumn List with drop down spinner - android

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.

Related

How to get a specific value which is displayed in ListView?

In my project, I've a database in which I've a table called Student. In that table I've attributes as ROLL_NO, FIRST_NAME, LAST_NAME, CONTACT, CLASS_NAME, PASSWORD, EMAIL_ID, GENDER.
I'm reading all students data from database and showing only "FIRST_NAME, LAST_NAME and ROLL_NO" in a listview. The code is as follows:
listView = (ListView) findViewById(R.id.id_student_list);
db = new DatabaseHelper(this);
ArrayList<HashMap<String, String>> Items = new ArrayList<HashMap<String, String>>();
// Reading all contacts
Log.d("Reading: ", "Reading all students..");
List<Student> studs = db.getAllStudents();
for (Student cn : studs) {
// Writing values to map
HashMap<String, String> map = new HashMap<String, String>();
map.put("firstname", cn.getFirstname());
map.put("lastname", cn.getLastname());
map.put("roll", Integer.toString(cn.getRoll_no()));
Items.add(map);
}
// Adding Items to ListView
ListAdapter adapter = new SimpleAdapter(this, Items,
R.layout.studname_listview, new String[] { "firstname", "lastname", "roll" },
new int[] {R.id.firstname, R.id.lastname, R.id.rollnumber }); <---- this
listView.setAdapter(adapter);
When I click on a particular listview item, I want to get the value that will be going inside "R.id.rollnumber". I've implemented setOnItemClickListener on listview and it's working but I don't know how to get the value that will be shown on the textview "R.id.rollnumber" whenever I click on any item of the listview.
This is my listview elements xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/firstname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10sp"
android:text="sameer"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>
<TextView
android:id="#+id/lastname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/firstname"
android:layout_alignBottom="#+id/firstname"
android:layout_toRightOf="#+id/firstname"
android:text="waskar"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/rollnumber" <!-- the value which will be displayed in here -->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/lastname"
android:layout_alignBottom="#+id/lastname"
android:layout_alignParentRight="true"
android:layout_marginRight="22dp"
android:textStyle="bold" />
</RelativeLayout>
Hashmap? you should create special class for student info and override OnItemClick method for adapter

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"/>

Listview in Listview -> which adapter should be used (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

Listview relativelayout simpleadapter not working

I am trying to display some items in a listview, which is located in a relativelayout.
The whole layout is located in one xml file (main.xml)
The layout has also 2 buttons and 2 more textfields (EditText).
I can`t get it to work, as:
The listview does not show the items
If the listview expands, it duplicates the buttons and textfields present in the layout
can someone please help me?
Here is a part of the layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/kpnback">
<EditText
android:layout_height="wrap_content"
android:layout_width="110dp"
android:id="#+id/editText1"
android:text="edit1"
android:inputType="textMultiLine"
android:layout_below="#+id/lbl_top">
</EditText>
<EditText
android:layout_height="wrap_content"
android:layout_width="110dp"
android:id="#+id/editText2"
android:text="edit2"
android:inputType="textMultiLine"
android:layout_marginLeft="160dp"
android:layout_below="#+id/lbl_top">
</EditText>
<ListView
android:layout_width="fill_parent"
android:layout_height="20dp"
android:id="#+id/kpn"
android:layout_y="350dp"
android:layout_below="#+id/editText1">
</ListView>
</RelativeLayout>
Code for the simpleadapter:
#Override
protected void onPostExecute(String result) {
// create the grid item mapping
ListView kp = (ListView)findViewById(R.id.kpn);
String[] from = new String[] {"col_1", "col_2"};
int[] to = new int[] { R.id.editText1, R.id.editText1 };
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
Document doc = Jsoup.parse(kpn);
Elements tdsFromSecondColumn = doc.select("table.personaltable td:eq(0)");
Elements tdsFromSecondColumn1 = doc.select("table.personaltable td:eq(1)");
for (Element tdFromSecondColumn : tdsFromSecondColumn) {
map.put("col_1", tdFromSecondColumn.text());
fillMaps.add(map);
System.out.println(tdFromSecondColumn.text());
}
for (Element tdFromSecondColumn1 : tdsFromSecondColumn1) {
map.put("col_2", tdFromSecondColumn1.text());
fillMaps.add(map);
System.out.println(tdFromSecondColumn1.text());
}
SimpleAdapter adapter = new SimpleAdapter(AndroidLogin.this, fillMaps, R.layout.main, from, to);
kp.setAdapter(adapter);
1) Do not use "#+id/..." for the layout_below attribute.. Instead use "#id/..."
2) lbl_top is not present inside the RelativeLayout..
3) You'll have to specify layout_toLeftOf or layout_toRightOf, apart from specifying layout_below.
Hope this helps..

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