The SimpleCursorAdapter is not populating my ListView, no error, just nothing happens.
Could someone help me find the error?
1) Layout with ListView component (tab_frag1_layout.xml):
<?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:background="#FF0000"
android:orientation="vertical" >
<ListView
android:id="#+id/lvVehicle"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
2) Layout to represent row (vehicle_row.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"
android:padding="10dip" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1dip" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_arrow_right"
android:contentDescription="#null"
android:adjustViewBounds="true"
android:layout_marginRight="3dip"
android:layout_gravity="center_vertical" />
<TextView
android:id="#+id/plate"
style="#style/vehicleDefaultFont.plate"
android:text="#string/label_text_view" />
<TextView
android:id="#+id/hyphen"
android:text="#string/label_hyphen" />
<TextView
android:id="#+id/model"
android:text="#string/label_text_view" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1dip" >
<TextView
android:id="#+id/driver"
style="#style/vehicleDefaultFont.driver"
android:layout_span="4"
android:text="#string/label_text_view" />
</TableRow>
</TableLayout>
3) Fragment Class:
public class Tab1Fragment extends Fragment {
String TAG = getClass().getName();
private VehicleDbAdapter dbHelper;
private SimpleCursorAdapter dataAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_frag1_layout, container, false);
dbHelper = new VehicleDbAdapter(getActivity());
dbHelper.open();
dbHelper.deleteAllVehicles();
dbHelper.insertVehicles();
Cursor cursor = dbHelper.fetchAllVehicles();
if(cursor != null && cursor.getCount() > 0) {
String[] columns = new String[] {
VehicleDbAdapter.KEY_MODEL,
VehicleDbAdapter.KEY_PLATE,
VehicleDbAdapter.KEY_DRIVER
};
int[] to = new int[] {
R.id.model,
R.id.plate,
R.id.driver,
};
dataAdapter = new SimpleCursorAdapter(
view.getContext(),
R.layout.vehicle_row,
cursor,
columns,
to,
0);
ListView listView = (ListView) view.findViewById(R.id.lvVehicle);
listView.setAdapter(dataAdapter);
Log.i(TAG, "Cursor = " + cursor.getCount());
} else {
Log.i(TAG, "Cursor = " + cursor.getCount());
}
return view;
}
}
Curiously:
My cursor contains an _id field.
I doublechecked my cursor and it has 7 rows.
07-21 01:30:22.215: I/ Cursor = 7
I tried using the layout to the generic android list layout, but nothing doing :(
dataAdapter = new SimpleCursorAdapter(
view.getContext(),
android.R.layout.simple_list_item_1,
cursor,
columns,
to,
0);
Any help is welcome.
The TextViews that you have defined in vehical_row.xml: #+id/plate, #+id/hyphen, #+id/model and #+id/driver are all missing the layout_width and the layout_height attributes. These are required.
For now, set these attributes as "wrap_content". For example:
<TextView
android:id="#+id/plate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/vehicleDefaultFont.plate"
android:text="#string/label_text_view" />
Do the above for the other three as well. This should work for you.
I think you have problem at XMl. Check id of your listview.
android:id="#+android:id/mainListView">
Related
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 create a statistics activity with listView . but onItemClick Not working I surffed internet but given solution not working for me.
try
{
int counter=0;
db = helper.getReadableDatabase();
c = db.query(DBHelper.Result, null, null, null, null, null, null);
c.moveToFirst();
do
{
counter++;
States state = new States(c.getString(2), c.getString(3), false);
stateList.add(state);
}while(c.moveToNext());
Log.d("counter", ""+counter);
/*adapter = new SimpleCursorAdapter(this, R.layout.row, c, new String [] {DBHelper.R_test,DBHelper.R_testNM}, new int []{R.id.rowTxt1,R.id.rowTxt2});
Lv.setAdapter(adapter);
Lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);*/
db.close();
}
catch(Exception e)
{
e.printStackTrace();
Log.d("Error", ""+e.getMessage());
}
// create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this, R.layout.row, stateList);
//ListView listView = (ListView) findViewById(R.id.LvStatistics);
// Assign adapter to ListView
Lv.setAdapter(dataAdapter);
Lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
Log.d("click", "0");
}
});
row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/Background"
android:orientation="vertical" >
<TextView
android:id="#+id/rowTxt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/ExamFontColor"
/>
<TextView
android:id="#+id/rowTxt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/rowTxt1"
android:layout_marginLeft="20dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/FontBlack"
/>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:text="1" />
</RelativeLayout>
exam_statistics.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/Background"
android:orientation="vertical" >
<TextView
android:id="#+id/statisticsHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#color/HeaderColor"
android:gravity="center"
android:text="#string/ButtonStatistics"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/FontWhite"
android:textSize="30sp" />
<ListView
android:id="#+id/LvStatistics"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/btnDelete"
android:background="#color/Background"
>
</ListView>
<Button
android:id="#+id/btnDelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/statisticsHeader"
android:layout_marginBottom="10dp"
android:background="#drawable/button_green_effect"
android:text="#string/ButtonDelete"
android:textColor="#color/FontWhite"
android:textSize="27sp" />
</RelativeLayout>
I tried a Lot but not Working help me to solve this problem
write this in ur xml inside checkbox tag
android:focusable="false"
try after using this..
If your MyCustomAdapter implements OnItemClickListener so you have to use the following code :
MyCustomAdapter.setOnItemclikListener(dataAdapter);
As there is a checkBox in the row, ListView OnitemClickListener wont work. You can write OnItemClickLister in the row (Adapter) , to get the click event.
If you add args2 in log does it return the click position?
Log.d("click", "Row " + args.toString);
I want to change the font face of the list items in my ListView. I created a custom items for that because I want to let the user see the basic information on the database from that activity. How can I change the fonts of my views?
MainActivity.java
//Method in that fetch the data to list view
private void displayListView() {
final DatabaseHandler db = new DatabaseHandler(this);
Cursor cursor = db.fetchClinicByName("");
// The desired columns to be bound
String[] columns = new String[] { Constants.CLINIC_ID, Constants.CLINIC_NAME, Constants.CLINIC_ADD };
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.textviewId, R.id.tv_ClinicName, R.id.tv_ClinicAdd };
simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.attribute_clinic, cursor, columns, to, 0);
ListView listView = (ListView) findViewById(R.id.lv_clinic);
listView.setAdapter(simpleCursorAdapter);
registerForContextMenu(listView);
attribute_clinic.xml
<?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:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clinic Name:"
android:textSize="20sp"
android:layout_marginRight="40dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_ClinicName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="19sp"
android:text="clinic"/>
</TableRow>
<TableRow
android:id="#+id/tableRow01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clinic Address:"
android:textSize="20sp"
android:layout_marginRight="20dp"
android:textStyle="bold"
android:paddingBottom="5dp"
android:layout_marginBottom="5dp" />
<TextView
android:id="#+id/tv_ClinicAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="19sp"
android:text="clinicAdd"
android:paddingBottom="5dp"
android:layout_marginBottom="5dp" />
</TableRow>
</LinearLayout>
<TextView
android:id="#+id/textviewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="114dp"
android:layout_marginTop="16dp"
android:visibility="gone" />
I have not actually tried this yet, but I believe this should work.
You need to override SimpleCursorAdapter's setViewText(). It would be something like this when your fonts are inside of your assets/fonts folder.
private void displayListView() {
final DatabaseHandler db = new DatabaseHandler(this);
Cursor cursor = db.fetchClinicByName("");
// The desired columns to be bound
String[] columns = new String[] { Constants.CLINIC_ID, Constants.CLINIC_NAME, Constants.CLINIC_ADD };
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.textviewId, R.id.tv_ClinicName, R.id.tv_ClinicAdd };
simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.attribute_clinic,
cursor, columns, to, 0) {
#Override
public void setViewText(TextView v, String text) {
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/yourfont.ttf");
v.setTypeface(face);
v.setText(text);
}
};
ListView listView = (ListView) findViewById(R.id.lv_clinic);
listView.setAdapter(simpleCursorAdapter);
registerForContextMenu(listView);
I am trying to implement onItemClickListener in Fragment class but unfortunately it is not working properly... here is the source code.. please let me know what is the error??
NetworkDetailsFragment.java(My Fragment class)
public class NetworkDetailsFragment extends Fragment implements AdapterView.OnItemClickListener{
private ListView listView;
private View networkDetailsView;
private QOSNetworkDetailsAdapter qosNetworkDetailsAdapter;
private QOSNetworkDetailsDatabaseHelper qosNetworkDetailsDatabaseHelper;
private SimpleCursorAdapter simpleCursorAdapter;
private String LOG_TAG = NetworkDetailsFragment.class.getSimpleName();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(LOG_TAG, "Inside onCreateView() method");
networkDetailsView = inflater.inflate(R.layout.network_details, container, false);
listView = (ListView) networkDetailsView.findViewById(R.id.list_view);
qosNetworkDetailsDatabaseHelper = new QOSNetworkDetailsDatabaseHelper(getActivity());
Cursor cursor = qosNetworkDetailsDatabaseHelper.getRecord();
String[] columns = {QOSNetworkDetailsDatabaseConstants.COLUMN_NETWORK_TYPE,
QOSNetworkDetailsDatabaseConstants.COLUMN_NETWORK_STATUS,
QOSNetworkDetailsDatabaseConstants.COLUMN_LATITUDE,
QOSNetworkDetailsDatabaseConstants.COLUMN_LONGITUDE,
QOSNetworkDetailsDatabaseConstants.COLUMN_TIME};
int[] to = new int[]{
R.id.networkType,
R.id.networkStatus,
R.id.latitudeAndLongitude,
R.id.date
};
/*SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(
this, R.layout.activity_network_info,
cursor,
columns,
to,
0);*/
simpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.activity_network_info, cursor, columns, to, 0);
qosNetworkDetailsAdapter = new QOSNetworkDetailsAdapter(getActivity().getApplicationContext(), qosNetworkDetailsDatabaseHelper.getRecord(), false);
listView.setAdapter(qosNetworkDetailsAdapter);
return networkDetailsView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listView.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
String countryCode =
cursor.getString(cursor.getColumnIndexOrThrow("code"));
Toast.makeText(getActivity().getApplicationContext(),
countryCode, Toast.LENGTH_SHORT).show();
}
}
network_details.xml(i.e list_view.xml)
<?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="match_parent"
android:contentDescription="#string/network_details_fragment_string"
android:descendantFocusability="blocksDescendants" >
<ListView
android:id="#+id/list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
activity_network_info.xml(list items)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/black"
>
<TextView
android:id="#+id/networkStatus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#drawable/white"
android:textSize="15sp" />
<TextView
android:id="#+id/networkType"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#drawable/white"
android:textSize="15sp" />
<TextView
android:id="#+id/latitudeAndLongitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#drawable/lightblue"
android:textSize="15sp" />
<TextView
android:id="#+id/date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#drawable/lightblue"
android:textSize="15sp" />
</LinearLayout>
</ScrollView>
List is getting populated to the screen... but onItemClickListener is not working in this code... please let me know what is the error.. Thanks
Try to extend FragmentActivity instead of Fragment...
If you use a ListFragment, your ListView Id in layout must be android.R.id.list
like that:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="#android:id/list" />
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.