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
Related
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" />
I Have a ListView. In my listview I create a custom items on every rows. this custom item as some textviews and two buttons. The problem I have now is I want to detect when the user clicks on the buttons in the listview items.
But My code is not working.
Please don't mind the numerouse text views that are to be hidden from the user only one is to be seen by the user and the two buttons are visible.
Each time I click the button nothing happens, no response
My listview custom item is pasted below.
Thanks for reading my post, am also grateful for your time and effort.
<?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:background="#drawable/listitemdesign"
android:orientation="vertical"
android:padding="10dp"
android:layout_marginBottom="10dp">
<TextView
android:id="#+id/itemprescrip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="gone"
/>
<TextView
android:id="#+id/agerange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:visibility="gone"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/itemanufac"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:visibility="gone"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/itemexdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:visibility="gone"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/itemdcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:visibility="gone"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/itemnafno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:visibility="gone"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/drugID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:visibility="gone"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/plus"
android:layout_marginTop="8dp"
/>
<TextView
android:id="#+id/itemname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:layout_margin="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<View android:layout_width="wrap_content" android:layout_height="2dip"
android:background="#color/wine"
android:layout_marginTop="5dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/ordernow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/whitebackground"
android:src="#drawable/ordernow"
android:layout_marginTop="15dp"
/>
<ImageButton
android:id="#+id/details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/detailsicon"
android:layout_marginTop="15dp"
android:layout_marginLeft="10dp"
android:background="#drawable/whitebackground"
/>
</LinearLayout>
</LinearLayout>
My activity code is also pasted below. The OnItemClickListener of the ListView component, is what is below :
private AdapterView.OnItemClickListener itemclickListener = new AdapterView.OnItemClickListener(){
String drugID = null,drugName = null, drugPrecription = null, drugManufacturedate = null, drugExpiratorydate = null,drugCode = null, nafdacNo=null,drugageRange = null;
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
drugID = ((TextView)view.findViewById(R.id.drugID)).getText().toString();
drugName = ((TextView)view.findViewById(R.id.itemname)).getText().toString();
drugPrecription =((TextView)view.findViewById(R.id.itemprescrip)).getText().toString();
drugManufacturedate = ((TextView)view.findViewById(R.id.itemanufac)).getText().toString();
drugExpiratorydate = ((TextView)view.findViewById(R.id.itemexdate)).getText().toString();
drugCode = ((TextView)view.findViewById(R.id.itemdcode)).getText().toString();
nafdacNo = ((TextView)view.findViewById(R.id.textViewName)).getText().toString();
drugageRange = ((TextView)view.findViewById(R.id.agerange)).getText().toString();
orderButton = (ImageButton)parent.getChildAt(9);//view.findViewById(R.id.ordernow);
detailsButton = (ImageButton)parent.getChildAt(0);//view.findViewById(R.id.details);
Object o = orderButton.getClass();
Toast.makeText(context, o.toString(), Toast.LENGTH_SHORT).show();
//Now we are going check for the button that would clicked
//int elementID = view.getId();
//this.detectItemClicked(elementID);
}
my app needs to display data in a list, I walked through some tutorials and figured out to use a header.xml and row.xml. It works fine but now I would like to add more stuff on my header.xml and I found out that the entire header.xml is scrolling with the row.xml as well, which I dont really want.
Any solution that doesn't require me to rewrite and change my code style completely?
Activity:
public class HistoryActivity extends ListActivity
{
private static final String TAG = "HistoryActivity";
ListView lv;
SimpleAdapter sd;
RecordDAO dao = new RecordDAO(HistoryActivity.this);
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
lv = getListView();
lv.addHeaderView(getLayoutInflater().inflate(
R.layout.header, null, false));
}
#Override
protected void onResume()
{
super.onResume();
ArrayList<Record> Records = new ArrayList<Record>();
Records = (ArrayList<Record>) dao.findAll();
ArrayList<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for (int x = Records.size()-1; x >=0; x--)
{
map = new HashMap<String, String>();
map.put(....// all my data
aList.add(map);
}
sd = new SimpleAdapter(this, aList, R.layout.row,
new String[]
{ "date", "name", "time",
"rating" }, new int[]
{ R.id.date, R.id.name, R.id.time,
R.id.rating});
lv.setAdapter(sd);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3)
{
TextView tx = (TextView) view.findViewById(R.id.date);
String s = tx.getText().toString();
Intent intent = new Intent(HistoryActivity.this, EditRecordActivity.class);
intent.putExtra("date", s);
startActivity(intent);
}
});
}
private void insertNewRecord()
{
dao.add(newRecord);
}
}
header.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Date"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="name"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Time"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Rating"
android:textColor="#FFFFFF"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
row.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:id="#+id/date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/Rating"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FFFFFF" />
</LinearLayout>
Don't use ListActivity. I think it is a bad practice. Use a regular Activity. Just insert a ListView with an id "#android:id/list" into header.xml.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Date"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="name"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Time"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Rating"
android:textColor="#FFFFFF"
android:textSize="16dp" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
And you can get the list reference by doing:
ListView lv = (ListView)findViewById(R.id.list);
Setting the Header of a ListView means that it will be the first item of your list, but still will scroll with the list itself.
In your case, you can create some kind of layout (similar to header.xml) and place it above the ListView.
Instead of ListActivity, extend from Activity
Create a main_layout.xml that define some views and a ListView.
main_layout
<include header.xml/>
<listview>
If you don't want your header and footer views to scroll with your ListView, then make sure that you use the addHeaderView() and addFooterView() BEFORE you call your setAdapter() method. I would recommend moving the setAdapter() method to your onCreate() method.
I have a ListView:
SimpleAdapter adapter = new SimpleAdapter(
getApplicationContext(), forecastList,
R.layout.weather_list_row, new String[] { "forecast",
"precipitation", "temperature" }, new int[] {
R.id.forecast, R.id.precipitation,
R.id.temperature });
lv1.setAdapter(adapter);
and I have a "R.layout.weather_list_row" as:
<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/forecast" android:layout_width="150dip"
android:layout_height="wrap_content" android:textColor="#000000" />
<LinearLayout android:layout_height="wrap_content"
android:layout_weight="1" android:layout_width="50dip">
<TextView android:id="#+id/precipitation"
android:layout_width="wrap_content" android:textColor="#00357A"
android:layout_height="wrap_content" />
<ImageView android:layout_height="wrap_content" android:id="#+id/weather_status1"
android:layout_width="wrap_content" />
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:layout_weight="1" android:layout_width="50dip">
<TextView android:id="#+id/temperature" android:layout_width="wrap_content"
android:textColor="#00357A" android:layout_height="wrap_content" />
<ImageView android:layout_height="wrap_content" android:id="#+id/weather_status2"
android:layout_width="wrap_content" />
</LinearLayout>
</LinearLayout>
In the R.layout.weather_list_row I am dynamically changing the src= of the ImageView.
Question: How can I notify the ListView, when I change the
values in the R.layout.weather_list_row?
try adapter.notifyDataSetChanged();
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
}
);