2017-07-20 14 views
1

2列のデータベースを印刷したいだけですが、TextViewには1列しか表示されません。最初の列はユーザ​​ー入力で、2番目の列はユーザ​​ー入力の日付です。問題は、MyDBHandler.javaクラスのdatabaseToString()メソッドの2回目のwhileループ、またはProduct.javaクラスのgettersおよびsetterのいずれかで問題が発生している可能性があります。2番目のSQLiteデータベースの列をTextViewに取得できません

MyDBHandler.java

public class MyDBHandler extends SQLiteOpenHelper { 

private static final int DATABASE_VERSION = 1; 
private static final String DATABASE_NAME = "productDB.db"; 
public static final String KICK_TABLE = "products"; 
public static final String COLUMN_ID = "_id"; 
public static final String COLUMN_PRODUCTNAME = "productname"; 
public static final String COLUMN_PRODUCTNAME_DATE = "productnamedate"; 

//We need to pass database information along to superclass 
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { 
    super(context, DATABASE_NAME, factory, DATABASE_VERSION); 
} 

/* 
CREATE TABLE table_name (
     column_1 data_type, 
     column_2 data_type, 
     column_3 data_type 
); 
*/ 

@Override 
public void onCreate(SQLiteDatabase db) { 
    String query = "CREATE TABLE " + 
      KICK_TABLE + "(" + 
      COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
      COLUMN_PRODUCTNAME + " TEXT, " + 
      COLUMN_PRODUCTNAME_DATE + " TEXT);"; 
    db.execSQL(query); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS " + KICK_TABLE); 
    onCreate(db); 
} 

//Add a new row to the database 
public void addProduct(Product product){ 
    SQLiteDatabase db = getWritableDatabase(); 
    ContentValues values = new ContentValues(); 
    values.put(COLUMN_PRODUCTNAME, product.get_productname()); 
    db.insert(KICK_TABLE, null, values); 
    db.close(); 
} 

//Add a new row to the database 
public void addProductDate(Product productdate){ 
    SQLiteDatabase db = getWritableDatabase(); 
    ContentValues values = new ContentValues(); 
    values.put(COLUMN_PRODUCTNAME_DATE, productdate.get_productnamedate()); 
    db.insert(KICK_TABLE, null, values); 
    db.close(); 
} 

//Delete a product from the database 
public void deleteProduct(String productName){ 
    SQLiteDatabase db = getWritableDatabase(); 
    db.execSQL("DELETE FROM " + KICK_TABLE + " WHERE " + COLUMN_PRODUCTNAME + "=\"" + productName + "\";"); 
} 

public Integer dbSize(){ 
    SQLiteDatabase db = getWritableDatabase(); 
    Cursor mCount = db.rawQuery("SELECT count(" + COLUMN_PRODUCTNAME + ") FROM " + KICK_TABLE, null); 
    mCount.moveToFirst(); 
    int count = mCount.getInt(0); 

    mCount.close(); 
    return count; 
} 

public String databaseToString(){ 
    String dbString = ""; 
    SQLiteDatabase db = getWritableDatabase(); 
    String query = "SELECT * FROM " + KICK_TABLE + " WHERE 1"; 

    //Cursor points to a location in your results 
    Cursor c = db.rawQuery(query, null); 
    //Move to the first row in your results 
    c.moveToFirst(); 

    //Position after the last row means the end of the results 
    while (!c.isAfterLast()) { 
     if (c.getString(c.getColumnIndex("productname")) != null) { 
      dbString += c.getString(c.getColumnIndex("productname")); 
      dbString += "\n"; 
     } 
     c.moveToNext(); 
    } 

    //this is the sql date attempt 
    c.getColumnIndex("productnamedate"); 
    c.moveToFirst(); 

    //Position after the last row means the end of the results 
    while (!c.isAfterLast()) { 
     if (c.getString(c.getColumnIndex("productnamedate")) != null) { 
      dbString += c.getString(c.getColumnIndex("productnamedate")); 
      dbString += "\n"; 
     } 
     c.moveToNext(); 
     } 

    db.close(); 
    return dbString; 
} 

Product.java

public class Product { 

private int _id; 
private String _productname; 
private String _productnamedate; 

public Product(){ 
} 

public Product(String productname){ 
    this._productname = productname; 
} 

public void set_id(int _id) { 
    this._id = _id; 
} 

public void set_productname(String _productname) { 
    this._productname = _productname; 
} 

public void set_productnamedate(String _productnamedate) { 
    this._productnamedate = _productnamedate; 
} 

public int get_id() { 
    return _id; 
} 

public String get_productname() { 
    return _productname; 
} 
public String get_productnamedate() { 
    return _productnamedate; 
} 

}

KickCounter.java

public class KickCounter extends Fragment { 
// TODO: Rename parameter arguments, choose names that match 
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 
private static final String ARG_PARAM1 = "param1"; 
private static final String ARG_PARAM2 = "param2"; 

// TODO: Rename and change types of parameters 
private String mParam1; 
private String mParam2; 

private OnFragmentInteractionListener mListener; 

public KickCounter() { 
    // Required empty public constructor 
} 

/** 
* Use this factory method to create a new instance of 
* this fragment using the provided parameters. 
* 
* @param param1 Parameter 1. 
* @param param2 Parameter 2. 
* @return A new instance of fragment KickCounter. 
*/ 
// TODO: Rename and change types and number of parameters 
public static KickCounter newInstance(String param1, String param2) { 
    KickCounter fragment = new KickCounter(); 
    Bundle args = new Bundle(); 
    args.putString(ARG_PARAM1, param1); 
    args.putString(ARG_PARAM2, param2); 
    fragment.setArguments(args); 
    return fragment; 
} 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if (getArguments() != null) { 
     mParam1 = getArguments().getString(ARG_PARAM1); 
     mParam2 = getArguments().getString(ARG_PARAM2); 
    } 
} 

//SQLite 
//Add a product to the database 
/*public void addButtonClicked(View view){ 
    Product product = new Product(kickString); 
    dbHandler.addProduct(product); 
    printDatabase(); 
}*/ 

Button kickButton; 
Button removeKickButton; 
Button startButton; 
Chronometer chronometer; 
TextView kickCount; 
Integer kick = 0; 
//SQLite 

TextView sQLiteOutput; 

//SQLite 
MyDBHandler dbHandler; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.fragment_kick_counter, container, false); 

    kickButton = (Button)rootView.findViewById(R.id.kick_button_ID); 
    kickButton.setVisibility(View.INVISIBLE); 
    removeKickButton = (Button)rootView.findViewById(R.id.remove_kick_button_ID); 
    removeKickButton.setVisibility(View.INVISIBLE); 
    startButton = (Button)rootView.findViewById(R.id.start_button_ID); 
    chronometer = (Chronometer)rootView.findViewById(R.id.chronometer_ID); 
    kickCount = (TextView)rootView.findViewById(R.id.kickCount_ID); 
    sQLiteOutput = (TextView)rootView.findViewById(R.id.SQLiteOutput_ID); 

    //SQLite 
    dbHandler = new MyDBHandler(getContext(), null, null, 1); 
    // printDatabase(); 

    String dbString = dbHandler.databaseToString(); 
    sQLiteOutput.setText(dbString); 


    startButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      startButton.setVisibility(View.GONE); 

      kickButton.setVisibility(View.VISIBLE); 
      removeKickButton.setVisibility(View.VISIBLE); 

      //trying to make SQL date work here 
      String dateString = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); 

      chronometer.setBase(SystemClock.elapsedRealtime()); 
      chronometer.start(); 

      chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() { 

         @Override 
         public void onChronometerTick(Chronometer chronometer) { 
          if(chronometer.getText().toString().equalsIgnoreCase("00:05")) { 
           Snackbar.make(getView(),"Your text",Snackbar.LENGTH_LONG).show(); 
           end(); 
          } 
          if(kick == 10) { 
           Snackbar.make(getView(),"heo",Snackbar.LENGTH_INDEFINITE).show(); 
           end(); 
          } 

         } 
      }); 

     } 
    }); 

    kickButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(kick < 10){ 
       kick++; 
       setKickCountText(); 
      } 
      else{ 
       return; 
      } 
     } 
    }); 

    removeKickButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(kick != 0){ 
       kick--; 
       setKickCountText(); 
      } 
      else{ 
       return; 
      } 
     } 
    }); 

    // Inflate the layout for this fragment 
    return rootView; 
} 

