2017-04-18 11 views
0

私は3つのテーブルを持つデータベースを持っています。最初の2つのテーブルは正常に動作します。私の3番目のテーブルはデータを追加しているようです(データベースに文字列を追加しました)。ビューアにデータベースを開き、入力した値ですべての列が配置されました。データベースで問題を見つけることができません

この情報をActivityInvoiceのRecycleradapterに表示すると、 。アプリケーションはシャットダウンします。たぶん、私は他の誰かが見るかもしれないという非常に小さい何かが欠けている。

私はInvoiceActivityで呼び出さgetAllInvoicesをコメントアウト場合はデータなしでロードする活性を得ることができます。

ここInvoiceActivity(万一がありますリサイクルビューの情報を表示)

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

正確なエラーは何ですか?あなたのlogcatを投稿してください。 – Henry

+0

4月18日00:24:09.546 6824から6824/com.nuclient.travisbunn.nuclient E/AndroidRuntime:致命的な例外:メインプロセス: com.nuclient.travisbunn.nuclient、PID:6824 android.view.InflateException :バイナリXMLファイルライン#168:インフレータブルクラスのクラス もっと後ろのインフレータとリサイクルアダプターについてもっと知りたい場合はインフレータを見てください... @Henry –

+0

あなたが間違っていないかどうかを確認してくださいレイアウトファイルの1つ。 – Henry

答えて

0

だから私はこれを解決しました。ありがとう@あなたが即座にどのように考え出したか尋ねたときに、logcatを指摘してくれてありがとう。私のリサイクラーアダプターには2つの浮動ボタンがありました。それらを取り出して問題を解決しました。私はそれらを戻さなければなりません(うまくいけばうまくいきます)が、私はそれらを宣言したことはないので、一時的にそこに座っていた可能性が高いです。

関連する問題