Specific Listview -> Android - android

Does somebody knows how to make similar listview?
How to split row?
Image: link text
Thanks for answers

Something like
rows.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/icon1"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_alignParentLeft="true"
android:src="#drawable/icon"
/>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/icon2"
android:layout_toRightOf="#+id/icon1"
android:text="Some Text"
/>
<ImageView
android:id="#+id/icon2"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_alignParentRight="true"
android:src="#drawable/icon"
/>
</RelativeLayout>
Then populate the ListView with your data (depending which Adapter you use, here an example for an SimpleCursorAdapter):
Cursor c = getContentResolver().query(uri, null, null, null, null, null);
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
R.layout.row, c,
new String[] {
"icon"
,"text"
,"icon2"
},
new int[] {
R.id.icon1
,R.id.text
,R.id.icon2
}
);

Related

Android RelativeLayout Simple Cursor Adapter

I am using Simple Cursor Adapter to generate views in the adapter.The below is the code to generate views.
String[] from = new String []
{DatabaseHelper.COLUMN_CATEGORY,DatabaseHelper.COLUMN_TASKNAME,DatabaseHelper.COLUMN_CROSSED};
int[] to = new int[] {android.R.id.text1, android.R.id.text2, android.R.id.content};
adapter = new SimpleCursorAdapter(context, R.layout.item_todo, data, from, to,1);
lv.setAdapter(adapter);
I am getting the below error.
android.widget.RelativeLayout is not a view that can be bounds by this SimpleCursorAdapter
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="10dip"
android:paddingTop="10dip" >
<ImageView
android:id="#android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" />
<TextView
android:id="#android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#android:id/icon"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#android:id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/text1"
android:layout_toRightOf="#android:id/icon"
android:textAppearance="?android:attr/textAppearanceSmall" />

SimpleCursorAdapter and ListView app crashes

I have a problem displaying an records from my db in listview. When i start my app it crash at the beggining.This is my code :
dbHelper = new DbAdapter(MainActivity.this);
dbHelper.open();
cursor = dbHelper.fetchAllContacts();
startManagingCursor(cursor);
String[] columns = new String[] {
DbAdapter.KEY_NAME,
DbAdapter.KEY_SURNAME,
DbAdapter.KEY_SEX,
DbAdapter.KEY_BIRTH_DATE
};
int[] to = new int[] {
R.id.name,
R.id.cognome,
R.id.sesso,
R.id.datanascita,
};
SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(
MainActivity.this, R.layout.contatti,
cursor,
columns,
to,
0);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(dataAdapter);
cursor.close();
dbHelper.close();
This is the my layout for the listview :
contatti.xml
<?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="wrap_content"
android:orientation="vertical"
android:padding="6dip" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Nome : "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:text="Cognome : "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:text="Sesso : "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_below="#+id/textView3"
android:text="Data nasc. : "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView3"
android:layout_alignBottom="#+id/textView3"
android:layout_toRightOf="#+id/textView3"
android:text="TextView" />
<TextView
android:id="#+id/cognome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView4"
android:layout_alignBottom="#+id/textView4"
android:layout_alignLeft="#+id/continent"
android:text="TextView" />
<TextView
android:id="#+id/sesso"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView3"
android:layout_toRightOf="#+id/textView3"
android:text="TextView" />
<TextView
android:id="#+id/datanascita"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView2"
android:layout_alignLeft="#+id/name"
android:text="TextView" />
</RelativeLayout>
You should try running your app without using cursor.close() on the cursor and DBHelper.The SimpleCursorAdapter needs it to be open so it can manage the list view.

change textview style in ExpandableListView

i want to change the style of textview of group and child item in ExpandableListView , but it doesn't work .
in the creating adapter first text view will be (lblListHeader ) and second will be (lblListItem )
mAdapter = new HadithExpandableListAdapter(mGroupsCursor, this,
R.layout.drawer_list_ahadith, R.layout.drawer_list_ahadith,
new String[] { "ChapterName" },
new int[] { R.id.lblListHeader }, new String[] { "HadithTitel" },
new int[] { R.id.lblListItem });
in the layout this is file which contain two textview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:orientation="vertical" >
<TextView
android:id="#+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dip"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
<TextView
android:id="#+id/lblListHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dip"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
</LinearLayout>

Customized ListView with TextView, TextView, RadioGroup in Android

