I have a simple database, with _id column and createdOn column.
Trying to print a listView, but my code doesn't show nothing (neither I've errors, also, in LOG I have correctly my values).
Thank you for your help.
EventActivity.java
[...]
private void getEvent(){
db = new DBManager(this);
Cursor cursor = db.query();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.list_activity,
cursor,
new String[] { "createdOn" },
new int[] { R.id.singleDate });
ListView listView = (ListView) findViewById(R.id.listDate1);
listView.setAdapter(adapter);
while (cursor.moveToNext()) {
Log.d(LOGTAG, cursor.getLong(0) + " " + cursor.getString(1));
// here I have all my entries in log
}
list_activity.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">
<ListView
android:id="#+id/listDate1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
</ListView>
</RelativeLayout>
list_row.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="5dip" >
<TextView
android:id="#+id/singleDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</RelativeLayout>
your error is in creating the adapter
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.list_row, // that's the layout to inflate for each row
cursor,
new String[] { "createdOn" }, // those are the cursor columns
new int[] { R.id.singleDate }); // those are the layout views
// to match to the cursor columns
Related
I am currently making an app in android on which I have randomly generated a password in the database when registration is done. I wanted to know how to give the notification for the password right after the registration done?
https://developer.android.com/guide/topics/ui/notifiers/notifications.html
You can learn here to display a local notification from your application.
create listview in xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ListActivity" >
<ListView
android:id="#+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
In onCreate() of your new activity
String[] passwordArray = (String[]) getValues().toArray(new String[alDcCode.size()]);
ArrayAdapter adapter = new ArrayAdapter<String>(this,
R.layout.activity_listview, passwordArray);
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
Paste this method
public ArrayList<String> getValues() {
ArrayList<String> values = new ArrayList<String>();
LocalDatabase DpDataBase = new LocalDatabase(getApplicationContext());
SQLiteDatabase db =DpDataBase.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT passwordColumName FROM " + tableName, null);
if(cursor.moveToFirst()) {
do {
values.add(cursor.getString(cursor.getColumnIndex("passwordColumName")));
}while(cursor.moveToNext());
}
cursor.close();
db.close();
}
and your activity_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
I have a ListView populated with a SimpleCursorAdapter. The listview shows the content of one column of my database. That column is just lessons I created in my db (like English,Maths). I have also themes for each lesson ( like Reading Writting..). And that is also a column of the same table. In my listView it only shows "English", but I'd like to show "English - Reading", so I can make the difference.
How can I do that ?
btw, my column for the lesson is 'branche_cours' and the other column I want to show is 'designation'.
Here's my SimpleCursorAdapter
lvCours =(ListView)findViewById(R.id.ListCours);
final Cursor cursor = dbhelper.getAllCours();
String[] from = { "branche_cours" }; int[] to = { android.R.id.text1 };
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, from, to, 0);
lvCours.setAdapter(adapter);
adapter.notifyDataSetChanged();
1. Create a layout list_item.xml for your row item.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="#+id/text_branche_cours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:text="English"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:text=" - " />
<TextView
android:id="#+id/text_designation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:text="Reading" />
</LinearLayout>
2. To show branche_cours to TextView R.id.text_branche_cours and designation to TextView R.id.text_designation, do this in your Activity:
lvCours = (ListView)findViewById(R.id.ListCours);
final Cursor cursor = dbhelper.getAllCours();
String[] from = { "branche_cours", "designation" };
int[] to = { R.id.text_branche_cours, R.id.text_designation };
adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, from, to, 0);
lvCours.setAdapter(adapter);
adapter.notifyDataSetChanged();
Here is an good Tutorial.
Hope this will help~
I have a SQLite Database that I want to use to populate a ListView, but I can't for the life of me figure out how to do that. Based on various internet sources (other questions/tutorials/etc.), I've been able to get this code, but it's clearly not working, as I'm now not even able to open the app in the emulator:
populateListView method
public void populateListView() {
Cursor cursor = db.getAllContacts();
String[] fromFieldNames = new String[] {DBAdapter.KEY_ROWID, DBAdapter.KEY_LOCATION, DBAdapter.KEY_TIME};
int[] toViewIds = new int[] {R.id.textView3, R.id.textView2, R.id.textView4};
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this,
R.layout.list_view_layout, cursor, fromFieldNames, toViewIds, 0);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(myCursorAdapter);
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="vertical"
android:id="#+id/TableLayout">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set New Alarm"
android:id="#+id/setAlarmButton"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="onClickSetAlarm" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView" />
</RelativeLayout>
list_view_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView4"
android:layout_gravity="center_horizontal" />
</LinearLayout>
I'm fairly certain that the issue isn't with the code for the DBAdapter, as it was working fine before I added this bit of code (I had it display a Toast with the data).
Thanks!
Use a SimpleCursorAdapter
Cursor cursor = obj.fetchAllNames();
registerForContextMenu(lv);
String[] columns = new String[] {
obj.KEY_MAIL,
obj.KEY_NAME
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.tvname,
R.id.tvemail
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
try{
dataAdapter = new SimpleCursorAdapter(
this, R.layout.viewusertext,cursor,columns,
to,
0);
}catch(Exception e)
{e.printStackTrace();}
lv.setAdapter(dataAdapter);
where lv is the listview and dataAdapter is private SimpleCursorAdapter dataAdapter;
The query to retrieve desired element you want to show in the list:
public Cursor fetchAllNames() {
Cursor mCursor = app.myDbHelper.MyDB().query("reg", new String[] {"email as _id","fname"},
null, null, null, null, null);
try{
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}catch(Exception e)
{
return mCursor;
}
}
"reg" is the table name.
Note:while using simplecursoradapter you should use the primary key as _id.In my case email is primary key and I have defined it as public static final String KEY_MAIL="_id";
This question I answered just yesterday. The easiest way to create CursorLoader, which will load your data in background from DB only once when activity creates and easy to set adapter: just form columns "from" (DB.COLUMN_NAME) and "to" (R.id.textviewToDisplay).
Check please this answer.
retriveing data from exisint sqlite database
I am trying to get my class to show all the data in a database. at the current time it i only showing the one set of data which would be the title and i tried to alter he code to what i thought would show all the data
public class WorkoutProgress extends ListActivity {
private DataBaseHelper datasource;
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.progress);
datasource = new DataBaseHelper(this);
datasource.open();
fillData();
datasource.close();
}
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = datasource.getAllTitles();
startManagingCursor(c);
String[] from = new String[] { DataBaseHelper.KEY_TITLE,DataBaseHelper.KEY_ISBN, DataBaseHelper.KEY_PUBLISHER };
int[] to = new int[] { R.id.text1 };
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
setListAdapter(notes);
}
}
The code I thought I needed to change was this line
String[] from = new String[] { DataBaseHelper.KEY_TITLE};
To this line
String[] from = new String[] { DataBaseHelper.KEY_TITLE,DataBaseHelper.KEY_ISBN, DataBaseHelper.KEY_PUBLISHER };
this is the notes_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="#+id/text1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/text2"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/text3"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
You change the from array but you must also add some ids in the to array(ids of other TextViews in the list row layout(R.layout.notes_row) where you would show the other columns.)
String[] from = new String[] { DataBaseHelper.KEY_TITLE,DataBaseHelper.KEY_ISBN, DataBaseHelper.KEY_PUBLISHER };
int[] to = { R.id.text1, R.id.text2, R.id.text3 };
where R.id.text1, R.id.text2 and R.id.text3 are the ids of TextView from the R.layout.notes_row:
<?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="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I want to display data, from a database, in a gridview.
Means I have a data in my table (let it's a customer detail) and I want to show it in gridview (or any other control to display details) same as we done in asp.net.
Solution:--
public void FillGrid() {
DatabaseHelper dbh = new DatabaseHelper(this);
dbh.openDataBase();
Cursor cursor;
GridView grv = (GridView) findViewById(R.id.grvData);
cursor = dbh.getGridData();
dbh.close();
if (cursor != null) {
startManagingCursor(cursor);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.gridlayout, cursor,
new String[] { GridTestActivity.KEY_ROW_ID, GridTestActivity.KEY_ID, GridTestActivity.KEY_DESCRIPTION }
,new int[] { R.id.txtGrv_id, R.id.txtGrvid, R.id.txtGrvDescription } );
adapter.setViewResource(R.layout.gridlayout);
grv.setAdapter(adapter);
}
}
Here's the full example.
Also if your beginning Android this would be a good book for you.
Check this post for gridview: http://developer.android.com/resources/tutorials/views/hello-gridview.html
and this for cursor adapter: http://developer.android.com/reference/android/widget/CursorAdapter.html
Hope this helps!
You can Set the GridView With 3 Columns. It will give you a look of Table form.
OR
You can use customized GridView for your application Using the Following type of Codes..
customergrid.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<GridView
android:id="#+id/grid"
android:layout_width="fill_parent"
android:layout_height="357dp"
android:numColumns="1"
android:stretchMode="columnWidth" />
<Button
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cancel" />
</LinearLayout>
And, for each Row use a customized Xml file..
customerrow.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="fill_parent" >
<TableRow>
<TextView
android:layout_width="50px"
android:layout_height="wrap_content"
android:id="#+id/row_ID"
android:padding="5px"
android:layout_weight="1" />
<TextView
android:layout_width="50px"
android:layout_height="wrap_content"
android:id="#+id/key_ID"
android:padding="5px"
android:layout_weight="1" />
<TextView
android:layout_width="50px"
android:layout_height="wrap_content"
android:id="#+id/key_Description"
android:padding="5px"
android:layout_weight="1" />
</TableRow>
</TableLayout>
Use customergrid.xml on the Oncreate() method and use customerrow.xml in the Grid creation like as your code..
public void FillGrid() {
DatabaseHelper dbh = new DatabaseHelper(this);
dbh.openDataBase();
Cursor cursor;
GridView grv = (GridView) findViewById(R.id.grvData);
cursor = dbh.getGridData();
dbh.close();
if (cursor != null) {
startManagingCursor(cursor);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.customerrow, cursor, new String[] { GridTestActivity.KEY_ROW_ID,
GridTestActivity.KEY_ID, GridTestActivity.KEY_DESCRIPTION }, new int[] {
R.id.txtGrv_id, R.id.txtGrvid, R.id.txtGrvDescription } );
adapter.setViewResource(R.layout.gridlayout);
grv.setAdapter(adapter);
}
}
You can get the data from the Database on the gridview now.