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);
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 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 don't see any errors and the debug is not very useful. How can I fix that error?
This is part of the code and the error appears inside the last line
Thanks in advance!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selfie_list);
mMatrixCursor = new MatrixCursor(new String[]{"_id","_data","_details"});
ListView lVThumbnail = (ListView) findViewById(R.id.selfie_list_layout);
mAdapter = new SimpleCursorAdapter(
getBaseContext(),
R.layout.activity_selfie_list,
null,
new String[] { "_data","_details"} ,
new int[] { R.id.img_thumbnail,R.id.tv_details},
0
);
lVThumbnail.setAdapter(mAdapter);
And this are the layouts used, the first one is the list fragment (since I use a list and detailed fragments), and the second is the list layout. :
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/selfie_list"
android:name="com.lordpato.dailyselfie.SelfieListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
tools:context="com.lordpato.dailyselfie.SelfieListActivity"
tools:layout="#layout/lv_layout" />
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="#+id/selfie_list_layout"
android:layout_height="75dp" >
<ImageView
android:id="#+id/img_thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:adjustViewBounds="true"
android:padding="5dp"
/>
<TextView
android:id="#+id/tv_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/img_thumbnail"
android:padding="5dp"
/>
</RelativeLayout>
selfie_list_layout is not a ListView.
ListView lVThumbnail = (ListView) findViewById(R.id.selfie_list_layout);
...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="#+id/selfie_list_layout"
android:layout_height="75dp" >
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.
I'm stumped as to how to properly implement an adapter that can pull a column of data from my sqlite database and add it to a listview (based on the android-provided multiple checkbox). All of the existing examples use the following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] myList = new String[] {"Hello","World","Foo","Bar"};
ListView lv = new ListView(this);
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myList));
setContentView(lv);
}
However, I don't want a string. Here's my code:
public class ListGroups extends Activity {
/** Called when the activity is first created. */
private ExpensesDbAdapter mDBHelper;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
ListView lv = (ListView)findViewById(R.id.list1);
mDBHelper = new ExpensesDbAdapter(ListGroups.this);
mDBHelper.open();
Cursor c = mDBHelper.fetchAllGroups();
startManagingCursor(c);
String[] from = new String[] {ExpensesDbAdapter.KEY_GROUPNAME};
int[] to = new int[] {R.id.groupname};
ListAdapter ladapter = new SimpleCursorAdapter(ListGroups.this, R.layout.grouprow, c, from, to);
lv.setAdapter(ladapter);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}
Here's the view.xml file:
<?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:gravity="center">
<TextView android:id="#+id/header01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Select Groups"/>
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/footerbutton01"
android:text="Get Expense Reports" />
</LinearLayout>
And, because the SimpleCursorAdapter asks for it, here's the grouprow.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/groupname"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"/>
If I use a ListActivity and don't invoke the layout on Creation, I can get it to work, but I need the wrappers around that ListView, and I can't make the .addHeader and .addFooter methods work for me. Any help on creating the proper adapter would be appeciated.
Answered it with some help. Several things were wrong, mostly in the list.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:gravity="center">
<TextView android:id="#+id/header01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Select Groups"/>
<ListView
android:id="#android:id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_around"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/footerbutton01"
android:text="Get Expense Reports" />
</LinearLayout>
This allows you to tie the ListView to that element in the xml. You don't have a name for it, but you can use getListView() in the onCreate method to access it.