I'm developing an application with ListView for Student Marklist creation.
In this application, the List have 10 students. There are four grades provided for the exam which was conducted to those Students. One student can adapt only one grade from the four.
The teacher will assign the grade to the student in ListView.
My xml file Studentlist.xml is as follow:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
and my row.xml file is as follow:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="500dp"
android:layout_height="fill_parent"
android:padding="10dp" >
<TableRow>
<TextView
android:id="#+id/SNo"
style="#style/text"
android:layout_width="40dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/StudNo"
style="#style/text"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/StudName"
style="#style/text"
android:layout_width="180dp"
android:layout_height="wrap_content" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:checked="true" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="40dp"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="40dp"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/radio3"
android:layout_width="40dp"
android:layout_height="wrap_content" />
</RadioGroup>
</TableRow>
Now I'm trying for the output in the form of:
1 0001 AAAA <RadioButton1> <RadioButton2> <RadioButton3> <RadioButton4>
2 0002 BBBB <RadioButton1> <RadioButton2> <RadioButton3> <RadioButton4>
How can I use the Adapter functionalities?
ArrayAdapter<String,String,String,RadiGroup> studList=new ArrayAdapter<String,String,String,RadiGroup>();
Can I use like this, and how to develop the customized Adapter for the ListView?
Suggest me for the best solution!
This is a sample example of custom adapter for listview. Modify it as your need:
XML layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/main"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Header -->
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black" >
<TextView
android:id="#+id/item2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:height="30dip"
android:text="Latest News - Your Healing Place"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:width="100dip" />
<TextView
android:id="#+id/item1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:height="30dip"
android:width="20dip" />
</LinearLayout>
<View android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="?android:attr/listDivider" />
<LinearLayout android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/black" >
</ListView>
</LinearLayout>
</LinearLayout>
news.java :
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
String[] from = new String[] {"details", "date"};
int[] to = new int[] { R.id.item1, R.id.item2};
ListView lv= (ListView)findViewById(R.id.listview);
HashMap<String, String> map = new HashMap<String, String>();
map.put("details", "This is a sample message");
map.put("date", "24-07-2003");
fillMaps.add(map);
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
lv.setAdapter(adapter);
You can use a class inheriting from ArrayAdapter, and overriding its getView() method.
public class StudentAdapter extends ArrayAdapter<Student> {
protected LayoutInflater inflater;
public StudentAdapter(final Context context) {
super(context, 0);
inflater = (LayoutInflater) ((Context) context)
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent) {
// Note: You should optimize here with re-using convertView
View rowView = inflater.inflate(R.layout.user_address_row_layout,
parent, false);
TextView sNo = (TextView) rowView.findViewById(R.id.sNo);
sNo.setText(getItem(position).number);
// same for every field of the row
// ...
return rowView;
}
}

Textview display in Android from sqlite

In my application I have one listview, in that I am displaying data from database.
Following is my code:
for(int i=1;i<=db.getAllTitles().getCount();i++)
{
String cat=db.getTitle(i).getString(5).toString();
Cursor c = db.gethouseholdTitle();
startManagingCursor(c);
if(cat.equals("Income"))
{
// System.out.println("inside if="+select);
// Cursor cin = db.income();
from = new String[] { db.KEY_INCOME,db.KEY_DESC,db.KEY_CATEGORY,db.KEY_QUANTITY,db.KEY_TOTAL};
to = new int[] {R.id.text1 ,R.id.text3,R.id.text5,R.id.text7,R.id.text9};
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.columnview, c, from, to);
// System.out.println("notes="+notes.getCount());
// setListAdapter(notes);
lv.setAdapter(notes);
}
else
{
String catexp=db.getTitle(i).getString(5).toString();
Cursor cexp = db.gethouseholdTitleExp();
System.out.println("inside else="+cexp.getCount());
from = new String[] { db.KEY_INCOME,db.KEY_DESC,db.KEY_CATEGORY,db.KEY_QUANTITY,db.KEY_TOTAL};
to = new int[] {R.id.text2 ,R.id.text4,R.id.text6,R.id.text8,R.id.text10};
SimpleCursorAdapter notesexp =
new SimpleCursorAdapter(this, R.layout.columnview, cexp, from, to);
// System.out.println("notes="+notes.getCount());
// setListAdapter(notes);
lv.setAdapter(notesexp);
}
}
My xml file:
<?xml version="1.0" encoding="utf-8"?>
<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="vertical">
<TextView android:id="#+id/text1"
android:layout_width="30dip"
android:layout_height="wrap_content"
android:text="Price:"/>
<TextView android:id="#+id/text2"
android:layout_width="110dip"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="-20dp"
android:layout_weight="1"/>
<TextView android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/text4"
android:layout_marginTop="-20dp"
android:layout_width="110dip"
android:layout_height="wrap_content"
android:layout_marginLeft="180dp"
android:layout_weight="1"/>
<TextView android:id="#+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/text6"
android:layout_marginTop="-20dp"
android:layout_width="110dip"
android:layout_height="wrap_content"
android:layout_marginLeft="180dp"
android:layout_weight="1"/>
<TextView android:id="#+id/text7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/text8"
android:layout_marginTop="-20dp"
android:layout_width="110dip"
android:layout_height="wrap_content"
android:layout_marginLeft="180dp"
android:layout_weight="1"/>
<TextView android:id="#+id/text9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/text10"
android:layout_marginTop="-20dp"
android:layout_width="110dip"
android:layout_height="wrap_content"
android:layout_marginLeft="180dp"
android:layout_weight="1"/>
<TextView
android:id="#+id/textView21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</LinearLayout>..
In this, only income data get displayed. Nothing is displaying for expense. I checked database for expense count, it gives exact count. So data is present in database, but not get displayed. Is there any problem in my design?
In listview only possible to load one items only. Not supported to load more than one. Please try to display via grid view with the same syntax. If you want to try in list view then you should add the columns via Base adapter to list view.
Please see the given tutorial link for base adapter.It will help you,
http://developerlife.com/tutorials/?p=327
http://pradeep-sharma.com/blog/android-custom-listview-baseadapter-tutorial/
Draw the Layout by this given coding,
showlist.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content" >
<TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/Show">
<TextView android:layout_width="50dp" android:layout_height="20dp" android:textColor="#ff8000"
android:id="#+id/Entry" android:layout_weight="1" android:gravity="left"/>
<TextView android:layout_width="50dp" android:layout_height="20dp" android:textColor="#ff8000"
android:id="#+id/sales" android:layout_weight="1" android:gravity="right" />
</TableRow>
You can add the any no of textview here,I think you want to add four textview so you has to change the textview width as 25dp.
GridView set:
Then set simplecursoradapter for gridview coding as given
ada=new SimpleCursorAdapter(context, R.layout.showlist, mon,new String[] { cname, "_id" }, new int[] { R.id.Entry,R.id.sales })
gridview.setadapter(ada);
//It will work well.
The main thing is you should create the gridview with mention the column is one.
The Result will be

Categories

Resources