package one.two;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.database.Cursor;
import android.inputmethodservice.Keyboard.Row;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class List_View extends ListActivity {
private ListView lv;
private TextView toptext;
private TextView bottomtext;
DBAdapter db = new DBAdapter(this);
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.list_main);
DBAdapter db = new DBAdapter(this);
getData();
toptext = (TextView) findViewById(R.id.toptext);
bottomtext = (TextView) findViewById(R.id.bottomtext);
}
private void getData() {
db.open();
List<String> items = new ArrayList<String>();
Cursor c = db.getAllEntry();
c.moveToFirst();
toptext.setText("Date: "+c.getString(4));
bottomtext.setText("Title: "+c.getString(1));
ArrayAdapter<String> entries = new ArrayAdapter<String>(this, R.layout.view_list, items);
setListAdapter(entries);
db.close();
}
}
我无法将数据库中的数据放入我的 ListView。
这是我的数据库代码:
package one.two;
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;
public class DBAdapter
{
//values for the login table
public static final String KEY_ROWID = "_id";
public static final String KEY_USER = "user";
public static final String KEY_PASSWORD = "pass";
public static final String KEY_NUMBER ="no";
//Values for the entry table
public static final String KEY_ROWID2 = "_id2";
public static final String KEY_TITLE = "title";
public static final String KEY_ENTRY = "entry";
public static final String KEY_MOOD = "mood";
public static final String KEY_DATE = "date";
public static final String KEY_TIME = "time";
private static final String TAG = "DBAdapter";
//declare Database name, tables names
private static final String DATABASE_NAME = "Database3";
private static final String DATABASE_TABLE = "Login";
private static final String DATABASE_TABLE_2 = "Entry";
private static final int DATABASE_VERSION = 1;
//declares the rules for the database tables
private static final String DATABASE_CREATE =
"create table login (_id integer primary key autoincrement, "
+ "user text not null, pass text not null,"
+ " no integer not null);";
private static final String DATABASE_CREATE_2 =
"create table entry (_id2 integer primary key autoincrement, "
+ "title text,entry text, mood text not null, date text not null, "
+ "time text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
//Create the tables with the rules we set.
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
db.execSQL(DATABASE_CREATE_2);
}
//OnUpgrade is only for use when u changed the database's version to 2 etc.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS titles");
onCreate(db);
}
}
//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//Method for inserting login details, can be used in other java files when DBAdapter is
//declared in the java file. e.g. DBAdapter db = new DBAdapter(this);
public long insertLogin(String user, String pass, String no)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_USER, user);
initialValues.put(KEY_PASSWORD, pass);
initialValues.put(KEY_NUMBER, no);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public long insertEntry(String title, String entry, String mood, String date, String time)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_ENTRY, entry);
initialValues.put(KEY_MOOD, mood);
initialValues.put(KEY_DATE, date);
initialValues.put(KEY_TIME, time);
return db.insert(DATABASE_TABLE_2, null, initialValues);
}
//---deletes a particular title---
public boolean deleteLogin(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID +
"=" + rowId, null) > 0;
}
public boolean deleteEntry(long rowId2)
{
return db.delete(DATABASE_TABLE_2, KEY_ROWID2 +
"=" + rowId2, null) > 0;
}
//method for retrieving all the inputs from database
public Cursor getAllLogin()
{
return db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_USER,
KEY_PASSWORD,
KEY_NUMBER,},
null,
null,
null,
null,
null,
null);
}
public Cursor getAllEntry()
{
return db.query(DATABASE_TABLE_2, new String[] {
KEY_ROWID2,
KEY_TITLE,
KEY_ENTRY,
KEY_MOOD,
KEY_DATE,
KEY_TIME},
null,
null,
null,
null,
null,
null);
}
//---retrieves a particular title---
public Cursor getLogin(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_USER,
KEY_PASSWORD,
KEY_NUMBER},
KEY_ROWID + "=" + rowId,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor getEntry(long rowId2) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE_2, new String[] {
KEY_ROWID2,
KEY_TITLE,
KEY_ENTRY,
KEY_MOOD,
KEY_DATE,
KEY_TIME},
KEY_ROWID2 + "=" + rowId2,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//---updates a title---
public boolean updateLogin(long rowId, String user,
String pass, String no)
{
ContentValues args = new ContentValues();
args.put(KEY_USER, user);
args.put(KEY_PASSWORD, pass);
args.put(KEY_NUMBER, no);
return db.update(DATABASE_TABLE, args,
KEY_ROWID + "=" + rowId, null) > 0;
}
public boolean updateEntry(long rowId,String title, String entry,
String mood, String date, String time)
{
ContentValues args = new ContentValues();
args.put(KEY_TITLE, title);
args.put(KEY_ENTRY, entry);
args.put(KEY_MOOD, mood);
args.put(KEY_DATE, date);
args.put(KEY_TIME, time);
return db.update(DATABASE_TABLE, args,
KEY_ROWID + "=" + rowId, null) > 0;
}
}
无法显示。希望你们能帮助我!
最佳答案
您没有使用检索到的数据填充列表。并且,在这些情况下,您应该使用 SimpleCursorAdapter:
private void getData() {
db.open();
List<String> items = new ArrayList<String>();
Cursor c = db.getAllEntry();
c.moveToFirst();
ListAdapter adapter=new SimpleCursorAdapter(this,
R.layout.view_list, c,
new String[] {"date", "title"},
new int[] {R.id.toptext, R.id.bottomtext});
setListAdapter(adapter);
setListAdapter(entries);
db.close();
}
在这种情况下,我猜你的表有一个名为 date 的列和另一个名为 title 的列。查看此示例以供进一步引用:SimpleCursorAdapters and ListViews
关于android - 从数据库中获取存储的数据到 ListView 中。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3090041/
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我安装了ruby版本管理器,并将RVM安装的ruby实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby。有没有办法让emacs像shell一样尊重ruby的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el
假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit
我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur
如何在Ruby中获取BasicObject实例的类名?例如,假设我有这个:classMyObjectSystem我怎样才能使这段代码成功?编辑:我发现Object的实例方法class被定义为returnrb_class_real(CLASS_OF(obj));。有什么方法可以从Ruby中使用它? 最佳答案 我花了一些时间研究irb并想出了这个:classBasicObjectdefclassklass=class这将为任何从BasicObject继承的对象提供一个#class您可以调用的方法。编辑评论中要求的进一步解释:假设你有对象
是否可以在应用程序中包含的gem代码中知道应用程序的Rails文件系统根目录?这是gem来源的示例:moduleMyGemdefself.included(base)putsRails.root#returnnilendendActionController::Base.send:include,MyGem谢谢,抱歉我的英语不好 最佳答案 我发现解决类似问题的解决方案是使用railtie初始化程序包含我的模块。所以,在你的/lib/mygem/railtie.rbmoduleMyGemclassRailtie使用此代码,您的模块将在