Implementing simpleadapter using HashMap - android

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

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

SimpleCursorAdapter will not populate my ListView, no error, just nothing happens

I tried to populate a listview from a cursor so I use this code:
Cursor curVal = db.rawQuery("SELECT * FROM category ORDER BY order_cat ASC", null);
String[] inVal = new String[] { "nume_cat", "order_cat" };
int[] outVal = new int[] { R.id.title_cat, R.id.duration };
SimpleCursorAdapter cadapter = new SimpleCursorAdapter(this, R.layout.list_row, curVal, inVal, outVal);
ListView listView = (ListView) findViewById(R.id.mainListView);
listView.setAdapter(cadapter);
listView.setOnItemClickListener(this);
here are my XML`s:
List_row.xml
......................
<TextView
android:id="#+id/title_cat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:text="FILL CAT NAME HERE"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15dip"
android:textStyle="bold"/>
<TextView
android:id="#+id/duration"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/title_cat"
android:textColor="#343434"
android:textSize="10dip"
android:layout_marginTop="1dip"
android:layout_toRightOf="#+id/thumbnail"
android:text="FILL DURATION HERE" />
.....................
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="#+id/title_main"
android:text="TITLE HERE"
android:padding="15dip"
android:textSize="18dip"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dip"
android:id="#+android:id/mainListView">
</ListView>
</LinearLayout>
The problem :
When I run the app nothing happens and I can't see my listview. Eclipse don't return me any error or warning in logcat...
I tryed the easy method, using ArrayAdapter and all works ok, but I need to use cursoradapter because I need to have more than 1 textview on each list item.
Here is the working ArrayAdapter:
ArrayList<String> catList = new ArrayList<String>();
ListView listView = (ListView) findViewById( R.id.mainListView );
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.list_row, R.id.title_cat, catList);
Cursor curVal = db.rawQuery("SELECT * FROM category ORDER BY order_cat ASC", null);
if (curVal.moveToFirst()) {
do {
String data = curVal.getString(curVal.getColumnIndex("nume_cat"));
listAdapter.add(data);
} while (curVal.moveToNext());
}
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(this);
Now lets exclude any questions:
Yes, my cursor contains an _id field.
Yes, I doublechecked my cursor and it has 20 rows.
System.out.println("Cursor adapter has " + cadapter.getCount() + " lines.");
prints "Cursor adapter has 18 lines."
Is possible to be from:
int[] outVal = new int[] { R.id.title_cat, R.id.duration };
and
SimpleCursorAdapter cadapter = new SimpleCursorAdapter(this, R.layout.list_row, curVal, inVal, outVal);
NEED TO REPLACE WITH ?
int[] outVal = new int[] { android.R.id.title_cat, android.R.id.duration };
and
SimpleCursorAdapter cadapter = new SimpleCursorAdapter(this, android.R.layout.list_row, curVal, inVal, outVal);
But how I define the ID as android.R.id.title_cat instead of R.id.title_cat?
If I put in my xml
<TextView
android:id="#+android:id/title_cat"
In my class file eclipse retuns me an error "title_cat cannot be resolved or is not a field"
I think you have problem at XMl. Check id of your listview.
android:id="#+android:id/mainListView">
Give id of listview as
android:id="#+android:id/list"
Then give
SimpleCursorAdapter cadapter = new SimpleCursorAdapter(this, R.layout.list_row, curVal, inVal, outVal);
setListAdapter(cadapter);
instead of
ListView listView = (ListView) findViewById(R.id.mainListView);
listView.setAdapter(cadapter);
Also your class should extend ListActivity

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: when i connect my listview to database i lost the styling

when i connect my listview to database i lost the listview style.
and when i add the style i see same data on all of the listview.
public void update_list(String NN) {
//HashMap<String,String> item = new HashMap<String,String>();
db_results = new ArrayList<String>();
Cursor cursor = db.rawQuery(NN, null);
while(cursor.moveToNext()) {
//item.put( "line1",String.valueOf(cursor.getString(cursor.getColumnIndex("A"))));
//item.put( "line2",String.valueOf(cursor.getString(cursor.getColumnIndex("B"))));
//item.put( "line3",String.valueOf(cursor.getString(cursor.getColumnIndex("C"))));
//list.add( item );
db_results.add(String.valueOf(cursor.getString(cursor.getColumnIndex("A"))) + "\n" +cursor.getString(cursor.getColumnIndex("B")) + "\n" +cursor.getString(cursor.getColumnIndex("C")));
}
cursor.close();
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,db_results));
option1: like this code i see the data on my list but i lost the styling.
option2: i un remark the remarked code and i see the styling but same data on all of the list
You'd be much better off to create a custom layout and use a SimpleCursorAdapter to populate it.
threelinelayout.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:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/ListItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/ListItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/ListItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
adapter code
Cursor mCursor = db.rawQuery(NN, null);
this.startManagingCursor(cursor);
String[] from = new String[] { "A", "B", "C" };
int[] to = new int[] { R.id.ListItem1, R.id.ListItem2, R.id.ListItem3 };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(getActivity(),
R.layout.threelinelayout, mCursor, from, to);
setListAdapter(mAdapter);

Categories

Resources