public void end(){ 
    chronometer.stop(); 
    kickButton.setVisibility(View.GONE); 
    removeKickButton.setVisibility(View.GONE); 

    String kickString = kick.toString(); 
    String dateString = "hello";//new SimpleDateFormat("yyyy-MM-dd").format(new Date()); 


    Product product = new Product(kickString); 
    Product productDate = new Product(dateString); 

    dbHandler.addProduct(product); 
    dbHandler.addProductDate(productDate); 

    printDatabase(); 
} 



//SQLite 
//Print the database 
public void printDatabase(){ 
    String dbString = dbHandler.databaseToString(); 
    sQLiteOutput.setText(dbString); 
    //buckysInput.setText(""); 
} 

public void setKickCountText(){ 
    kickCount.setText(kick.toString()); 
} 

// TODO: Rename method, update argument and hook method into UI event 
public void onButtonPressed(Uri uri) { 
    if (mListener != null) { 
     mListener.onFragmentInteraction(uri); 
    } 
} 

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 
    if (context instanceof OnFragmentInteractionListener) { 
     mListener = (OnFragmentInteractionListener) context; 
    } else { 
     throw new RuntimeException(context.toString() 
       + " must implement OnFragmentInteractionListener"); 
    } 
} 

@Override 
public void onDetach() { 
    super.onDetach(); 
    mListener = null; 
} 

