我有一个包含三个表的数据库。前两个表工作正常。我的第三个表似乎是在添加数据(字符串很好用于数据库。我在查看器中打开了数据库,所有列都在我输入的值中。
我在ActivityInvoice中的Recycleradapter中显示此信息时遇到问题。应用程序关闭。也许我错过了别人可能会看到的微不足道的东西。
如果我在InvoiceActivity中注释掉getAllInvoices,我可以在没有数据的情况下加载活动。
这是InvoiceActivity(应该在recyclerview中显示信息)
package com.nuclient.travisbunn.nuclient;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class InvoiceActivity extends AppCompatActivity implements View.OnClickListener{
private AppCompatActivity activity = InvoiceActivity.this;
private RecyclerView recyclerViewInvoices;
private List<Invoice> listInvoices;
private InvoiceRecyclerAdapter invoiceRecyclerAdapter;
private DatabaseHelper databaseHelper;
private FloatingActionButton myFab;
private FloatingActionButton myBack;
private FloatingActionButton myEdit;
private FloatingActionButton mySub;
private Invoice invoice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_invoice);
getSupportActionBar().setTitle("Invoices");
initViews();
initObjects();
initListeners();
}
// Update recyclerView onResume of activity
@Override
public void onResume(){
super.onResume();
getDataFromSQLite();
}
/**
* This method is to initialize views
*/
private void initViews() {
recyclerViewInvoices = (RecyclerView) findViewById(R.id.recyclerViewInvoices);
}
/**
* This method is to initialize listeners
*/
private void initListeners(){
myFab = (FloatingActionButton) findViewById(R.id.addButtonTwo);
myBack = (FloatingActionButton) findViewById(R.id.backButtonTwo);
myEdit = (FloatingActionButton) findViewById(R.id.editButtonTwo);
mySub = (FloatingActionButton) findViewById(R.id.minusButtonTwo);
myFab.setOnClickListener(this);
myBack.setOnClickListener(this);
myEdit.setOnClickListener(this);
mySub.setOnClickListener(this);
}
/**
* This method is to initialize objects to be used
*/
private void initObjects() {
listInvoices = new ArrayList<Invoice>();
invoiceRecyclerAdapter = new InvoiceRecyclerAdapter(listInvoices);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerViewInvoices.setLayoutManager(mLayoutManager);
recyclerViewInvoices.setItemAnimator(new DefaultItemAnimator());
recyclerViewInvoices.setHasFixedSize(true);
recyclerViewInvoices.setAdapter(invoiceRecyclerAdapter);
invoice = new Invoice();
databaseHelper = new DatabaseHelper(activity);
getDataFromSQLite();
}
/**
* This method is to fetch all invoice records from SQLite
*/
private void getDataFromSQLite() {
// AsyncTask is used that SQLite operation not blocks the UI Thread.
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
listInvoices.clear();
listInvoices.addAll(databaseHelper.getAllInvoices());
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
invoiceRecyclerAdapter.notifyDataSetChanged();
}
}.execute();
}
// Method that enables the buttons on bottom
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.addButtonTwo:
Intent intentAddInvoice = new Intent(this, AddInvoiceActivity.class);
startActivityForResult(intentAddInvoice, 1);
break;
case R.id.editButtonTwo:
//editChecked();
break;
case R.id.minusButtonTwo:
/*
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener);
builder.setNegativeButton("No", dialogClickListener).show();
*/
break;
case R.id.backButtonTwo:
finish();
break;
}
}
}
这是我的invoiceRecyclerAdapter:
package com.nuclient.travisbunn.nuclient;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
public class InvoiceRecyclerAdapter extends RecyclerView.Adapter<InvoiceRecyclerAdapter.InvoiceViewHolder> {
private List<Invoice> listInvoices;
public InvoiceRecyclerAdapter(List<Invoice> listInvoices) {
this.listInvoices = listInvoices;
}
@Override
public InvoiceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// inflating recycler item view
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_invoice_recycler, parent, false);
return new InvoiceViewHolder(itemView);
}
@Override
public void onBindViewHolder(InvoiceRecyclerAdapter.InvoiceViewHolder holder, int position) {
final int pos = position;
holder.textViewName.setText(listInvoices.get(position).getName());
holder.textViewDate.setText(listInvoices.get(position).getDate());
holder.textViewTime.setText(listInvoices.get(position).getTime());
holder.textViewMoney.setText(listInvoices.get(position).getMoney());
holder.textViewRate.setText(listInvoices.get(position).getRate());
/*
holder.checkBox.setChecked(listClients.get(position).isSelected());
holder.checkBox.setTag(listClients.get(position));
holder.checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Client contact = (Client) cb.getTag();
contact.setSelected(cb.isChecked());
listClients.get(pos).setSelected(cb.isChecked());
}
});
*/
}
@Override
public int getItemCount() {
Log.v(InvoiceRecyclerAdapter.class.getSimpleName(),""+listInvoices.size());
return listInvoices.size();
}
@Override
public long getItemId(int position) {
return position;
}
/**
* ViewHolder class
*/
public class InvoiceViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public AppCompatTextView textViewName;
public AppCompatTextView textViewDate;
public AppCompatTextView textViewTime;
public AppCompatTextView textViewMoney;
public AppCompatTextView textViewRate;
//public AppCompatCheckBox checkBox;
public InvoiceViewHolder(View view) {
super(view);
textViewName = (AppCompatTextView) view.findViewById(R.id.textviewInvoiceName);
textViewDate = (AppCompatTextView) view.findViewById(R.id.textviewInvoiceDate);
textViewTime = (AppCompatTextView) view.findViewById(R.id.textviewInvoiceTime);
textViewMoney = (AppCompatTextView) view.findViewById(R.id.textviewInvoiceMoney);
textViewRate = (AppCompatTextView) view.findViewById(R.id.textviewInvoiceRate);
//checkBox = (AppCompatCheckBox) view.findViewById(R.id.checkBox);
view.setOnClickListener(this);
}
@Override
public void onClick(View view) {
}
}
}
这是我的DatabaseHelper(仅限于空间的发票表):
package com.nuclient.travisbunn.nuclient;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
public class DatabaseHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "Manager.db";
/*
*
* Beginning of INVOICE Table
*
*/
// User table name
private static final String TABLE_INVOICE = "invoice";
// User Table Columns names
private static final String COLUMN_INVOICE_ID = "invoice_id";
private static final String COLUMN_INVOICE_NAME = "invoice_name";
private static final String COLUMN_INVOICE_DATE = "invoice_date";
private static final String COLUMN_INVOICE_TIME = "invoice_time";
private static final String COLUMN_INVOICE_MONEY = "invoice_money";
private static final String COLUMN_INVOICE_RATE = "invoice_rate";
// create table sql query
private String CREATE_INVOICE_TABLE = "CREATE TABLE " + TABLE_INVOICE + "("
+ COLUMN_INVOICE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_INVOICE_NAME + " TEXT,"
+ COLUMN_INVOICE_DATE + " TEXT," + COLUMN_INVOICE_TIME + " TEXT," + COLUMN_INVOICE_MONEY + " TEXT," + COLUMN_INVOICE_RATE + " TEXT" +")";
// drop table sql query
private String DROP_INVOICE_TABLE = "DROP TABLE IF EXISTS " + TABLE_INVOICE;
/*
*
* End of INVOICE Table
*
*/
/**
* Constructor
*/
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_USER_TABLE);
db.execSQL(CREATE_CLIENT_TABLE);
db.execSQL(CREATE_INVOICE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//Drop User Table if exist
db.execSQL(DROP_USER_TABLE);
//Drop Client table if exist
db.execSQL(DROP_CLIENT_TABLE);
//Drop Invoice table if exist
db.execSQL(DROP_INVOICE_TABLE);
// Create tables again
onCreate(db);
}
/*
*
*Start of INVOICE methods
*
*/
/**
* This method is to create invoice record
*/
public void addInvoice(Invoice invoice) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_INVOICE_NAME, invoice.getName());
values.put(COLUMN_INVOICE_DATE, invoice.getDate());
values.put(COLUMN_INVOICE_TIME, invoice.getTime());
values.put(COLUMN_INVOICE_MONEY, invoice.getMoney());
values.put(COLUMN_INVOICE_RATE, invoice.getRate());
// Inserting Row
db.insert(TABLE_INVOICE, null, values);
db.close();
}
/**
* This method is to fetch all invoice and return the list of client records
*
* @return list
*/
public List<Invoice> getAllInvoices() {
// array of columns to fetch
String[] columns = {
COLUMN_INVOICE_ID,
COLUMN_INVOICE_NAME,
COLUMN_INVOICE_DATE,
COLUMN_INVOICE_TIME,
COLUMN_INVOICE_MONEY,
COLUMN_INVOICE_RATE
};
// sorting orders
String sortOrder =
COLUMN_INVOICE_NAME + " ASC";
List<Invoice> invoiceList = new ArrayList<Invoice>();
SQLiteDatabase db = this.getReadableDatabase();
// query the client table
Cursor cursor = db.query(TABLE_INVOICE, //Table to query
columns, //columns to return
null, //columns for the WHERE clause
null, //The values for the WHERE clause
null, //group the rows
null, //filter by row groups
sortOrder); //The sort order
// Traversing through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Invoice invoice = new Invoice();
invoice.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(COLUMN_INVOICE_ID))));
invoice.setName(cursor.getString(cursor.getColumnIndex(COLUMN_INVOICE_NAME)));
invoice.setDate(cursor.getString(cursor.getColumnIndex(COLUMN_INVOICE_DATE)));
invoice.setTime(cursor.getString(cursor.getColumnIndex(COLUMN_INVOICE_TIME)));
invoice.setMoney(cursor.getString(cursor.getColumnIndex(COLUMN_INVOICE_MONEY)));
invoice.setRate(cursor.getString(cursor.getColumnIndex(COLUMN_INVOICE_RATE)));
// Adding client record to list
invoiceList.add(invoice);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
// return client list
return invoiceList;
}
/**
* This method to update invoice record
*
* @param invoice
*/
public void updateInvoice(Invoice invoice) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_INVOICE_NAME, invoice.getName());
values.put(COLUMN_INVOICE_DATE, invoice.getDate());
values.put(COLUMN_INVOICE_TIME, invoice.getTime());
values.put(COLUMN_INVOICE_MONEY, invoice.getMoney());
values.put(COLUMN_INVOICE_RATE, invoice.getRate());
// updating row
db.update(TABLE_INVOICE, values, COLUMN_INVOICE_ID + " = ?",
new String[]{String.valueOf(invoice.getId())});
db.close();
}
/**
* This method is to delete invoice record
*
* @param invoice
*/
public void deleteInvoice(Invoice invoice) {
SQLiteDatabase db = this.getWritableDatabase();
// delete invoice record by id
db.delete(TABLE_INVOICE, COLUMN_INVOICE_ID + " = ?",
new String[]{String.valueOf(invoice.getId())});
db.close();
}
/**
* This method to check client exist or not
*
*/
public boolean checkInvoice(String email) {
// array of columns to fetch
String[] columns = {
COLUMN_INVOICE_ID
};
SQLiteDatabase db = this.getReadableDatabase();
// selection criteria
String selection = COLUMN_INVOICE_NAME + " = ?";
// selection argument
String[] selectionArgs = {email};
// query client table with condition
Cursor cursor = db.query(TABLE_INVOICE, //Table to query
columns, //columns to return
selection, //columns for the WHERE clause
selectionArgs, //The values for the WHERE clause
null, //group the rows
null, //filter by row groups
null); //The sort order
int cursorCount = cursor.getCount();
cursor.close();
db.close();
return cursorCount > 0;
}
/*
*
*End of INVOICE methods
*
*/
}
答案 0 :(得分:0)
所以我解决了这个问题。谢谢你@Henry指出了logcat(当你问我马上想出怎么样的时候就这么新了)。在我的回收器适配器中,我有两个浮动按钮。拿出来解决了这个问题。我将不得不把它们放回去(并且希望它有效)但很可能是因为我从未宣布它们我只是让它们暂时坐在那里。