如何在单击相应的列表视图项时创建包含SQLite信息的对话框?

时间:2017-08-21 22:24:14

标签: java android sqlite android-sqlite android-dialog

我有一个预算应用程序,在单独的片段中,我显示了由用户提供的收入和费用。我想创建一个对话框,显示用户填写费用/收入表时插入的日期和注释。

下面是我的数据库助手,以便在我不清楚我的意思时显示信息。

package com.dharquissandas.budget;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.View;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseHelper";

public static final String DATABASE_NAME = "budget.db";
public static final String TABLE_NAME = "expense_table";
public static final String TABLE_NAME2 = "income_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "ID2";
public static final String EXPENSE_AMOUNT = "AMOUNT";
public static final String EXPENSE_DATE = "DATE";
public static final String EXPENSE_NOTES = "NOTES";
public static final String INCOME_AMOUNT = "AMOUNT";
public static final String INCOME_DATE = "DATE";
public static final String INCOME_NOTES = "NOTES";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 3);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,AMOUNT INTEGER,DATE INTEGER,NOTES TEXT)");
    db.execSQL("create table " + TABLE_NAME2 + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,AMOUNT INTEGER,DATE INTEGER,NOTES TEXT)");

}

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

public boolean insertexpenseData(String amount_expense, String date_expense, String notes_expense) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(EXPENSE_AMOUNT, amount_expense);
    contentValues.put(EXPENSE_DATE, date_expense);
    contentValues.put(EXPENSE_NOTES, notes_expense);
    long result = db.insert(TABLE_NAME, null, contentValues);
    if (result == -1)
        return false;
    else
        return true;
}

public boolean insertincomeData(String amount_income, String date_income, String notes_income) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(INCOME_AMOUNT, amount_income);
    contentValues.put(INCOME_DATE, date_income);
    contentValues.put(INCOME_NOTES, notes_income);
    long result = db.insert(TABLE_NAME2, null, contentValues);
    if (result == -1)
        return false;
    else
        return true;
}

public Cursor getexpenseData() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
    return res;
}

public Cursor getincomeData() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from " + TABLE_NAME2, null);
    return res;
}

public Cursor getexpenseItemID(String name){
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT " + COL_1 + " FROM " + TABLE_NAME + " WHERE " + EXPENSE_AMOUNT + " = '" + name + "'";
    Cursor data = db.rawQuery(query, null);
    return data;
}

public Cursor getincomeItemID(String name){
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT " + COL_2 + " FROM " + TABLE_NAME2 + " WHERE " + INCOME_AMOUNT + " = '" + name + "'";
    Cursor data = db.rawQuery(query, null);
    return data;
}

public boolean updateexpenseData(String id, String amount, String date, String notes) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_1, id);
    contentValues.put(EXPENSE_AMOUNT, amount);
    contentValues.put(EXPENSE_DATE, date);
    contentValues.put(EXPENSE_NOTES, notes);
    db.update(TABLE_NAME, contentValues, "ID = ?", new String[]{id});
    return true;
}

public boolean updateincomeData(String id, String amount, String date, String notes) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_2, id);
    contentValues.put(INCOME_AMOUNT, amount);
    contentValues.put(INCOME_DATE, date);
    contentValues.put(INCOME_NOTES, notes);
    db.update(TABLE_NAME2, contentValues, "ID = ?", new String[]{id});
    return true;
}

public Integer deleteexpenseData(String id) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(TABLE_NAME, "ID = ?", new String[]{id});
}

public Integer deleteincomeData(String id) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(TABLE_NAME2, "ID = ?", new String[]{id});
}


}

以下是我希望点击特定金额以显示与其相关的其他信息的列表。

package com.dharquissandas.budget;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.database.Cursor;
import android.util.Log;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.widget.Toast;

import java.util.ArrayList;

import static android.R.attr.button;

public class tab3expense extends Fragment {

    private static final String TAG = "tab3expense";
    DatabaseHelper mDatabaseHelper;
    private ListView mListView;
    View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.tab3expense, container, false);
        return rootView;



    }

    @Override
    public void onActivityCreated( Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mListView = (ListView) rootView.findViewById(R.id.listViewexpense);
        mDatabaseHelper = new DatabaseHelper(getActivity());

        populateListView();
    }

    private void populateListView() {
        Log.d(TAG, "populateListView: Displaying data in the ListView.");

        //get the data and append to a list
        Cursor data = mDatabaseHelper.getexpenseData();
        ArrayList<String> listData = new ArrayList<>();
        while(data.moveToNext()){
            //get the value from the database in column 1
            //then add it to the ArrayList
            listData.add(data.getString(1));
        }
        //create the list adapter and set the adapter
        ListAdapter adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, listData);
        mListView.setAdapter(adapter);

        //set an onItemClickListener to the ListView
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String name = adapterView.getItemAtPosition(i).toString();
                Log.d(TAG, "onItemClick: You Clicked on " + name);

                Cursor data = mDatabaseHelper.getexpenseItemID(name); //get the id associated with that name
                int itemID = -1;
                while(data.moveToNext()){
                    itemID = data.getInt(0);
                }
                if(itemID > -1){
                    Log.d(TAG, "onItemClick: The ID is: " + itemID);
                    Intent editScreenIntent = new Intent(getActivity(), add_expense.class);
                    editScreenIntent.putExtra("id",itemID);
                    editScreenIntent.putExtra("name",name);
                    startActivity(editScreenIntent);
                }
                else{
                    toastMessage("No ID associated with that name");
                }
            }
        });
    }

    /**
     * customizable toast
     * @param message
     */
    private void toastMessage(String message){
        Toast.makeText(getActivity(),message, Toast.LENGTH_SHORT).show();
    }
}

很抱歉发布了很多代码我是android编程的新手,我真的很感激帮助。

1 个答案:

答案 0 :(得分:0)

如果你按照@ Nongthonbam Tonthoi所说的那样,你会发现它实现起来非常简单,但我会在下面给你看一些示例代码。

您可以为自定义对话框创建一个方法,该方法包含两个参数 - 注释和日期值,如下所示

public void displayNoteDate(String noteContent, String dateValue) {
    final Dialog builder = new Dialog(this);
    builder.setContentView(R.layout.custom_dialog);

    builder.setTitle("Display note date");

    TextView note = (TextView)builder.findViewById(R.id.note);
    TextView date = (TextView)builder.findViewById(R.id.date);

    //add the database note and date
    note.setText(noteContent);
    date.setText(dateValue);

    Button closeButton = (Button)builder.findViewById(R.id.close);
    closeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            builder.dismiss();
        }
    });
    builder.show();
}

xml布局文件包含2个用于注释和日期的TextView以及一个用于关闭对话框的按钮。

<强> custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical">

<TextView
    android:id="@+id/note"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""/>

<TextView
    android:id="@+id/date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="12dp"
    android:text=""/>

<Button
    android:id="@+id/close"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="40dp"
    android:layout_gravity="center"
    android:text="close"/>

</LinearLayout>

希望有所帮助