无法弄清楚为什么TextView不会更新为SQLite结果

时间:2017-06-16 22:44:15

标签: java android sqlite

在我的KickCounter.java片段中,我有一个名为SQLiteOutput的TextView,我试图在计时器启动时将其更改为数据库第一行的任何值。问题是它没有改变,我不知道为什么。

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;
String kickString = kick.toString();
//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();


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

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

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

            chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {

                        @Override
                        public void onChronometerTick(Chronometer chronometer) {
                            if( chronometer.getText().toString().equalsIgnoreCase("00:05")) {
                                chronometer.stop();
                                kickButton.setVisibility(View.GONE);
                                removeKickButton.setVisibility(View.GONE);
                                Snackbar.make(getView(),"Your text",Snackbar.LENGTH_LONG).show();

                            }
                            if(kick == 10) {
                                chronometer.stop();
                                kickButton.setVisibility(View.GONE);
                                removeKickButton.setVisibility(View.GONE);
                                Snackbar.make(getView(),"heo",Snackbar.LENGTH_INDEFINITE).show();
                            }
                        }
            });

        }
    });

    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;
}

//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);
}

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";

//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 " +
            ");";
    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();
}

//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 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();
    }
    db.close();
    return dbString;
}

Product.java //这是数据库的getter和setter

public class Product {

private int _id;
private String _productname;

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 int get_id() {
    return _id;
}

public String get_productname() {
    return _productname;
}

fragment_kick_counter.xml

<android.support.constraint.ConstraintLayout 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:id="@+id/kick_counter_ID"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/kickCounterFragmentBackground"
tools:context="binarysoup.mommy_info.ActivitiesAndFragments.KickCounter">

<!-- TODO: Update blank fragment layout -->

<Button
    android:id="@+id/start_button_ID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:text="Start"
    android:layout_above="@+id/kick_button_ID"
    android:layout_centerHorizontal="true"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp"
    app:layout_constraintVertical_bias="0.651" />

<Button
    android:id="@+id/kick_button_ID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Kick!"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginStart="71dp"
    android:layout_marginLeft="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    app:layout_constraintVertical_bias="0.758"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintHorizontal_bias="0.814" />

<Button
    android:id="@+id/remove_kick_button_ID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Remove Kick"
    android:layout_alignBaseline="@+id/kick_button_ID"
    android:layout_alignBottom="@+id/kick_button_ID"
    android:layout_toRightOf="@+id/start_button_ID"
    android:layout_toEndOf="@+id/start_button_ID"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    app:layout_constraintVertical_bias="0.758"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintHorizontal_bias="0.105" />

<Chronometer
    android:id="@+id/chronometer_ID"
    android:textSize="50dp"
    android:layout_width="168dp"
    android:layout_height="56dp"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp"
    app:layout_constraintVertical_bias="0.086"
    app:layout_constraintHorizontal_bias="0.64" />

<TextView
    android:id="@+id/kicksText_ID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25dp"
    android:text="Kicks:"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp"
    app:layout_constraintHorizontal_bias="0.386"
    app:layout_constraintVertical_bias="0.299" />

<TextView
    android:id="@+id/kickCount_ID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25dp"
    android:text="00"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="136dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp"
    app:layout_constraintVertical_bias="0.298"
    app:layout_constraintLeft_toRightOf="@+id/kicksText_ID" />

<TextView
    android:id="@+id/SQLiteOutput_ID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Data"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp"
    app:layout_constraintVertical_bias="0.426" />

1 个答案:

答案 0 :(得分:3)

您的代码中存在两个问题:

  1. 首先,您应该更改UIThread上的textview,因此如果您想在printDatabase()类中调用KickCounter方法,它应该是这样的:

    this.getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            printDatabase();
        }
    });
    
  2. 您目前根本没有在代码中致电printDatabase()。您已在addButtonClicked()中调用此方法,但未在代码中的任何位置调用此addButtonClicked()方法!