Listview and 2nd xml - android

I have a list_view_row xml file.
The list_view_row.xml contains editText boxes, i want to use them to display the items (collected by a SimpleAdapter) in the listbox in my main program (Android.java).
Eclipse won`t let me compile the code as i need to declare these editText boxes in Android.java
int[] to = new int[] { R.id.editText1, R.id.editText1 };
How do i do that?
Thank you in advance.
The list_view_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:width="100dp"
android:height="100dp"
>
<EditText>
android:inputType="textMultiLine"
android:id="#+id/editText1"
android:layout_height="wrap_content"
android:text="edit1"
android:layout_width="110dp">
</EditText>
<EditText>
android:layout_height="wrap_content"
android:layout_width="110dp"
android:id="#+id/editText2"
android:text="edit2"
android:inputType="textMultiLine"
android:layout_marginLeft="50dp"
</EditText>
</LinearLayout>
The code in main.xml:
#Override
protected void onPostExecute(String result) {
//publishProgress(false);
// 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("Hashmap: " + map);
System.out.println(tdFromSecondColumn.text());
}
for (Element tdFromSecondColumn1 : tdsFromSecondColumn1) {
map.put("col_2", tdFromSecondColumn1.text());
fillMaps.add(map);
System.out.println("Hashmap: " + map);
System.out.println(tdFromSecondColumn1.text());
}
SimpleAdapter adapter = new SimpleAdapter(AndroidLogin.this, fillMaps, R.layout.list_view_row, from, to);
kp.setAdapter(adapter);

Your XML is incorrect, here is the correct format:
<EditText android:inputType="textMultiLine"
android:id="#+id/editText1"
android:layout_height="wrap_content"
android:text="edit1"
android:layout_width="110dp"/>
Once you correct this, build your project and the R.id.editText1 and R.id.editText2 should be recognized in code.
EDIT: Hint - go to your Project menu at the top, and then down to "Build Automatically." This will cause your project to build upon saving a file (java / xml). After you modify the XML, and save, it will build and make your editText1 resource available to access by R.id.editText1.

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

Implementing simpleadapter using HashMap

I am retrieving data from sqlite and showing it in a listview using simpleadapter.I don't have any issues regarding Layouts.Still just for your reference :
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<!-- Name Label -->
<TextView
android:id="#+id/subject"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="#421fc4"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/day"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:textStyle="bold" />
<TextView
android:id="#+id/slot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColorHighlight="#782086"
android:textStyle="bold" />
<TextView
android:id="#+id/instructor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#782086"
android:textStyle="bold" />
</LinearLayout>
I am having problem somewhere in the following code snippet.I tried to retrieve the data from sqlite using normal listview and it worked perfect.But when i used this code snippet for custom listview using SimpleAdapter i get only the last row of the result of the query.That last row's data gets repeated 8 times in the listview as there are 8 rows in the result of the query.
HashMap<String, String> hashMap = new HashMap<String, String>();
ArrayList<HashMap<String, String>> Timetablelist;
//c being the cursor
if (c.moveToFirst()) {
do {
String subject = c.getString(c.getColumnIndex(dba.KEY_SUBJECT));
String day = c.getString(c.getColumnIndex(dba.KEY_DAY));
String slot = c.getString(c.getColumnIndex(dba.KEY_SLOT));
String instructor = c.getString(c.getColumnIndex(dba.KEY_INSTRUCTOR_NAME));
// adding each child node to HashMap key => value
hashMap.put("Subject", subject);
hashMap.put("Day", day);
hashMap.put("Slot", slot);
hashMap.put("Instructor", instructor);
Timetablelist.add(hashMap);
ListAdapter adapter = new SimpleAdapter(
this, Timetablelist,R.layout.list_item, new String[] { "Subject", "Day",
"Slot","Instructor" }, new int[] { R.id.subject,
R.id.day, R.id.slot,R.id.instructor });
setListAdapter(adapter);
}while (c.moveToNext());
}
P.S : I was able to retrieve data using normal listview.So database and other layouts are working fine.
Move the below out of do While
ListAdapter adapter = new SimpleAdapter(
this, Timetablelist,R.layout.list_item, new String[] { "Subject", "Day",
"Slot","Instructor" }, new int[] { R.id.subject,
R.id.day, R.id.slot,R.id.instructor });
setListAdapter(adapter);
You get the data from database and you can populate Timetablelist in the do while. But there is no need to initialize adapter everytime and setListAdapter.
Edit:
HashMap<String, String> hashMap ;
ArrayList<HashMap<String, String>> Timetablelist = new ArrayList<HashMap<String,String>>();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
String subject = c.getString(c.getColumnIndex(dba.KEY_SUBJECT));
String day = c.getString(c.getColumnIndex(dba.KEY_DAY));
String slot = c.getString(c.getColumnIndex(dba.KEY_SLOT));
String instructor = c.getString(c.getColumnIndex(dba.KEY_INSTRUCTOR_NAME));
// adding each child node to HashMap key => value
hashMap = new HashMap<String, String>();
hashMap.put("Subject", subject);
hashMap.put("Day", day);
hashMap.put("Slot", slot);
hashMap.put("Instructor", instructor);
Timetablelist.add(hashMap);
}
ListAdapter adapter = new SimpleAdapter(
this, Timetablelist,R.layout.list_item, new String[] { "Subject", "Day",
"Slot","Instructor" }, new int[] { R.id.subject,
R.id.day, R.id.slot,R.id.instructor });
setListAdapter(adapter);

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.

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