我m beginner at Java coding and would like to use ad advice. Sorry for basic question, butu I couldn
找到我要找的东西。
我想阅读SqLite数据库并在mainactivity中显示其数据。 下面是它应该看起来的一个例子(例如,有两行显示 - 1和2)。
问题是我应该使用哪个方法/类来创建框架(示例中为1和2),这些框架将根据SqLite中的行数自动更新。 这意味着如果SqLite有5行,我希望有5帧可以显示数据。
提前致谢。
答案 0 :(得分:1)
您必须在自定义适配器中使用android ListView
或RecyclerView
,或者可以使用ListView
的默认适配器。
详细了解ListView
和RecyclerView
。
https://developer.android.com/guide/topics/ui/layout/listview.html
https://developer.android.com/training/material/lists-cards.html
答案 1 :(得分:0)
您可以使用GridView或RecyclerView并使用适配器设置数据。
这样当你有5行时。它将数据设置为5行。此外,GridView或Recycler视图将为您提供平滑的滚动效果。
答案 2 :(得分:0)
这里分享一个例子。 DBManager是一个可用于添加,删除,更新行的接口。 MainActivity显示了它的用法。
package in.apachetechnology.apachedb;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
public class DBManager
{
// the Activity or Application that is creating an object from this class.
Context m_oContext;
// a reference to the database used by this application/object
private SQLiteDatabase m_sqlDB;
// These constants are specific to the database. They should be
// changed to suit your needs.
private final String cDB_NAME = "database_name";
private final int cDB_VERSION = 1;
// These constants are specific to the database table. They should be
// changed to suit your needs.
private final String cTABLE_NAME = "database_table";
private final String cROW_NAME_ID = "id";
private final String cROW_NAME_ONE = "table_row_one";
private final String cROW_NAME_TWO = "table_row_two";
public DBManager(Context c)
{
m_oContext = c;
// create or open the database
CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(c);
m_sqlDB = helper.getWritableDatabase();
}
/**********************************************************************
* ADDING A ROW TO THE DATABASE TABLE
*
* This is an example of how to add a row to a database table
* using this class. You should edit this method to suit your
* needs.
*
* the key is automatically assigned by the database
* @param rowStringOne the value for the row's first column
* @param rowStringTwo the value for the row's second column
*/
public void addRow(String rowStringOne, String rowStringTwo)
{
// this is a key value pair holder used by android's SQLite functions
ContentValues values = new ContentValues();
values.put(cROW_NAME_ONE, rowStringOne);
values.put(cROW_NAME_TWO, rowStringTwo);
// ask the database object to insert the new data
try{
m_sqlDB.insert(cTABLE_NAME, null, values);}
catch(Exception e) {
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
/**********************************************************************
* DELETING A ROW FROM THE DATABASE TABLE
*
* This is an example of how to delete a row from a database table
* using this class. In most cases, this method probably does
* not need to be rewritten.
*
* @param rowID the SQLite database identifier for the row to delete.
*/
public void deleteRow(long rowID)
{
// ask the database manager to delete the row of given id
try {
m_sqlDB.delete(cTABLE_NAME, cROW_NAME_ID + "=" + rowID, null);}
catch (Exception e) {
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
/**********************************************************************
* UPDATING A ROW IN THE DATABASE TABLE
*
* This is an example of how to update a row in the database table
* using this class. You should edit this method to suit your needs.
*
* @param rowID the SQLite database identifier for the row to update.
* @param rowStringOne the new value for the row's first column
* @param rowStringTwo the new value for the row's second column
*/
public void updateRow(long rowID, String rowStringOne, String rowStringTwo)
{
// this is a key value pair holder used by android's SQLite functions
ContentValues values = new ContentValues();
values.put(cROW_NAME_ONE, rowStringOne);
values.put(cROW_NAME_TWO, rowStringTwo);
// ask the database object to update the database row of given rowID
try {
m_sqlDB.update(cTABLE_NAME, values, cROW_NAME_ID + "=" + rowID, null);}
catch (Exception e) {
Log.e("DB Error", e.toString());
e.printStackTrace();
}
}
/**********************************************************************
* RETRIEVING A ROW FROM THE DATABASE TABLE
*
* This is an example of how to retrieve a row from a database table
* using this class. You should edit this method to suit your needs.
*
* @param rowID the id of the row to retrieve
* @return an array containing the data from the row
*/
public ArrayList<Object> getRowAsArray(long rowID)
{
// create an array list to store data from the database row.
// I would recommend creating a JavaBean compliant object
// to store this data instead. That way you can ensure
// data types are correct.
ArrayList<Object> rowArray = new ArrayList<Object>();
Cursor cursor;
try
{
// this is a database call that creates a "cursor" object.
// the cursor object store the information collected from the
// database and is used to iterate through the data.
cursor = m_sqlDB.query(
cTABLE_NAME,
new String[] { cROW_NAME_ID, cROW_NAME_ONE, cROW_NAME_TWO },
cROW_NAME_ID + "=" + rowID,
null, null, null, null, null
);
// move the pointer to position zero in the cursor.
cursor.moveToFirst();
// if there is data available after the cursor's pointer, add
// it to the ArrayList that will be returned by the method.
if (!cursor.isAfterLast()) {
do {
rowArray.add(cursor.getLong(0));
rowArray.add(cursor.getString(1));
rowArray.add(cursor.getString(2));
}
while (cursor.moveToNext());
}
// let java know that you are through with the cursor.
cursor.close();
}
catch (SQLException e) {
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
// return the ArrayList containing the given row from the database.
return rowArray;
}
/**********************************************************************
* RETRIEVING ALL ROWS FROM THE DATABASE TABLE
*
* This is an example of how to retrieve all data from a database
* table using this class. You should edit this method to suit your
* needs.
*
* the key is automatically assigned by the database
*/
public ArrayList<ArrayList<Object>> getAllRowsAsArrays()
{
// create an ArrayList that will hold all of the data collected from
// the database.
ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
// this is a database call that creates a "cursor" object.
// the cursor object store the information collected from the
// database and is used to iterate through the data.
Cursor cursor;
try {
// ask the database object to create the cursor.
cursor = m_sqlDB.query(
cTABLE_NAME,
new String[]{cROW_NAME_ID, cROW_NAME_ONE, cROW_NAME_TWO},
null, null, null, null, null
);
// move the cursor's pointer to position zero.
cursor.moveToFirst();
// if there is data after the current cursor position, add it
// to the ArrayList.
if (!cursor.isAfterLast()) {
do {
ArrayList<Object> dataList = new ArrayList<Object>();
dataList.add(cursor.getLong(0));
dataList.add(cursor.getString(1));
dataList.add(cursor.getString(2));
dataArrays.add(dataList);
}
// move the cursor's pointer up one position.
while (cursor.moveToNext());
}
} catch (SQLException e) {
Log.e("DB Error", e.toString());
e.printStackTrace();
}
// return the ArrayList that holds the data collected from
// the database.
return dataArrays;
}
/**
* This class is designed to check if there is a database that currently
* exists for the given program. If the database does not exist, it creates
* one. After the class ensures that the database exists, this class
* will open the database for use. Most of this functionality will be
* handled by the SQLiteOpenHelper parent class. The purpose of extending
* this class is to tell the class how to create (or update) the database.
*/
private class CustomSQLiteOpenHelper extends SQLiteOpenHelper
{
public CustomSQLiteOpenHelper(Context m_oContext)
{
super(m_oContext, cDB_NAME, null, cDB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase m_sqlDB)
{
// This string is used to create the database. It should
// be changed to suit your needs.
String newTableQueryString = "create table " +
cTABLE_NAME +
" (" +
cROW_NAME_ID + " integer primary key autoincrement not null," +
cROW_NAME_ONE + " text," +
cROW_NAME_TWO + " text" +
");";
// execute the query string to the database.
m_sqlDB.execSQL(newTableQueryString);
}
@Override
public void onUpgrade(SQLiteDatabase m_sqlDB, int oldVersion, int newVersion)
{
// NOTHING TO DO HERE. THIS IS THE ORIGINAL DATABASE VERSION.
// OTHERWISE, YOU WOULD SPECIFIY HOW TO UPGRADE THE DATABASE.
}
}
}
MainActivity
public class MainActivity extends Activity {
// the text fields that users input new data into
EditText textFieldOne, textFieldTwo,
idField,
updateIDField, updateTextFieldOne, updateTextFieldTwo;
// the buttons that listen for the user to select an action
Button addButton, deleteButton, retrieveButton, updateButton;
// the table that displays the data
TableLayout dataTable;
// the class that opens or creates the database and makes sql calls to it
DBManager db;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
// this try catch block returns better error reporting to the log
try {
// Android specific calls
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create the database manager object
db = new DBManager(this);
// create references and listeners for the GUI interface
setupViews();
// make the buttons clicks perform actions
addButtonListeners();
// load the data table
updateTable();
} catch (Exception e) {
Log.e("ERROR", e.toString());
e.printStackTrace();
}
}
/**
* creates references and listeners for the GUI interface
*/
private void setupViews() {
// THE DATA TABLE
dataTable = (TableLayout) findViewById(R.id.data_table);
// THE DATA FORM FIELDS
textFieldOne = (EditText) findViewById(R.id.text_field_one);
textFieldTwo = (EditText) findViewById(R.id.text_field_two);
idField = (EditText) findViewById(R.id.id_field);
updateIDField = (EditText) findViewById(R.id.update_id_field);
updateTextFieldOne = (EditText) findViewById(R.id.update_text_field_one);
updateTextFieldTwo = (EditText) findViewById(R.id.update_text_field_two);
// THE BUTTONS
addButton = (Button) findViewById(R.id.add_button);
deleteButton = (Button) findViewById(R.id.delete_button);
retrieveButton = (Button) findViewById(R.id.retrieve_button);
updateButton = (Button) findViewById(R.id.update_button);
}
/**
* adds listeners to each of the buttons and sets them to call relevant methods
*/
private void addButtonListeners() {
addButton.setOnClickListener
(
new View.OnClickListener() {
@Override
public void onClick(View v) {
addRow();
}
}
);
deleteButton.setOnClickListener
(
new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteRow();
}
}
);
updateButton.setOnClickListener
(
new View.OnClickListener() {
@Override
public void onClick(View v) {
updateRow();
}
}
);
retrieveButton.setOnClickListener
(
new View.OnClickListener() {
@Override
public void onClick(View v) {
retrieveRow();
}
}
);
}
/**
* adds a row to the database based on information contained in the
* add row fields.
*/
private void addRow() {
try {
// ask the database manager to add a row given the two strings
db.addRow
(
textFieldOne.getText().toString(),
textFieldTwo.getText().toString()
);
// request the table be updated
updateTable();
// remove all user input from the Activity
emptyFormFields();
} catch (Exception e) {
Log.e("Add Error", e.toString());
e.printStackTrace();
}
}
/**
* deletes a row from the database with the id number in the corresponding
* user entry field
*/
private void deleteRow() {
try {
// ask the database manager to delete the row with the give rowID.
db.deleteRow(Long.parseLong(idField.getText().toString()));
// request the table be updated
updateTable();
// remove all user input from the Activity
emptyFormFields();
} catch (Exception e) {
Log.e("Delete Error", e.toString());
e.printStackTrace();
}
}
/**
* retrieves a row from the database with the id number in the corresponding
* user entry field
*/
private void retrieveRow() {
try {
// The ArrayList that holds the row data
ArrayList<Object> row;
// ask the database manager to retrieve the row with the given rowID
row = db.getRowAsArray(Long.parseLong(updateIDField.getText().toString()));
// update the form fields to hold the retrieved data
updateTextFieldOne.setText((String) row.get(1));
updateTextFieldTwo.setText((String) row.get(2));
} catch (Exception e) {
Log.e("Retrieve Error", e.toString());
e.printStackTrace();
}
}
/**
* updates a row with the given information in the corresponding user entry
* fields
*/
private void updateRow() {
try {
// ask the database manager to update the row based on the information
// found in the corresponding user entry fields
db.updateRow
(
Long.parseLong(updateIDField.getText().toString()),
updateTextFieldOne.getText().toString(),
updateTextFieldTwo.getText().toString()
);
// request the table be updated
updateTable();
// remove all user input from the Activity
emptyFormFields();
} catch (Exception e) {
Log.e("Update Error", e.toString());
e.printStackTrace();
}
}
/**
* helper method to empty all the fields in all the forms.
*/
private void emptyFormFields() {
textFieldOne.setText("");
textFieldTwo.setText("");
idField.setText("");
updateIDField.setText("");
updateTextFieldOne.setText("");
updateTextFieldTwo.setText("");
}
/**
* updates the table from the database.
*/
private void updateTable() {
// delete all but the first row. remember that the count
// starts at one and the index starts at zero
while (dataTable.getChildCount() > 1) {
// while there are at least two rows in the table widget, delete
// the second row.
dataTable.removeViewAt(1);
}
// collect the current row information from the database and
// store it in a two dimensional ArrayList
ArrayList<ArrayList<Object>> data = db.getAllRowsAsArrays();
// iterate the ArrayList, create new rows each time and add them
// to the table widget.
for (int position = 0; position < data.size(); position++) {
TableRow tableRow = new TableRow(this);
ArrayList<Object> row = data.get(position);
TextView idText = new TextView(this);
idText.setText(row.get(0).toString());
tableRow.addView(idText);
TextView textOne = new TextView(this);
textOne.setText(row.get(1).toString());
tableRow.addView(textOne);
TextView textTwo = new TextView(this);
textTwo.setText(row.get(2).toString());
tableRow.addView(textTwo);
dataTable.addView(tableRow);
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:background="#736f6e"
>
<!-- ADD A DATA ENTRY FORM -->
<TextView
android:text="@string/add_directions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/text_field_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="100px"
/>
<EditText
android:id="@+id/text_field_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="100px"
/>
<Button
android:id="@+id/add_button"
android:text="@string/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="100px"
/>
</LinearLayout>
<!-- DELETE A DATA ENTRY FORM -->
<TextView
android:text="@string/delete_directions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/id_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="100px"
/>
<Button
android:id="@+id/delete_button"
android:text="@string/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<!-- UPDATE A DATA ENTRY FORM -->
<TextView
android:text="@string/update_directions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/update_id_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="45px"
/>
<Button
android:id="@+id/retrieve_button"
android:text="@string/retrieve"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/update_text_field_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="70px"
/>
<EditText
android:id="@+id/update_text_field_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="70px"
/>
<Button
android:id="@+id/update_button"
android:text="@string/update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<ScrollView
android:id="@+id/tbl_scrollbar"
android:scrollbars="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbarStyle="insideOverlay"
android:scrollbarSize="12px"
>
<!-- THE DATA TABLE -->
<TableLayout
android:id="@+id/data_table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stretchColumns="*"
>
<TableRow>
<TextView
android:text="@string/th_id"
android:minWidth="50px"
/>
<TextView
android:text="@string/th_text_one"
android:minWidth="125px"
/>
<TextView
android:text="@string/th_text_two"
android:minWidth="125px"
/>
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>
MyTask.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:background="#736f6e"
>
<!-- ADD A DATA ENTRY FORM -->
<TextView
android:text="@string/add_directions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/text_field_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="100px"
/>
<EditText
android:id="@+id/text_field_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="100px"
/>
<Button
android:id="@+id/add_button"
android:text="@string/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="100px"
/>
</LinearLayout>
<!-- DELETE A DATA ENTRY FORM -->
<TextView
android:text="@string/delete_directions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/id_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="100px"
/>
<Button
android:id="@+id/delete_button"
android:text="@string/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<!-- UPDATE A DATA ENTRY FORM -->
<TextView
android:text="@string/update_directions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/update_id_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="45px"
/>
<Button
android:id="@+id/retrieve_button"
android:text="@string/retrieve"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/update_text_field_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="70px"
/>
<EditText
android:id="@+id/update_text_field_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="70px"
/>
<Button
android:id="@+id/update_button"
android:text="@string/update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<ScrollView
android:id="@+id/tbl_scrollbar"
android:scrollbars="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbarStyle="insideOverlay"
android:scrollbarSize="12px"
>
<!-- THE DATA TABLE -->
<TableLayout
android:id="@+id/data_table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stretchColumns="*"
>
<TableRow>
<TextView
android:text="@string/th_id"
android:minWidth="50px"
/>
<TextView
android:text="@string/th_text_one"
android:minWidth="125px"
/>
<TextView
android:text="@string/th_text_two"
android:minWidth="125px"
/>
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>