我正在制作一个可以存储3个数据收入,ExpenseName和ExpenseCost的应用程序。我正在尝试仅获取txt_incomeview的收入,而忽略其他2个数据,但我不知道ExpenseName和ExpenseCost的用法和相同之处。在数据库中,我将收入保存在同一张表中,但ExpenseName和ExpenseCost的值为空。
收入
例如nullnull100
与ExpenseName和ExpenseCost相同
Ex.name100null
就像我将其保存在SQL数据库中
MainActivity:
package com.example.back4app.userregistrationexample.Classes;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.example.back4app.userregistrationexample.Data.DatabaseHelper;
import com.example.back4app.userregistrationexample.R;
public class MainActivity extends AppCompatActivity {
DatabaseHelper mDatabaseHelper;
private TextView nameview, costview, income;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
income = (TextView) findViewById(R.id.txt_incomeview);
mDatabaseHelper = new DatabaseHelper(this);
Cursor cursor = mDatabaseHelper.getData();
if(cursor.getCount()==0){
Toast.makeText(getApplicationContext(), "No Data", Toast.LENGTH_SHORT).show();
}
else{
while (cursor.moveToNext()){
}
}
final Button btn_income = findViewById(R.id.btn_Income);
btn_income.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, IncomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
final Button btn_expense = findViewById(R.id.btn_Expense);
btn_expense.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ExpenseActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}
}
DatabaseHelper:
package com.example.back4app.userregistrationexample.Data;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseHelper";
private static final String TABLE_NAME = "Financial";
private static final String COL1 = "ID";
private static final String COL2 = "item";
private static final String COL3 = "cost";
private static final String COL4 = "income";
public DatabaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 + " TEXT, " +
COL3 + " INTEGER, " +
COL4 + " INTEGER);";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item, String cost, String income) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item);
contentValues.put(COL3, cost);
contentValues.put(COL4, income);
Log.d(TAG, "addData: Adding " + item + cost + income + " to " + TABLE_NAME);
long result = db.insert(TABLE_NAME, null, contentValues);
//if date as inserted incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
/**
* Returns all the data from database
* @return
*/
public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
}
/**
* Returns only the ID that matches the name passed in
* @param name
* @return
*/
public Cursor getItemID(String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COL1 + " FROM " + TABLE_NAME +
" WHERE " + COL2 + " = '" + name + "'";
Cursor data = db.rawQuery(query, null);
return data;
}
/**
* Updates the name field
* @param newName
* @param id
* @param oldName
*/
public void updateName(String newName, int id, String oldName){
SQLiteDatabase db = this.getWritableDatabase();
String query = "UPDATE " + TABLE_NAME + " SET " + COL2 +
" = '" + newName + "' WHERE " + COL1 + " = '" + id + "'" +
" AND " + COL2 + " = '" + oldName + "'";
Log.d(TAG, "updateName: query: " + query);
Log.d(TAG, "updateName: Setting name to " + newName);
db.execSQL(query);
}
/**
* Delete from database
* @param id
* @param name
*/
public void deleteName(int id, String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "DELETE FROM " + TABLE_NAME + " WHERE "
+ COL1 + " = '" + id + "'" +
" AND " + COL2 + " = '" + name + "'";
Log.d(TAG, "deleteName: query: " + query);
Log.d(TAG, "deleteName: Deleting " + name + " from database.");
db.execSQL(query);
}
}
我尝试使用
ArrayList<String> listData = new ArrayList<>();
但是我在youtube上看到的这种方法仅在ListView上显示1列,但我希望在textview中查看它