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.
Related
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);
}
I have a employee table in the database and have an activity where user inserts the employee details into the table
Now I have to also update the particular employee , so , I used the same xml with different buttons setting the visibility as gone and visible for update and insert buttons respectively
Now when the user clicks on update, I want to display the same xml but with the Edittexts filled with the employee details already and then can be editable
how can I do that ??
this is my 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="match_parent"
android:background="#drawable/background"
android:gravity="top" >
<TextView
android:id="#+id/TV"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="20sp"
android:gravity="center_horizontal"
android:text="Employee Entry details"
android:textColor="#000000"
android:textSize="35sp"
android:textStyle="italic" />
<LinearLayout
android:id="#+id/mainll"
android:layout_width="fill_parent"
android:layout_height="650sp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/TV"
android:layout_marginTop="10dp"
android:orientation="vertical" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50sp">
<TextView
android:id="#+id/enametext"
android:layout_width="260sp"
android:layout_height="wrap_content"
android:text="Employee Name:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="30sp" />
<EditText
android:id="#+id/enameedit"
android:layout_width="340sp"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp">
<TextView
android:id="#+id/edesignationtext"
android:layout_width="260sp"
android:layout_height="wrap_content"
android:text="Employee Designation:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="30sp"/>
<EditText
android:id="#+id/edesignationedit"
android:layout_width="340sp"
android:layout_height="wrap_content" >
</EditText>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp">
<TextView
android:id="#+id/enumbertext"
android:layout_width="260sp"
android:layout_height="wrap_content"
android:text="Employee Phone Number:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="30sp"/>
<EditText
android:id="#+id/enumberedit"
android:layout_width="340sp"
android:layout_height="wrap_content"
>
</EditText>
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp">
<TextView
android:id="#+id/edobtext"
android:layout_width="260sp"
android:layout_height="wrap_content"
android:text="Employee D.O.B :"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="30sp"/>
<EditText
android:id="#+id/edobedit"
android:layout_width="250sp"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/dateOfBirth"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date"
android:textColor="#000000"
android:textSize="30sp"
android:background="#ffffff" />
</TableRow>
<TableRow
android:id="#+id/tableRow5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp">
<TextView
android:id="#+id/ebasicpaytext"
android:layout_width="260sp"
android:layout_height="wrap_content"
android:text="Employee Basic Pay :"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="30sp"/>
<EditText
android:id="#+id/ebasicpayedit"
android:layout_width="340sp"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow
android:id="#+id/tableRow6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp">
<TextView
android:id="#+id/epftext"
android:layout_width="260sp"
android:layout_height="wrap_content"
android:text="Employee PF deduction :"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="30sp"/>
<EditText
android:id="#+id/epfedit"
android:layout_width="340sp"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow
android:id="#+id/tableRow6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp">
<TextView
android:id="#+id/eotherdeductionstext"
android:layout_width="260sp"
android:layout_height="wrap_content"
android:text="Employee Other Deductions :"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="30sp"/>
<EditText
android:id="#+id/eotherdeductionedit"
android:layout_width="340sp"
android:layout_height="wrap_content"
/>
</TableRow>
</LinearLayout>
<TableRow
android:id="#+id/inserttablerow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/mainll"
android:layout_marginTop="61dp"
android:visibility="visible" >
<Button
android:id="#+id/savebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70sp"
android:background="#ffffff"
android:text="Save"
android:textColor="#000000"
android:textSize="30sp" />
<Button
android:id="#+id/cancelbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100sp"
android:background="#ffffff"
android:text="Cancel"
android:textColor="#000000"
android:textSize="30sp" />
<Button
android:id="#+id/addanotheremployeebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:layout_marginLeft="100sp"
android:text="Add new"
android:textColor="#000000"
android:textSize="30sp" />
</TableRow>
<TableRow
android:id="#+id/updatetablerow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/mainll"
android:layout_marginTop="61dp"
android:visibility="gone" >
<Button
android:id="#+id/updatesavebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="160sp"
android:background="#ffffff"
android:text="Update"
android:textColor="#000000"
android:textSize="30sp" />
<Button
android:id="#+id/updatecancelbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100sp"
android:background="#ffffff"
android:text="Cancel"
android:textColor="#000000"
android:textSize="30sp" />
</TableRow>
and my insert and update methods from datahelper class
//inserting into employeetable
public void insertDataIntoEmployee(String name_STR, String designation_STR, String phonenumber_STR, String age_STR,
String basicpay_STR, String pfdeduction_STR, String otherdeductions_STR) {
System.out.println(" insertData start ");
cv = new ContentValues();
cv.put("ename", name_STR);
cv.put("edesignation", designation_STR);
cv.put("ephonenumber", phonenumber_STR);
cv.put("eage", age_STR);
cv.put("ebasic", basicpay_STR);
cv.put("epf", pfdeduction_STR);
cv.put("eotherdeductions", otherdeductions_STR);
this.db.insert(EMPLOYEE_TABLE_NAME, null, cv);
System.out.println(" insertData end ");
}
// updating the table
public void updateDataIntoEmployee(long rowId ,String name_STR, String designation_STR, String phonenumber_STR, String age_STR,
String basicpay_STR, String pfdeduction_STR, String otherdeductions_STR) {
System.out.println(" updateData start ");
cv = new ContentValues();
cv.put("ename", name_STR);
cv.put("edesignation", designation_STR);
cv.put("ephonenumber", phonenumber_STR);
cv.put("eage", age_STR);
cv.put("ebasic", basicpay_STR);
cv.put("epf", pfdeduction_STR);
cv.put("eotherdeductions", otherdeductions_STR);
this.db.update(EMPLOYEE_TABLE_NAME, cv, EMPLOYEE_ID + "=" + rowId, null);
System.out.println(" updateData end ");
}
and my method in class where i have to display the populated edittexts is here
public void onCreate(Bundle savedState) {
EditText editText = (EditText) findViewById(R.id.my_edittext);
String value = // Query your 'last value'.
editText.setText(value);
}
Now what can be the query which has to be passed in string value which can be retrieved and also , am I supposed to rewrite this code every time for each edit texts?
Is there anyway to avoid the redundancy?
Thank you
You can try something like
String[] mProjection = {/* All The Columns which you want to retrieve */};
Cursor cursor = this.db.query(EMPLOYEE_TABLE_NAME, mProjection, EMPLOYEE_NAME + "=?", new String[] { name_STR }, null, null, null);
This will return all the columns names in one shot and you can use whatever you wish. No need to query the db again and again.
After getting the cursor,
if (cursor != null && cursor.moveToNext()) {
// you can store all the returned columns in a custom class here and return that class object
}
So I've been trying my best and getting this Spinner to be able to change the list depending on the choices done. For example changing the say a Spinner devoted to floors, would yield a different set of spots when pressing the spot spinner:
XML format:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="202dp"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_weight="0.49" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="New Reservation"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/Fname"
android:layout_width="129dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Name"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/Lname"
android:layout_width="126dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="License"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/License"
android:layout_width="126dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<Spinner
android:id="#+id/Garage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/Garages"
android:prompt="#string/GaragePrompt" />
<Spinner
android:id="#+id/Floor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/Floors"
android:prompt="#string/FloorPrompt" />
<Spinner
android:id="#+id/Spot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/Spots"
android:prompt="#string/SpotPrompt" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/Date"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:inputType="date" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/Time"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:inputType="time" />
<Button
android:id="#+id/Reserve"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="21dp"
android:text="Reserve" />
</LinearLayout>
</ScrollView>
Thank you for the help.
The following is one way of populating different values on a Spinner depending on the selection of another Spinner.
In the following case the Spots will change values with the selection of the Floor spinner. This is one way of doing it but might not be the best way...
Declare as class variables...
private Spinner floor, spot;
private ArrayAdapter<String> floorAdapter, spotAdapter;
In onCreate
floor = (Spinner) findViewById(R.id.Floor);
spot = (Spinner) findViewById(R.id.Spot);
// NOTE:
// You can populate the lists below from a resource array as well using ArrayAdapter.createFromResource.
List<String> floor = new ArrayList<String>();
floor.add("spot 1");
floor.add("spot 2");
List<String> spot1 = new ArrayList<String>();
spot1.add("spot1 item 1");
spot1.add("spot1 item 2");
spot1.add("spot1 item 3");
List<String> spot2 = new ArrayList<String>();
spot2.add("spot2 item 1");
spot2.add("spot2 item 2");
spot2.add("spot2 item 3");
floorAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, floor);
floor.setAdapter(floorAdapter);
floor.setOnItemSelectedListener(this);
Then do the following on the onItemSelected for the floor spinner...
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(position) {
case 0:
spotAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spot1);
spot.setAdapter(spotAdapter);
break;
case 1:
spotAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spot2);
spot.setAdapter(spotAdapter);
break;
default:
break;
}
As I mentioned above, you can load the list from an array as well using ArrayAdapter.createFromResource.
All the best.
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
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();