/** 
* This interface must be implemented by activities that contain this 
* fragment to allow an interaction in this fragment to be communicated 
* to the activity and potentially other fragments contained in that 
* activity. 
* <p> 
* See the Android Training lesson <a href= 
* "http://developer.android.com/training/basics/fragments/communicating.html" 
* >Communicating with Other Fragments</a> for more information. 
*/ 
public interface OnFragmentInteractionListener { 
    // TODO: Update argument type and name 
    void onFragmentInteraction(Uri uri); 
} 
+2

Q:ありあなたはデバッガをステップ実行すると考えましたか? – paulsm4

+2

'databaseToString'では' while'ループ内の両方のフィールドの値を取得する必要があります。 **しかし、**データを挿入すると、実際には 'productname'データを' productname'データと一緒に挿入していないことを推測しています - それらは 'one'ローでなければなりません。 –

答えて

3

あなたが実際に代わり一つで、二つの異なる行を挿入します:

dbHandler.addProduct(product); 
dbHandler.addProductDate(productDate); 

たび、あなたは二つの異なる行の挿入、テーブルにデータを挿入:この問題を解決するには

----------------------------------------------- 
| Id | product_name | product_date   | 
----------------------------------------------- 
| 1 | SOME_NAME | null     | 
----------------------------------------------- 
| 2 | null   | SOME_DATE    | 
----------------------------------------------- 

を、代わりに2の1つのinsertメソッドを作る:

//Add a new row to the database 
public void addProduct(Product product, Product productdate){ 
    SQLiteDatabase db = getWritableDatabase(); 
    ContentValues values = new ContentValues(); 
    values.put(COLUMN_PRODUCTNAME, product.get_productname()); 
    values.put(COLUMN_PRODUCTNAME_DATE, productdate.get_productnamedate()); 
    db.insert(KICK_TABLE, null, values); 
    db.close(); 
} 
+0

彼は彼の検索結果をよく –

関連する問題