**编辑:我将使用listview而不是Gridview。 **
我已经学习了Android几个星期和最后几天我一直在努力在网格视图中获取我的SQL数据。我一直在搜索许多主题,但我似乎无法找到适合我的问题的答案。这是我到目前为止的代码:
package com.example.myfirstapp;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class DisplayMessageActivity extends AppCompatActivity {
public static ArrayList<String> ArrayofName = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
SQLiteDatabase db = mDbHelper.getReadableDatabase();
String selectQuery = "SELECT * FROM " + MainActivity.FeedReaderContract.FeedEntry.TABLE_NAME;
Cursor cursor = db.rawQuery(selectQuery, null);
{
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
String id = cursor.getString(cursor.getColumnIndex("_ID"));
String title = cursor.getString(cursor.getColumnIndex("title"));
String subtitle = cursor.getString(cursor.getColumnIndex("subtitle"));
String name = id +"\n" + title +"\n"+ subtitle;
DisplayMessageActivity.ArrayofName.add(name);
} while (cursor.moveToNext());
}
}
GridView gridView = (GridView) findViewById(R.id.gridView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, ArrayofName);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) v).getText(), Toast.LENGTH_SHORT).show();
}
});
}
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ",";
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + MainActivity.FeedReaderContract.FeedEntry.TABLE_NAME + " (" +
MainActivity.FeedReaderContract.FeedEntry._ID + " INTEGER PRIMARY KEY," +
MainActivity.FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE + TEXT_TYPE + COMMA_SEP +
MainActivity.FeedReaderContract.FeedEntry.COLUMN_NAME_SUBTITLE + TEXT_TYPE + " )";
private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS '" + MainActivity.FeedReaderContract.FeedEntry.TABLE_NAME + "'";
public class FeedReaderDbHelper extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "FeedReader.db";
public FeedReaderDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
}
DisplayMessageActivity.FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(this);
public void Test (View view) {
SQLiteDatabase db = mDbHelper.getReadableDatabase();
// Define a projection that specifies which columns from the database you will actually use after this query.
String[] projection = {MainActivity.FeedReaderContract.FeedEntry._ID, MainActivity.FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, MainActivity.FeedReaderContract.FeedEntry.COLUMN_NAME_SUBTITLE};
// Filter results WHERE "title" = 'My Title'
String selection = MainActivity.FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE + " = ?";
String[] selectionArgs = {"A"};
// How you want the results sorted in the resulting Cursor
String sortOrder = MainActivity.FeedReaderContract.FeedEntry.COLUMN_NAME_SUBTITLE + " DESC";
Cursor c = db.query(
MainActivity.FeedReaderContract.FeedEntry.TABLE_NAME, // The table to query
projection, // The columns to return
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
if (c.getCount()>0) {
c.moveToFirst();
int itemId = c.getInt(c.getColumnIndexOrThrow(MainActivity.FeedReaderContract.FeedEntry._ID));
String strTitel = c.getString(c.getColumnIndexOrThrow(MainActivity.FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE));
String strSubtitel = c.getString(c.getColumnIndexOrThrow(MainActivity.FeedReaderContract.FeedEntry.COLUMN_NAME_SUBTITLE));
EditText edtID = (EditText) findViewById(R.id.txtInvoer);
edtID.setText(Integer.toString(itemId));
EditText edtTitle = (EditText) findViewById(R.id.txtTitel2);
edtTitle.setText(strTitel);
EditText edtSubtitle = (EditText) findViewById(R.id.txtSubtitel2);
edtSubtitle.setText(strSubtitel);
} else {
Toast.makeText(getApplicationContext(), "Geen data in db", Toast.LENGTH_LONG).show();
}
}
public void Vorige (View view) {
SQLiteDatabase db = mDbHelper.getReadableDatabase();
String[] projection = {MainActivity.FeedReaderContract.FeedEntry._ID, MainActivity.FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, MainActivity.FeedReaderContract.FeedEntry.COLUMN_NAME_SUBTITLE};
String selection = "_ID=?";
EditText edtGeselecteerdeID = (EditText) findViewById(R.id.txtInvoer);
int intVolgendeID=Integer.parseInt(edtGeselecteerdeID.getText().toString())-1;
String strVolgendeID=Integer.toString(intVolgendeID);
String[] selectionArgs = {strVolgendeID};
// How you want the results sorted in the resulting Cursor
String sortOrder = MainActivity.FeedReaderContract.FeedEntry.COLUMN_NAME_SUBTITLE + " DESC";
Cursor c = db.query(
MainActivity.FeedReaderContract.FeedEntry.TABLE_NAME, // The table to query
projection, // The columns to return
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
if (c.getCount()>0) {
c.moveToFirst();
int itemId = c.getInt(c.getColumnIndexOrThrow(MainActivity.FeedReaderContract.FeedEntry._ID));
String strTitel = c.getString(c.getColumnIndexOrThrow(MainActivity.FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE));
String strSubtitel = c.getString(c.getColumnIndexOrThrow(MainActivity.FeedReaderContract.FeedEntry.COLUMN_NAME_SUBTITLE));
EditText edtID = (EditText) findViewById(R.id.txtInvoer);
edtID.setText(Integer.toString(itemId));
EditText edtTitle = (EditText) findViewById(R.id.txtTitel2);
edtTitle.setText(strTitel);
EditText edtSubtitle = (EditText) findViewById(R.id.txtSubtitel2);
edtSubtitle.setText(strSubtitel);
} else {
Toast.makeText(getApplicationContext(), "Geen data in db", Toast.LENGTH_LONG).show();
}
}
public void Volgende (View view) {
SQLiteDatabase db = mDbHelper.getReadableDatabase();
String[] projection = {MainActivity.FeedReaderContract.FeedEntry._ID, MainActivity.FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, MainActivity.FeedReaderContract.FeedEntry.COLUMN_NAME_SUBTITLE};
String selection = MainActivity.FeedReaderContract.FeedEntry._ID + " = ?";
EditText edtGeselecteerdeID = (EditText) findViewById(R.id.txtInvoer);
int intVolgendeID=Integer.parseInt(edtGeselecteerdeID.getText().toString())+1;
String strVolgendeID=Integer.toString(intVolgendeID);
String[] selectionArgs = {strVolgendeID};
// How you want the results sorted in the resulting Cursor
String sortOrder = MainActivity.FeedReaderContract.FeedEntry.COLUMN_NAME_SUBTITLE + " DESC";
Cursor c = db.query(
MainActivity.FeedReaderContract.FeedEntry.TABLE_NAME, // The table to query
projection, // The columns to return
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
if (c.getCount()>0) {
c.moveToFirst();
int itemId = c.getInt(c.getColumnIndexOrThrow(MainActivity.FeedReaderContract.FeedEntry._ID));
String strTitel = c.getString(c.getColumnIndexOrThrow(MainActivity.FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE));
String strSubtitel = c.getString(c.getColumnIndexOrThrow(MainActivity.FeedReaderContract.FeedEntry.COLUMN_NAME_SUBTITLE));
EditText edtID = (EditText) findViewById(R.id.txtInvoer);
edtID.setText(Integer.toString(itemId));
EditText edtTitle = (EditText) findViewById(R.id.txtTitel2);
edtTitle.setText(strTitel);
EditText edtSubtitle = (EditText) findViewById(R.id.txtSubtitel2);
edtSubtitle.setText(strSubtitel);
} else {
Toast.makeText(getApplicationContext(), "Geen data in db", Toast.LENGTH_LONG).show();
}
}
}
因此,在MainActivity中,我已经获得了将数据添加到数据库中的代码。 &#39; DisplayMessageActivity&#39;用于测试新的东西。我的数据库由3列组成:ID,标题和副标题。我使用方法&#39;测试&#39;在Edittext对象中显示数据,以及方法&#39; Vorige&#39;和&#39; Volgende&#39;浏览数据。现在我试图在gridview或listview中显示数据(id,title&amp; subtitle)(我真的不知道该使用什么)。您可以在第一个onCreate类中找到我的问题的所有代码。
我想要达到这样的目标:
因此第一行显示不同的列标题,所有其他行显示数据。 显示标题的第一行甚至不需要,我可以使用EditText字段轻松添加它们。
任何人都可以帮助我吗?我们也欢迎其他改进我的代码的建议;)