hi guys I'm new to android Studio.I just show my data in listview from SQLite database, was stuck and I can not give advice on how to fill list in fragment extend records from SQLite database. Looking and I have tried many tutorials, but no not sat down or failed. The biggest problem i got is null Exception error.
here is my DailyPurchase.java code
public class DailyPurchase extends Fragment {
private CustomersDbAdapter dbHelper;
ListView slist;
private SimpleCursorAdapter dataAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.dialy_purchase, container, false);
dbHelper = new CustomersDbAdapter(getActivity());
dbHelper.open();
displayListView();
return view;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void displayListView(){
Cursor cursor = dbHelper.queueAll();
slist = (ListView) getActivity().findViewById(R.id.listView21);
// The desired columns to be bound
String[] columns = new String[] {
CustomersDbAdapter.CASH_PURCHASE_AMOUNT,
CustomersDbAdapter.CASH_PURCHASE_COMPANY};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.scustomer,
R.id.sstate};
dataAdapter = new SimpleCursorAdapter(getActivity(), R.layout.dailypurchase_listview, cursor, columns, to,0);
try {
slist.setAdapter(dataAdapter);
} catch (Exception e) {
e1.printStackTrace();
}
}
}
Here is my CustomersDbAdapter.java code
public class CustomersDbAdapter {
public static final String TAG = "CustomersDbAdapter";
public DatabaseHelper mDbHelper;
public SQLiteDatabase mDb;
public static final String DIALY_PURCHASE_CASH_TABLE = "TableDailyPurchase";
public static final String CASH_PURCHASE_COMPANY = "companyName";
public static final String CASH_PURCHASE_AMOUNT = "cashinneramount";
public static final String CREATE_DIALY_PURCHASE_CASH = "CREATE TABLE "+DIALY_PURCHASE_CASH_TABLE+"" +
"("+KEY_ROWID+" INTEGER PRIMARY KEY AUTOINCREMENT," +
""+CASH_PURCHASE_COMPANY+" VARCHAR2(255)," +
""+CASH_PURCHASE_AMOUNT+" INTEGER," +
""+CASH_CREATED_AT+" DATETIME DEFAULT CURRENT_TIMESTAMP," +
"UNIQUE (" + CASH_PURCHASE_COMPANY +"))";
public final Context mCtx;
public static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DIALY_PURCHASE_CASH);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + DIALY_PURCHASE_CASH_TABLE);
onCreate(db);
}
}
public CustomersDbAdapter(Context ctx) {
this.mCtx = ctx;
}
public CustomersDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
if (mDbHelper != null) {
mDbHelper.close();
}
}
public long dialycash(String name, int amount ){
ContentValues initialValues = new ContentValues();
initialValues.put(CASH_PURCHASE_COMPANY, name);
initialValues.put(CASH_PURCHASE_AMOUNT, amount);
return mDb.insert(DIALY_PURCHASE_CASH_TABLE, null, initialValues);
}
public Cursor queueAll() {
String[] col = new String[] {KEY_ROWID,
CASH_PURCHASE_AMOUNT, CASH_PURCHASE_COMPANY, CASH_CREATED_AT};
Cursor mCursor = mDb.query(DIALY_PURCHASE_CASH_TABLE,col,
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}
Here is my XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:selectorandroid="http://ift.tt/GEGVYd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/newt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="newtransaction"
android:text="@string/transaction" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/myFilter"
android:hint="Search ....">
<requestFocus />
</EditText>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView21" />
</LinearLayout>
</LinearLayout>
please can anyone help me out to solve this error.And highlight my mistake
Thankyou.
Aucun commentaire:
Enregistrer un commentaire