关闭并重新打开Android应用

时间:2016-07-25 23:48:06

标签: android database listview

在我的MainActivity中,我出现了一个对话框,用户在两个EditTexts中输入他们的信息。第一个EditText是他们的名字,第二个是人欠的金额。然后将此数据保存到数据库中,并显示在listView中。当我关闭应用程序并重新打开它时,我从数据库中获取值,但由于某种原因,每个视图的编号从1到我输入数据的次数,并且数值数据都设置为0.任何帮助都会很大赞赏。这是我的代码。

public class MainActivity extends AppCompatActivity
{
 FloatingActionButton fab;
 private ListView listView;
 private ArrayAdapter<OweInfo> adapter;

private PayUpDatabaseHelper db = new PayUpDatabaseHelper(this);
Context context;

ArrayList<OweInfo> owesList;


@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    context = this.getApplicationContext();
    owesList = db.getAllOwes();
    adapter = new OwesAdapter(this, R.layout.payup_list_row, owesList);
    listView = (ListView)findViewById(R.id.list);

    if(adapter != null && listView != null )
    {
        listView.setAdapter(adapter);
    }
    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


   fab = (FloatingActionButton)findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            //fire up the dialog!
            startDialog();
        }
    });

}

public void startDialog()
{
    //Instantiate an AlertDialog.Builder with its constructor
    AlertDialog.Builder builder = new        AlertDialog.Builder(MainActivity.this);
    //get layout inflater
    LayoutInflater inflater = getLayoutInflater();

    //inflate and set the layout for the dialog
    // pass null as the parent view because its going in the dialog layout
        final View view = inflater.inflate(R.layout.fragment_owe_dialog,null);
        builder.setView(view)

            //add action buttons
            .setPositiveButton(R.string.positive_button, new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int id)
                {
                    EditText nameEditText = (EditText)view.findViewById(R.id.name_edit_text);
                    EditText amountEditText = (EditText)view.findViewById(R.id.amount_edit_text);

                    String getNameText = nameEditText.getText().toString();
                    int getAmountText = Integer.parseInt(amountEditText.getText().toString());

                    OweInfo owes = new OweInfo(getNameText, getAmountText);
                    db.addOwe(owes);
                    adapter.add(owes);
                    adapter.notifyDataSetChanged();
                }
            })
            .setNegativeButton(R.string.negative_button, new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int id)
                {
                    dialog.dismiss();
                }
            });

                builder.create().show();
}




@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    return super.onOptionsItemSelected(item);
}


 }

这是我的数据库的代码

public class PayUpDatabaseHelper extends SQLiteOpenHelper
{
 public SQLiteDatabase db;

//Database name
public static final String DB_NAME = "com.ch1bi.payup";

//Database version
public static final int DB_VERSION = 1;

//PayUp table name
public static final String TABLE_NAME = "owes";

//PayUp table column names
public static final String KEY_ID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_AMOUNT = "amount";

public PayUpDatabaseHelper(Context context)
{
    super(context, DB_NAME, null, DB_VERSION);
}

//Create table
@Override
public void onCreate(SQLiteDatabase db)
{
    String createTable = "CREATE TABLE " + TABLE_NAME + "("
            + KEY_ID + " INTEGER PRIMARY KEY,"
            + KEY_NAME + " TEXT NOT NULL,"
            + KEY_AMOUNT + " TEXT NOT NULL)";

    db.execSQL(createTable);
}

//code for upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

    onCreate(db);
}

public void addOwe(OweInfo info)
{
    //open database to write to it
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, info.getNameExtra());
    values.put(KEY_AMOUNT, info.getAmountExtra());

    //insert values into row
    db.insert(TABLE_NAME, null, values);

    //close connection after writing to db
    db.close();
}

public ArrayList<OweInfo> getAllOwes()
{
    ArrayList<OweInfo> oweList = new ArrayList<>();

    String selectQuery = " SELECT * FROM " + TABLE_NAME;

    SQLiteDatabase db = this.getWritableDatabase();

    Cursor cursor = db.rawQuery(selectQuery, null);

    //looping through all rows and adding to list
    if(cursor.moveToFirst())
    {
        do
        {
            OweInfo owes = new OweInfo();
            owes.setNameExtra(cursor.getString(0));
            owes.setAmountExtra(cursor.getInt(1));
            oweList.add(owes);
        }

        while (cursor.moveToNext());
    }

    cursor.close();

    return oweList;

}

}

我的自定义课程

public class OweInfo
{
private String nameExtra;
private int amountExtra;

public OweInfo()
{

}

public OweInfo(String name, int amount)
{
    this.nameExtra = name;
    this.amountExtra = amount;
}

public String getNameExtra()
{
    return this.nameExtra;
}

public void setNameExtra(String nameExtra)
{
    this.nameExtra = nameExtra;
}

public int getAmountExtra()
{
    return this.amountExtra;
}

public void setAmountExtra(int amountExtra)
{
    this.amountExtra = amountExtra;
}



 }

2 个答案:

答案 0 :(得分:0)

您已将数据库中的if(window.location.search.indexOf('playdefault=1') > -1){ window.location.href = your_url; } 列定义为amount,但您将其值设为整数,并且为您提供了&#39; 0&#39; 0 。

您必须按如下方式修改获取查询 -

TEXT

答案 1 :(得分:0)

我想出了这个问题。在我的数据库类中,我更改了行

String selectQuery = " SELECT * FROM " + TABLE_NAME; 

                        To

String[] tableColumns = new String[]{"name", "amount"}; 

然后我运行了一个查询

Cursor cursor = db.query("owes", tableColumns, null, null, null, null, null);

以前,我选择了所有列,包括_id列,只要您向表中添加数据,该列就会自动更新。这就是我的所有观点都被编号而另一个观点是0的原因。我在查询所有列,并且我有两个TextViews,我在第一个TextView中显示id,在第二个Text中显示该人的名字那就是为什么值为0.所以我最好指定我想要查询的确切列。