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>
Related
I want to use SimpleAdapter in DriverActivity.
I create activity_list_layout.xml in folder layout like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/ColType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Type"/>
<TextView
android:id="#+id/ColDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Date"/>
</LinearLayout>
And create activity_driver.xml like this.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/lv_driver"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="#+id/emptyElement"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:text="Please add driver"
android:gravity="center"
android:textColor="#525252"
android:textSize="19.0sp"
android:visibility="gone" />
</android.support.constraint.ConstraintLayout>
In DriverActivity.java I use custom List view like this.
public void onResume() {
super.onResume();
db = dbHelper.getWritableDatabase();
String[] queryColumns = new String[]{"_id", DBHelper.COL_TYPE, DBHelper.COL_OPTION_NAME,DBHelper.COL_DATE };
cursor = db.query(DBHelper.TABLE_NAME, queryColumns, null,null,
null,null,null);
HashMap<String,String> map = new HashMap<String,String>();
String type_name;
while(cursor.moveToNext())
{
type_name=cursor.getColumnName(1);
map.put("v_type", dbHelper.V_Type(type_name));
map.put("date", type_name=cursor.getString(4));
lst_driver.add(map);
}
String[] showColumns = new String[]{DBHelper.COL_TYPE, DBHelper.COL_DATE};
int[] views = new int[] {android.R.id.col, android.R.id.text2};
adapter = new SimpleAdapter(DriverLicenseActivity.this,lst_driver, android.R.layout.activity_list_layout, showColumns, views);
lv_driver.setAdapter(adapter);
}
At new SimpleAdapter android.R.layout.activity_list_layout show error.
canot resole symbol android.R.layout.activity_list_layout
How to fix problem ?
Just remove the android on your android.R.layout.activity_list_layout. It should be like this: R.layout.activity_list_layout.
android.R. is used for access predefined classes by android SDK (layouts/drawables)
R isused for access custom classes (that means its layouts/drawables imported/created by user)
that case you should use
SimpleAdapter adapter = new SimpleAdapter(DriverLicenseActivity.this,lst_driver, R.layout.activity_list_layout, showColumns, views);
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
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
In my android app i want to display the list which is retrieving the values from DB .I m using simple cursor adapter to do so,but list view is empty.Here is my code.my list view have two rows. How can i use simple cursor adapter in app.what i m doing wrong.Please suggest.
Thanks in advance.
//list.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:layout_width="fill_parent" android:id="#android:id/android:list"
android:layout_height="fill_parent" android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"></ListView>
</RelativeLayout>
// textview.xml-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weightSum="1">
<TextView android:id="#+id/column1" android:background="#drawable/mid"
android:layout_height="30dp"
android:layout_width="fill_parent" android:layout_weight="0.03"></TextView>
<TextView android:layout_height="wrap_content"
android:text="TextView" android:id="#+id/column2" android:background="#drawable/mid"
android:layout_weight="0.03" android:layout_width="fill_parent"></TextView>
</LinearLayout>
public class AddFood extends ListActivity{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.listxml);
DataBaseHelper db = null;
try {
db = new DataBaseHelper(this);
} catch (IOException e) {
e.printStackTrace();
}
SQLiteDatabase database= db.getReadableDatabase();
Cursor c=database.query("food_list", new String[]{"_id","food_items"}, null, null, null,null,null);
startManagingCursor(c);
String[] displayfield=new String[]{"_id","food_items"};
int[] displayViews = new int[] {R.id.column1,R.id.column2};
SimpleCursorAdapter mAdapter=new SimpleCursorAdapter(getApplicationContext(),R.layout.textview, c,displayfield , displayViews);
this.setListAdapter(mAdapter);
c.close();
}
}
For list.xml use:
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></ListView>
In class file:
do not use c.close();
and use:
SimpleCursorAdapter mAdapter=new SimpleCursorAdapter(this,R.layout.textview, c,displayfield , displayViews);
setListAdapter(mAdapter);
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.