I am trying to fetch values from the database when I click on the view button which is another activity for navigation when I click on the view all button the CustodianViewActivity is triggered to display all the values from the database on a list view but in my case the application crashes every time I try to view the data from the database. Am not sure where I am going wrong.
Database Class
import android.content.ContentValues;
import android.content.Context;import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBHandler extends SQLiteOpenHelper {
private static DBHandler instance;
// Database Name
public static final String DATABASE_NAME ="AssetDB.db";
//Database version
public static final int Databasevr = 1;
//Custodian Table Name
public static final String TABLE_CUSTODIAN = " Custodian";
// Columbs in the Custodian Table
public static final String CUSTODIAN_ID = "_CustID";
public static final String CUSTODIAN_NAME = "CustName";
public static final String CUSTODIAN_DESIGNATION = "CustDesign";
public static final String CUSTODIAN_DEPARTMENT = "CustDepart";private static final String CREATE_TABLE_CUSTODIAN = "CREATE TABLE" + TABLE_CUSTODIAN + "("
+ CUSTODIAN_ID + " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
+ CUSTODIAN_NAME + " TEXT NOT NULL,"
+ CUSTODIAN_DESIGNATION + " TEXT NOT NULL,"
+ CUSTODIAN_DEPARTMENT + " TEXT NOT NULL" + ");";
// constructor passing parameter passing Database name and Database version
public DBHandler(Context ct)
{
super(ct, DATABASE_NAME, null, Databasevr);
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
// Creating the tables
db.execSQL(CREATE_TABLE_CUSTODIAN);
db.execSQL(CREATE_TABLE_ASSET);
Log.d("Tables","Tables have been created");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
// dropping the tables
db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_CUSTODIAN );
db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_ASSET);
// recreate the tables
onCreate(db);
}
public Cursor getAllCustodians(){
try {
SQLiteDatabase db_database = getWritableDatabase();
Cursor cursor = db_database.rawQuery("SELECT * FROM" + TABLE_CUSTODIAN, null);
if (cursor != null) {
return cursor;
} else {
return null;
}
}
catch(Exception e)
{
return null;
}
}}
CustodianViewActivity
package com.example.nfcams;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class CustodianViewActivity extends Activity {
ListView CustodianListview;
DBHandler db_database;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custodian_view);
db_database = new DBHandler(getApplicationContext());
CustodianListview = (ListView) findViewById(R.id.custodianlistView);
new Handler().post(new Runnable() {
#Override
public void run() {
populateCustoListView();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_custodian_view, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void populateCustoListView()
{
Cursor c = db_database.getAllCustodians();
CustodianListview.setAdapter(new CustodiansListAdapter(this,c));
}
private class CustodiansListAdapter extends CursorAdapter
{
private Cursor cursor;
public CustodiansListAdapter(Context context, Cursor cur) {
super(context, cur);
cursor = cur;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView custID = (TextView) view.findViewById(R.id.Custodianid_view);
int CustodID = cursor.getInt(cursor.getColumnIndex("_CustID"));
custID.setText(String.valueOf(CustodID));
TextView name = (TextView) view.findViewById(R.id.Custodianname_view);
String Custname = cursor.getString(cursor.getColumnIndex("CustName"));
name.setText(Custname);
TextView Designation = (TextView) view.findViewById(R.id.CustodianDesignation_view);
String CustDesignation = cursor.getString(cursor.getColumnIndex("CustDesign"));
Designation.setText(CustDesignation);
TextView Department = (TextView) view.findViewById(R.id.CustodianDepartment_view);
String CustDepartment = cursor.getString(cursor.getColumnIndex("CustDepart"));
Department.setText(CustDepartment);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View retView = inflater.inflate(R.layout.custodianrow_views, parent, false);
bindView(retView,context,cursor);
return retView;
}
}
}
Custodianview Activity layout
<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="com.example.nfcams.CustodianViewActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/custodianlistView"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
List view layout
<?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:background="#color/list_item_bg"
android:descendantFocusability="blocksDescendants" >
<RelativeLayout
android:id="#+id/layout_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/Custodianname_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp" />
<TextView
android:id="#+id/Custodianid_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/Custodianname_view"
android:padding="6dp" />
<TextView
android:id="#+id/CustodianDesignation_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Custodianid_view"
android:padding="6dp" />
<TextView
android:id="#+id/CustodianDepartment_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/CustodianDesignation_view"
android:padding="6dp" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/layout_item"
android:background="#color/view_divider_color" />
</RelativeLayout>
Error Log
05-30 09:42:16.471 1334-1334/com.example.nfcams E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.nfcams, PID: 1334
java.lang.IllegalArgumentException: column '_id' does not exist
at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
at android.widget.CursorAdapter.init(CursorAdapter.java:172)
at android.widget.CursorAdapter.<init>(CursorAdapter.java:120)
at com.example.nfcams.CustodianViewActivity$CustodiansListAdapter.<init>(CustodianViewActivity.java:95)
at com.example.nfcams.CustodianViewActivity.populateCustoListView(CustodianViewActivity.java:84)
at com.example.nfcams.CustodianViewActivity.access$000(CustodianViewActivity.java:17)
at com.example.nfcams.CustodianViewActivity$1.run(CustodianViewActivity.java:42)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Related
I Thought that I found a solution with the simple delete method that refers back to MySQLiteHelper, but I don't know what the list in db.deleteItem(list.get(0)); refers to.
My location file
package com.buysse.roan.findyourstuff;
import android.content.ClipData;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class Locatie extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, OnClickListener {
private Button buttonSave;
private Button buttonDelete;
private EditText editTextName;
private EditText editTextLocation;
MySQLiteHelper db = new MySQLiteHelper(this);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_locatie);
//add all items to list
filList();
/**
* CRUD Operations
*
*/
//buttonpress
buttonSave = (Button) findViewById(R.id.buttonAddUser);
buttonSave.setOnClickListener(this);
editTextName = (EditText) findViewById(R.id.editTextName);
editTextLocation = (EditText) findViewById(R.id.editTextLocation);
/* // add item
// get all items
List<Item> list = db.getAllItems();
// delete one book
db.deleteItem(list.get(0));
// get all items
db.getAllItems();
///new activity remove*/
//toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.locatie, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
//change links
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_Locatie) {
Intent Locatie = new Intent(this, Locatie.class);
startActivity(Locatie);
} else if (id == R.id.nav_objects) {
Intent Object = new Intent(this, Object.class);
startActivity(Object);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
protected void onStop() {
super.onStop();
}
#Override
protected void onResume() {
super.onResume();
}
// onclick add to array + refresh list
#Override
public void onClick(View view) {
if (view.getId() == R.id.buttonAddUser) {
db.addItem(new Item(editTextName.getText().toString(), editTextLocation.getText().toString()));
/////Make Method
filList();
}
}
public void filList(){
MyAdapter adapter = new MyAdapter(this, db.getAllItems());
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}
//list delete
// public void deleteItem (){
// db.deleteItem(list.get(0));
// }
}
Adapter
package com.buysse.roan.findyourstuff;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;
/**
* Created by roanb on 10/08/2016.
*/
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class MyAdapter extends ArrayAdapter<Item> {
private final Context context;
private final ArrayList<Item> itemsArrayList;
public MyAdapter(Context context, ArrayList<Item> itemsArrayList) {
super(context, R.layout.row, itemsArrayList);
this.context = context;
this.itemsArrayList = itemsArrayList;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
View rowView = inflater.inflate(R.layout.row, parent, false);
// 3. Get the two text view from the rowView
TextView labelView = (TextView) rowView.findViewById(R.id.Name);
TextView valueView = (TextView) rowView.findViewById(R.id.Location);
// 4. Set the text for textView
labelView.setText(itemsArrayList.get(position).getName());
valueView.setText(itemsArrayList.get(position).getLocation());
// 5. retrn rowView
return rowView;
}
}
MySQLiteHelper
package com.buysse.roan.findyourstuff;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* Created by roanb on 10/08/2016.
*/
public class MySQLiteHelper extends SQLiteOpenHelper { // Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "ItemDB";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
// SQL statement to create book table
String CREATE_ITEM_TABLE = "CREATE TABLE items ( " +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name TEXT, "+
"location TEXT )";
// create items table
db.execSQL(CREATE_ITEM_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older items if existed
db.execSQL("DROP TABLE IF EXISTS items");
// create fresh iems table
this.onCreate(db);
}
//sql statements
// items table name
private static final String TABLE_ITEMS = "items";
// items Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_LOCATION = "location";
private static final String[] COLUMNS = {KEY_ID,KEY_NAME,KEY_LOCATION};
public void addItem (Item item){
//for logging
Log.d("addBook", item.toString());
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put(KEY_NAME, item.getName()); // get name
values.put(KEY_LOCATION, item.getLocation()); // get author
// 3. insert
db.insert(TABLE_ITEMS, // table
null, //nullColumnHack
values); // key/value -> keys = column names/ values = column values
// 4. close
db.close();
}
public Item getItem(int id){
// 1. get reference to readable DB
SQLiteDatabase db = this.getReadableDatabase();
// 2. build query
Cursor cursor =
db.query(TABLE_ITEMS, // a. table
COLUMNS, // b. column names
" id = ?", // c. selections
new String[] { String.valueOf(id) }, // d. selections args
null, // e. group by
null, // f. having
null, // g. order by
null); // h. limit
// 3. if we got results get the first one
if (cursor != null)
cursor.moveToFirst();
// 4. build item object
Item item = new Item();
item.setId(Integer.parseInt(cursor.getString(0)));
item.setName(cursor.getString(1));
item.setLocation(cursor.getString(2));
//log
Log.d("geItem("+id+")", item.toString());
// 5. return book
return item;
}
public ArrayList<Item> getAllItems() {
ArrayList<Item> items = new ArrayList<>();
// 1. build the query
String query = "SELECT * FROM " + TABLE_ITEMS;
// 2. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
// 3. go over each row, build item and add it to list
Item item = null;
if (cursor.moveToFirst()) {
do {
item = new Item();
item.setId(Integer.parseInt(cursor.getString(0)));
item.setName(cursor.getString(1));
item.setLocation(cursor.getString(2));
// Add item to items
items.add(item);
} while (cursor.moveToNext());
}
Log.d("getAllItems()", items.toString());
// return items
return items;
}
public int updateItem(Item item) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put("name", item.getName()); // get title
values.put("location", item.getLocation()); // get author
// 3. updating row
int i = db.update(TABLE_ITEMS, //table
values, // column/value
KEY_ID+" = ?", // selections
new String[] { String.valueOf(item.getId()) }); //selection args
// 4. close
db.close();
return i;
}
public void deleteItem(Item item) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. delete
db.delete(TABLE_ITEMS, //table name
KEY_ID+" = ?", // selections
new String[] { String.valueOf(item.getId()) }); //selections args
// 3. close
db.close();
//log
Log.d("deleteItem",item.toString());
}
}
Content_locatie.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.buysse.roan.findyourstuff.Locatie"
tools:showIn="#layout/app_bar_locatie"
android:weightSum="1">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:stretchColumns="1"
android:id="#+id/tableLayout">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/textViewNameText"
android:id="#+id/textViewName" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editTextName" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableRow>
</TableLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_below="#+id/buttonAddUser"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:longClickable="true"
android:choiceMode="multipleChoice" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttonAddText"
android:id="#+id/buttonAddUser"
android:layout_span="2"
android:layout_below="#+id/tableLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="54dp"
android:layout_toStartOf="#+id/editTextLocation"
android:layout_alignRight="#+id/listView"
android:layout_alignEnd="#+id/listView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/textViewLocation"
android:layout_alignBottom="#+id/editTextLocation"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="#string/textViewLocation"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editTextLocation"
android:layout_below="#+id/tableLayout"
android:layout_alignRight="#+id/tableLayout"
android:layout_alignEnd="#+id/tableLayout"
android:layout_toEndOf="#+id/textViewLocation"
android:layout_toRightOf="#+id/textViewLocation" />
</RelativeLayout>
store list in activity you getting from db and pass it to adapter
ArrayList items = db.getAllItems()
remove item from this list also (u are removing from db only)
items.remove(0);
then call notifyDataSetChanged on adapter
I suggest using a CursorAdapter with your ListView. There are many tutorials online about how to do this.
I'm trying to do a simple retrieval of data from SQLite Database to the SearchView.
My problem is the SearchView is not populated with respect to the data stored in the database, although it always shows the green signal of
successfully created the database
Below is the code.
fragment_search.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="fill_parent"
android:background="#E6E6E6"
android:orientation="vertical" >
<View
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="#FFFFFF" />
<SearchView
android:id="#+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="3dp" >
</SearchView>
<ListView
android:id="#+id/listview_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/searchView"
android:layout_centerHorizontal="true"
android:divider="#E6E6E6"
android:dividerHeight="5dp" />
<LinearLayout
android:id="#+id/rightLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/view1"
android:layout_alignTop="#+id/view1"
android:orientation="vertical"
android:paddingTop="25dp" >
</LinearLayout>
</RelativeLayout>
Search_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/hotelLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/section_search"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="2" >
<ImageView
android:id="#+id/hotel_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="left"
android:src="#drawable/aaa" />
<EditText
android:id="#+id/hotel_name"
android:layout_width="209dp"
android:layout_height="56dp"
android:layout_gravity="fill_horizontal" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/hotel_city"
android:layout_width="96dp"
android:layout_column="1"
android:layout_gravity="left|bottom"
android:layout_row="0"
android:ems="10" />
<EditText
android:id="#+id/hotel_country"
android:layout_width="106dp"
android:layout_column="1"
android:layout_gravity="right|bottom"
android:layout_row="0"
android:ems="10" />
</GridLayout>
</TableRow>
</LinearLayout>
</RelativeLayout>
TableData.java
package com.mytry.test;
import android.provider.BaseColumns;
public class TableData
{
public TableData()
{
}
public static abstract class TableInfo implements BaseColumns
{
public static final String DATABASE_NAME = "tourDguide";
public static final String TABLE_NAME = "Hotels";
public static final String HOTEL_ID = "id";
public static final String HOTEL_NAME = "hotel_name";
public static final String HOTEL_ADDRESS = "hotel_address";
public static final String HOTEL_CITY = "hotel_city";
public static final String HOTEL_COUNTRY = "hotel_country";
public static final String HOTEL_POSTAL = "postal_code";
}
}
DataBaseHandler.java
package com.mytry.test;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.mytry.test.TableData.TableInfo;
public class DataHandler
{
public static final int DATABASE_VERSION = 1;
SQLiteDatabase db;
DataBaseHelper dbhelper;
Context ctx;
private static class DataBaseHelper extends SQLiteOpenHelper
{
public String CREATE_QUERY = "CREATE TABLE "+TableInfo.TABLE_NAME+"("+TableInfo.HOTEL_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+TableInfo.HOTEL_NAME+" VARCHAR,"+TableInfo.HOTEL_ADDRESS+" VARCHAR,"+TableInfo.HOTEL_CITY+" VARCHAR,"+TableInfo.HOTEL_COUNTRY+" VARCHAR,"+TableInfo.HOTEL_POSTAL+" INT );";
public DataBaseHelper(Context ctx) {
super(ctx,TableInfo.DATABASE_NAME, null, DATABASE_VERSION);
Log.d("Database Operations", "Successfully Created Database");
// TODO Auto-generated constructor stub
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try{
db.execSQL(CREATE_QUERY);
Log.d("Database Operations", "Successfully Created Table");
}
catch(SQLException e)
{
e.printStackTrace();
}
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("Drop table if exists "+TableInfo.TABLE_NAME);
onCreate(db);
}
}
public DataHandler(Context ctx) {
this.ctx = ctx;
dbhelper = new DataBaseHelper(ctx);
// TODO Auto-generated constructor stub
}
public DataHandler open()
{
dbhelper = new DataBaseHelper(ctx);
db = dbhelper.getReadableDatabase();
return this;
}
public void close()
{
dbhelper.close();
}
public Cursor searchHotels(String inputText) throws SQLException
{
String query = "Select "+TableInfo.HOTEL_ID+" as _id,"+TableInfo.HOTEL_NAME+","+TableInfo.HOTEL_ADDRESS+","+TableInfo.HOTEL_CITY+","+TableInfo.HOTEL_COUNTRY+","+TableInfo.HOTEL_POSTAL+" from "+TableInfo.TABLE_NAME+" where "+TableInfo.HOTEL_NAME+" LIKE '" + inputText + "';";
Log.d("table operations","Successfully transferred query");
Cursor cr = db.rawQuery(query, null);
if(cr!=null)
{
cr.moveToFirst();
}
return cr;
}
}
SeachViewActivity.java
package com.mytry.test;
import com.mytry.test.TableData.TableInfo;
import android.R.anim;
import android.app.Activity;
import android.app.DownloadManager.Query;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class SearchViewActivity extends Activity implements SearchView.OnQueryTextListener,SearchView.OnCloseListener
{
private ListView list;
private SearchView search;
private DataHandler dbHandler;
private TextView name,city,country;
private EditText edit;
Context ctx=this;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_search);
search = (SearchView) this.findViewById(R.id.searchView);
list = (ListView) this.findViewById(R.id.listview_search);
name = (TextView)this.findViewById(R.id.hotel_name);
city = (TextView)this.findViewById(R.id.hotel_city);
country = (TextView)this.findViewById(R.id.hotel_country);
search.setIconifiedByDefault(false);
search.setOnQueryTextListener(this);
search.setOnCloseListener(this);
dbHandler = new DataHandler(getBaseContext());
dbHandler.open();
}
public boolean onQueryTextSubmit(String query)
{
showResults(query + "*");
return false;
}
public boolean onQueryTextChange(String newText)
{
showResults(newText + "*");
return false;
}
public boolean onClose()
{
showResults("");
return false;
}
private void showResults(String query)
{
Cursor cr = dbHandler.searchHotels((query!=null?query.toString(): "####"));
if(cr==null)
{
}
else
{
String[] from = new String[]
{TableInfo.HOTEL_NAME,TableInfo.HOTEL_ADDRESS,TableInfo.HOTEL_CITY,TableInfo.HOTEL_COUNTRY,TableInfo.HOTEL_POSTAL};
int[] to = new int[]{R.id.hotel_name,R.id.hotel_city,R.id.hotel_country};
SimpleCursorAdapter hotels = new SimpleCursorAdapter(this,R.layout.search_list, cr, from, to);
list.setAdapter(hotels);
list.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Cursor cr = (Cursor)list.getItemAtPosition(position);
String hotel_name = cr.getString(cr.getColumnIndexOrThrow("hotel_name"));
String hotel_city = cr.getString(cr.getColumnIndexOrThrow("hotel_city"));
String hotel_country = cr.getString(cr.getColumnIndexOrThrow("hotel_country"));
LinearLayout hotelLayout = (LinearLayout)findViewById(R.id.hotelLayout);
if(hotelLayout == null){
//Inflate the Customer Information View
LinearLayout leftLayout = (LinearLayout)findViewById(R.id.rightLayout);
View hotelInfo = getLayoutInflater().inflate(R.layout.search_list, leftLayout, false);
leftLayout.addView(hotelInfo);
}
name.setText(hotel_name);
city.setText(hotel_city);
country.setText(hotel_country);
search.setQuery("", true);
}
});
}
}
}
i am new in android. i created a database and a listview. i want to add picture to listview by take picture by camera.
this is my input class:
package com.kalagar.warehouse;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class AddNew extends Activity implements View.OnClickListener {
ImageButton ib;
Button b;
ImageView iv;
EditText etName,etKharid,etForoush;
Intent i;
final static int CameraData = 0;
Bitmap bmp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addnew);
startAddNew();
}
private void startAddNew() {
ib = (ImageButton) findViewById(R.id.ibTakePic);
b = (Button) findViewById(R.id.bSave);
iv = (ImageView) findViewById(R.id.ivPic);
etName = (EditText) findViewById(R.id.etName);
etKharid = (EditText) findViewById(R.id.etKharid);
etForoush = (EditText) findViewById(R.id.etForoush);
b.setOnClickListener(this);
ib.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ibTakePic:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CameraData);
break;
case R.id.bSave:
boolean didItWork = true;
try {
String name = etName.getText().toString();
String kharid = etKharid.getText().toString();
String foroush = etForoush.getText().toString();
DataBase entry = new DataBase(AddNew.this);
entry.open();
entry.createEntry(name, kharid, foroush);
entry.close();
} catch (Exception e) {
// TODO Auto-generated catch block
didItWork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("oooh ! Nooo !!");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
e.printStackTrace();
}finally{
if (didItWork){
Dialog d = new Dialog(this);
d.setTitle("Heck Yea!");
TextView tv = new TextView(this);
tv.setText("Success");
d.setContentView(tv);
d.show();
}
}
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
iv.setImageBitmap(bmp);
}
}
}
this is my input 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" >
<EditText
android:id="#+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="نام محصول"
>
<requestFocus />
</EditText>
<EditText
android:id="#+id/etKharid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="قیمت خرید"
android:inputType="number"
/>
<EditText
android:id="#+id/etForoush"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="قیمت فروش"
android:inputType="number"
/>
<ImageButton
android:id="#+id/ibTakePic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:layout_gravity="center"/>
<ImageView
android:id="#+id/ivPic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:layout_gravity="center"/>
<Button
android:id="#+id/bSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ذخیره"
android:layout_gravity="center" />
</LinearLayout>
this is my DataBase :
package com.kalagar.warehouse;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBase {
private static final String LOGTAG = "WAREHOUSE";
private static final String DATABASE_TABLE = "WareHouse";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "SellList";
public static final String ROW_ID = "_id";
public static final String ROW_NAME = "nameOfObject";
public static final String ROW_KHARID = "ghBuy";
public static final String ROW_FOROUSH = "ghSell";
public static final String ROW_PICTURE = "picture";
private static final String TABLE_CREATE = "CREATE TABLE " + DATABASE_TABLE
+ " (" + ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + ROW_NAME
+ " TEXT, " + ROW_KHARID + " NUMERIC, " + ROW_FOROUSH
+ " NUMERIC, " + ROW_PICTURE + " TEXT " + ")";
private WareHouseDdbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class WareHouseDdbHelper extends SQLiteOpenHelper {
public WareHouseDdbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
Log.i(LOGTAG, "Table has been create");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
onCreate(db);
}
}
public DataBase(Context c) {
ourContext = c;
}
public DataBase open() throws SQLException {
ourHelper = new WareHouseDdbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
public long createEntry(String name, String kharid, String foroush) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(ROW_NAME, name);
cv.put(ROW_KHARID, kharid);
cv.put(ROW_FOROUSH, foroush);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public ArrayList<String> getDataName() {
// TODO Auto-generated method stub
String[] columns = new String[] { ROW_ID, ROW_NAME, ROW_KHARID,
ROW_FOROUSH };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
null, null);
ArrayList<String> result = new ArrayList<String>();
int iRow = c.getColumnIndex(ROW_ID);
int iName = c.getColumnIndex(ROW_NAME);
int iKharid = c.getColumnIndex(ROW_KHARID);
int iForoush = c.getColumnIndex(ROW_FOROUSH);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result.add(c.getString(iRow) + " " + c.getString(iName)
+ " " + c.getString(iKharid) + " " + c.getString(iForoush));
}
return result;
}
}
this is my class for show data in listView:
package com.kalagar.warehouse;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Show extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
Name();
}
private void Name() {
ListView tvName = (ListView) findViewById(R.id.lvListOfObject);
DataBase infoName = new DataBase(this);
infoName.open();
ArrayList<String> dataName = infoName.getDataName();
infoName.close();
tvName.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, dataName));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.show, menu);
return true;
}
}
and this is my ListView XML that i want add picture to each row:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/tvListHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:textSize="30dp"
android:text="لیست محصولات" />
<ListView
android:id="#+id/lvListOfObject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/searchView1"
android:layout_centerHorizontal="true" >
</ListView>
<SearchView
android:id="#+id/searchView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/tvListHeader" >
</SearchView>
</RelativeLayout>
how can i show ListView With this Template:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/ivPicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:maxHeight="80dp"
android:maxWidth="80dp"
android:src="#drawable/download" />
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/name_shown_here"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/tvKharid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/tvForoush"
android:layout_centerHorizontal="true"
android:text="Kharid" />
<TextView
android:id="#+id/tvForoush"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/ivPicture"
android:layout_alignLeft="#+id/tvKharid"
android:text="Foroush" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/tvKharid"
android:layout_alignParentRight="true"
android:text="قیمت خرید" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tvForoush"
android:layout_alignBottom="#+id/tvForoush"
android:layout_alignParentRight="true"
android:text="قیمت فروش" />
</RelativeLayout>
what code i need to add and where i need to add?
Sorry for my english !
why you don't save picture in Sdcard instead of databse.
This link help you to do so
I have a ListFragment with a Custom CursorAdapter connected to a database. The items in the database are not appearing in the ListView, including the ListHeader, and instead I see only a spinning wheel.
I had my app working fine with a SimpleCursorAdapter in the ListFragment before. I have tested this on non-empty databases.
The only clue I have as to why this is not working is that I get the following error in LogCat when I try the "Delete All" option in the ContextActionMenu.
ViewRootImpl sendUserActionEvent() == null
Here is the ListFragment:
import com.actionbarsherlock.app.SherlockListFragment;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.ActionMode.Callback;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import info.mariegrap.dancejournal.R.id;
import info.mariegrap.database.DanceContentProvider;
import info.mariegrap.database.DanceDatabaseHelper;
import info.mariegrap.database.StyleCursorAdapter;
import info.mariegrap.database.StyleTable;
import info.mariegrap.model.Style;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.database.Cursor;
import android.database.SQLException;
public class StyleFragment extends SherlockListFragment implements LoaderManager.LoaderCallbacks<Cursor>{
private static final int LOADER_ID = 0;
private Context context;
private OnStyleSelectedListener callback;
private StyleCursorAdapter styleAdapter;
private DanceDatabaseHelper myDbHelper;
private Callback mActionModeCallback;
private ActionMode mActionMode;
private long selectedId;
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
context = this.getActivity();
setHasOptionsMenu(true);
//setRetainInstance(true);
this.getListView().setBackgroundColor(getResources().getColor(R.color.style_background));
//this.getListView().setDividerHeight(0);
this.getListView().setScrollingCacheEnabled(true);
this.getListView().addHeaderView(new ListHeader(context,
this.getResources().getString(R.string.list_header_style_title),
this.getResources().getString(R.string.list_header_style_subtitle)));
this.setActionModeCallback();
this.setLongClickListener();
/*myDbHelper = new DanceDatabaseHelper(this.context, null,
null, 1);
try {
myDbHelper.openDataBase();;
}catch(SQLException sqle){
throw sqle;
}*/
this.styleAdapter = new StyleCursorAdapter(context, null, 0);
getLoaderManager().initLoader(LOADER_ID, null, this);
this.getListView().setAdapter(this.styleAdapter);
Log.d("mgrap", "Adapter: " + styleAdapter);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
callback = (OnStyleSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnStyleSelectedListener");
}
}
#Override
public void onListItemClick(ListView l, View v, int position, long id){
if (position == 0){
return;
}
this.setListAdapter(null);
this.getListView().getChildAt(position);
callback.onStyleSelected(position);
}
public interface OnStyleSelectedListener {
/** Called by StyleFragment when a list item is selected */
public void onStyleSelected(int position);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.style_actionbar, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.style_add:
ContentValues cv = new ContentValues();
cv.put(StyleTable.STYLE_KEY_NAME, "Tada!");
Uri uri = Uri.parse("content://info.mariegrap.dancejournal.provider/style_table/0");
Uri idUri = context.getContentResolver().insert(uri, cv);
//styleAdapter.notifyDataSetChanged();
fillData();
return true;
case R.id.style_delete_all:
Uri delete_uri = Uri.parse("content://info.mariegrap.dancejournal.provider/style_table");
context.getContentResolver().delete(delete_uri, null, null);
//styleAdapter.notifyDataSetChanged();
fillData();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void fillData() {
this.getSherlockActivity().getSupportLoaderManager().restartLoader(LOADER_ID, null, this);
this.getListView().setAdapter(styleAdapter);
}
protected void deleteStyle(long id) {
Uri uri = Uri.parse("content://info.mariegrap.dancejournal.provider/style_table/" + id);
this.getSherlockActivity().getContentResolver().delete(uri, null, null);
//styleAdapter.notifyDataSetChanged();
fillData();
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = { StyleTable.STYLE_KEY_ID, StyleTable.STYLE_KEY_NAME };
return new CursorLoader(this.context,
DanceContentProvider.CONTENT_URI_STYLE, projection, null, null, null);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
styleAdapter.swapCursor(data);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
styleAdapter.swapCursor(null);
}
private void setActionModeCallback(){
mActionModeCallback = new Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.style_context_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.style_delete:
deleteStyle(selectedId);
mode.finish();
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
}
private void setLongClickListener(){
this.getListView().setOnItemLongClickListener (new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (mActionMode != null || position == 0) {
return false;
}
mActionMode = getSherlockActivity().startActionMode(mActionModeCallback);
selectedId = id;
return true;
}
});
}
}
Here is the Custom CursorAdapter:
import view.StyleListItem;
import info.mariegrap.model.Style;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
public class StyleCursorAdapter extends CursorAdapter {
public StyleCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
}
#Override
public void bindView(View arg0, Context arg1, Cursor arg2) {
int id = arg2.getInt(StyleTable.STYLE_COL_ID);
String name = arg2.getString(StyleTable.STYLE_COL_NAME);
Style style = new Style(name, id);
StyleListItem listItem = (StyleListItem) arg0;
listItem.setStyle(style);
}
#Override
public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
int id = arg1.getInt(StyleTable.STYLE_COL_ID);
String name = arg1.getString(StyleTable.STYLE_COL_NAME);
Style style = new Style(name, id);
StyleListItem listItem = new StyleListItem(arg0, style);
listItem.setStyle(style);
Log.d("mgrap", "Adapter view: " + listItem.getStyleId());
return listItem;
}
}
Here is the ListItem View:
public class StyleListItem extends LinearLayout {
private TextView styleView;
private Style style;
public StyleListItem(Context context, Style style) {
super(context);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.style_row, this, true);
styleView = (TextView)findViewById(R.id.style_label);
Log.d("mgrap", "Style View: " + styleView);
setStyle(style);
}
public void setStyle(Style style) {
this.style = style;
displayStyle(this.style);
}
public Style getStyle(){
return style;
}
private void displayStyle(Style style) {
if (style != null) {
styleView.setText(style.getName());
}
}
public int getStyleId() {
return styleView.getId();
}
}
Here is the layout xml for the list items:
<?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="wrap_content" >
<ImageView
android:id="#+id/icon"
android:layout_width="30dp"
android:layout_height="24dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:src="#drawable/ic_launcher" >
</ImageView>
<TextView
android:id="#+id/style_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:lines="1"
android:text="#+id/TextView01"
android:textSize="24sp"
>
</TextView>
</LinearLayout>
Here is the xml layout for the list header:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/list_header_background_color"
android:layout_marginBottom="5dip" >
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:paddingTop="5dip"
android:paddingLeft="10dip"
android:paddingBottom="13dip"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true">
<TextView
android:id="#+id/listview_header_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/list_header_style_title"
android:textSize="17sp"
android:textStyle="bold"
android:textColor="#color/list_header_title_color"
/>
<TextView
android:id="#+id/listview_header_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/list_header_style_subtitle"
android:textSize="13sp"
android:textColor="#color/list_header_subtitle_color"
/>
</LinearLayout>
<ImageView
android:id="#+id/list_header_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="13dp"
android:visibility="gone"
android:contentDescription="#string/image_desc"
/>
</RelativeLayout>
And here is the class that defines the style database table:
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class StyleTable {
/** Style table in the database. */
public static final String TABLE_STYLE = "style_table";
/** Style table column names and IDs for database access. */
public static final String STYLE_KEY_ID = "_id";
public static final int STYLE_COL_ID = 0;
public static final String STYLE_KEY_NAME = "name";
public static final int STYLE_COL_NAME = STYLE_COL_ID + 1;
/** SQLite database creation statement. Auto-increments IDs of inserted
* styles. Style IDs are set after insertion into the database. */
public static final String DATABASE_CREATE = "create table " +
TABLE_STYLE + " (" +
STYLE_KEY_ID + " integer primary key autoincrement, " +
STYLE_KEY_NAME + " text);";
/** SQLite database table removal statement. Only used if upgrading
* database. */
public static final String DATABASE_DROP = "drop table if exists " +
TABLE_STYLE;
/**
* Initializes the database.
*
* #param database
* The database to initialize.
*/
public static void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
/**
* Upgrades the database to a new version.
*
* #param database
* The database to upgrade.
* #param oldVersion
* The old version of the database.
* #param newVersion
* The new version of the database.
*/
public static void onUpgrade(SQLiteDatabase database, int oldVersion,
int newVersion)
{
Log.w("mgrap", "updating database...");
database.execSQL(DATABASE_DROP);
onCreate(database);
}
}
It's hard to say without having some more logging to go with, but there are a couple things that jump out:
The adapter is being set on the list view directly instead of calling setAdapter on the ListFragment. This usually causes problems.
You're setting the list view's adapter after initializing the loader. This might not be a race condition since the callbacks would probably be called in the main loop after your call is finished, but I wouldn't count on it.
I am creating a checklist and it allows the user to add new items to the list, I am trying to allow the user to delete an item when they want to. So I am creating a contextual menu using this that comes us when the user long-presses an item in the list. Right now, it recognizes the long-press but nothing happens, so the contextual menu doesn't appear.
Here is my activity page: (ViewTask.java)
package com.example.androidhive;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class ViewTask extends Activity {
protected TaskerDbHelper db;
List<Task> list;
MyAdapter adapt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_task);
db = new TaskerDbHelper(this);
list = db.getAllTasks();
adapt = new MyAdapter(this, R.layout.list_inner_view, list);
ListView listTask = (ListView) findViewById(R.id.listView1);
listTask.setAdapter(adapt);
}
public void addTaskNow(View v) {
EditText t = (EditText) findViewById(R.id.editText1);
String s = t.getText().toString();
if (s.equalsIgnoreCase("")) {
Toast.makeText(this, "Enter a goal please!",
Toast.LENGTH_LONG);
} else {
Task task = new Task(s, 0);
db.addTask(task);
Log.d("tasker", "data added");
t.setText("");
adapt.add(task);
adapt.notifyDataSetChanged();
}
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.cmenu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.Delete_Task:
db.deleteTask(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.layout.activity_view_task, menu);
return true;
}
private class MyAdapter extends ArrayAdapter<Task> {
Context context;
List<Task> taskList = new ArrayList<Task>();
int layoutResourceId;
public MyAdapter(Context context, int layoutResourceId,
List<Task> objects) {
super(context, layoutResourceId, objects);
this.layoutResourceId = layoutResourceId;
this.taskList = objects;
this.context = context;
}
/**
* This method will DEFINe what the view inside the list view will
* finally look like Here we are going to code that the checkbox state
* is the status of task and check box text is the task name
*/
#Override
public View getView(int position, View convertView, ViewGroup parent) {
CheckBox chk = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_inner_view,
parent, false);
chk = (CheckBox) convertView.findViewById(R.id.chkStatus);
convertView.setTag(chk);
chk.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Task changeTask = (Task) cb.getTag();
changeTask.setStatus(cb.isChecked() == true ? 1 : 0);
db.updateTask(changeTask);
if(cb.isChecked())
{
Toast.makeText(
getApplicationContext(),
"Goal Accomplished!", Toast.LENGTH_LONG).show();
}
}
});
} else {
chk = (CheckBox) convertView.getTag();
}
Task current = taskList.get(position);
chk.setText(current.getTaskName());
chk.setChecked(current.getStatus() == 1 ? true : false);
chk.setTag(current);
Log.d("listener", String.valueOf(current.getId()));
return convertView;
}
}
}
Here is the xml page associated to this page:(activity_view_task.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DE8126"
android:orientation="vertical" >
<!-- Header Start -->
<LinearLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#layout/header_gradient"
android:orientation="vertical"
android:paddingBottom="5dip"
android:paddingTop="5dip" >
<!-- Logo Start -->
<ImageView
android:id="#+id/logoButton"
android:layout_width="match_parent"
android:layout_height="132dp"
android:src="#drawable/logo" />
<!-- Logo Ends -->
</LinearLayout>
<!-- Header End -->
<!-- Goals Title Start -->
<LinearLayout
android:id="#+id/goalsTitle"
android:layout_width="match_parent"
android:layout_height="80dp"
android:paddingTop="5dip"
android:paddingBottom="5dip">
<TextView
android:id="#+id/gTitle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="My Goals"
android:textSize="50sp"
android:textScaleX="1.5"
android:textStyle="bold"
android:gravity="center"/>
</LinearLayout>
<!-- Goals Title End -->
<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"
tools:context=".ViewTask" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="14dp"
android:text="#string/Save"
android:onClick="addTaskNow"/>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/button1" >
</ListView>
</RelativeLayout>
</LinearLayout>
And here is the menu itself: (cmenu.xml)
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/Edit_Task"></item>
<item android:id="#+id/Delete_Task"></item>
</menu>
Thanks you for the responses, now I cannot get the item to actually be deleted.
here is my database helper page:(TaskerDbHelper.java)
package com.example.androidhive;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class TaskerDbHelper extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "taskerManager";
// tasks table name
private static final String TABLE_TASKS = "tasks";
// tasks Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_TASKNAME = "taskName";
private static final String KEY_STATUS = "status";
public TaskerDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_TASKS + " ( "
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_TASKNAME+ " TEXT, "
+ KEY_STATUS + " INTEGER)";
db.execSQL(sql);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TASKS);
// Create tables again
onCreate(db);
}
public void addTask(Task task) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TASKNAME, task.getTaskName()); // task name
// status of task- can be 0 for not done and 1 for done
values.put(KEY_STATUS, task.getStatus());
// Inserting Row
db.insert(TABLE_TASKS, null, values);
db.close(); // Closing database connection
}
public boolean deleteTask(long task)
{
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_TASKS, KEY_TASKNAME + "=" + task, null) > 0;
}
public List<Task> getAllTasks() {
List<Task> taskList = new ArrayList<Task>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_TASKS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Task task = new Task();
task.setId(cursor.getInt(0));
task.setTaskName(cursor.getString(1));
task.setStatus(cursor.getInt(2));
// Adding contact to list
taskList.add(task);
} while (cursor.moveToNext());
}
// return task list
return taskList;
}
public void updateTask(Task task) {
// updating row
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TASKNAME, task.getTaskName());
values.put(KEY_STATUS, task.getStatus());
db.update(TABLE_TASKS, values, KEY_ID + " = ?",new String[] {String.valueOf(task.getId())});
db.close();
}
}
That file above contains my deleteTask().
I don't see registerForContextMenu() anywhere. This is necessary for the list to actually know that it has a contextmenu.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_task);
db = new TaskerDbHelper(this);
list = db.getAllTasks();
adapt = new MyAdapter(this, R.layout.list_inner_view, list);
ListView listTask = (ListView) findViewById(R.id.listView1);
listTask.setAdapter(adapt);
registerForContextMenu(listTask); // <-- Register!
}
In addition you should put a String value in your menu xml file. Obviously it is better to refer to your string xml file for the actual string.
<item
android:id="#+id/Edit_Task"
android:title="Edit">
</item>
<item
android:id="#+id/Delete_Task"
android:title="Delete">
</item>
Concerning the delete task:
To delete your task from the sql database you need some kind of identifier. As far as I can tell, you have only a task name and a status integer. If you allow tasks to have the same name, this cannot be used as an unique identifier. Perhaps you should add that to your Task class. You can then use this to find and remove the specific task in your database. (Your info.id will not work, because it is merely the identifier of the view) You can use info.position to retrieve the position of the Task in taskList, or more specifically, the current list in the adapter.
case R.id.Delete_Task:
Task task = list.get(info.position);
if (db.deleteTask(task.getUniqueIdentifier())) { // <-- Determine the unique id
list.Remove(info.position);
listTask.invalidate();
return true;
}
return false;
I'm not sure whether invalidate() will do the trick. But give it a try. You could also give the adapter the new list and call notifyDataSetChanged() as you do for the addTaskNow().
You need to add this line to your onCreate method
registerForContextMenu(listTask);
Your onCreateContextMenu() needs to look like this
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
if (v.getId() == R.id.listView1) { //YOUR VIEW THAT IS ATTACHED TO LISTASK
AdapterView.AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
menu.setHeaderTitle("Options");
String[] menuItems = { "Option1", "Option2", "Option3", "Option4"} };
for (int i = 0; i < menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
}
and finally your OnContextMenuItemSelected()
#Override
public boolean onContextItemSelected(android.view.MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
menuItemIndex = item.getItemId();
String[] menuItems = { "Option 1, Option 2, Option 3" };
String menuItemName = menuItems[menuItemIndex];
return true;
}