从数据库中显示标题和图像的最佳方法是什么?我只想一次显示一个数据行/记录,然后单击下一个按钮以显示下一个数据行/记录。我无法确定哪种视图控件最好?我看过很多与listview相关的例子。
id |标题|图像
1 Spring image1.blob 2夏天image2.blob 3冬季image3.blob
logcat的
06-07 04:33:06.213: E/Trace(950): error opening trace file: No such file or directory (2)
06-07 04:33:08.041: D/Insert:(950): Inserting ..
06-07 04:33:08.150: D/AndroidRuntime(950): Shutting down VM
06-07 04:33:08.150: W/dalvikvm(950): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-07 04:33:08.171: E/AndroidRuntime(950): FATAL EXCEPTION: main
06-07 04:33:08.171: E/AndroidRuntime(950): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqlitedemoactivity/com.example.sqlitedemoactivity.SQLiteDemoActivity}: java.lang.NullPointerException
06-07 04:33:08.171: E/AndroidRuntime(950): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-07 04:33:08.171: E/AndroidRuntime(950): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-07 04:33:08.171: E/AndroidRuntime(950): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-07 04:33:08.171: E/AndroidRuntime(950): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-07 04:33:08.171: E/AndroidRuntime(950): at android.os.Handler.dispatchMessage(Handler.java:99)
06-07 04:33:08.171: E/AndroidRuntime(950): at android.os.Looper.loop(Looper.java:137)
06-07 04:33:08.171: E/AndroidRuntime(950): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-07 04:33:08.171: E/AndroidRuntime(950): at java.lang.reflect.Method.invokeNative(Native Method)
06-07 04:33:08.171: E/AndroidRuntime(950): at java.lang.reflect.Method.invoke(Method.java:511)
06-07 04:33:08.171: E/AndroidRuntime(950): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-07 04:33:08.171: E/AndroidRuntime(950): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-07 04:33:08.171: E/AndroidRuntime(950): at dalvik.system.NativeStart.main(Native Method)
06-07 04:33:08.171: E/AndroidRuntime(950): Caused by: java.lang.NullPointerException
06-07 04:33:08.171: E/AndroidRuntime(950): at com.example.sqlitedemoactivity.SQLiteDemoActivity.onCreate(SQLiteDemoActivity.java:37)
06-07 04:33:08.171: E/AndroidRuntime(950): at android.app.Activity.performCreate(Activity.java:5104)
06-07 04:33:08.171: E/AndroidRuntime(950): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-07 04:33:08.171: E/AndroidRuntime(950): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-07 04:33:08.171: E/AndroidRuntime(950): ... 11 more
06-07 04:33:15.283: I/Process(950): Sending signal. PID: 950 SIG: 9
我的代码
private void setNewData(int position){
textview1 = (TextView)findViewById(R.id.textview1);
List<Contact> contacts = db.getContact(position) ;
for (Contact cn : contacts) {
String log = "ID:" + cn.getID() + " Name: " + cn.getName();
// Writing Contacts to log
Log.d("Name: ", log);
textview1.setText(cn.getName());
}
}
按钮点击代码
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//increment the counter (which is keeps track of what position we are displaying.
counter = counter+1;
//call method to update the view with the next items position
setNewData(counter);
}
});
答案 0 :(得分:0)
将数据库中的所有项目加载到数组中。然后使用一些简单的代码来跟踪 您当前正在显示的项目。单击该按钮时,更新视图 与下一项文本/图像。下面是一些示例代码(它不会编译,但会给你一个想法)
public class Thing{
public String imageURL;
public String text;
}
private List<Thing> myThings;
private int counter=0;
private Button nextButton;
nextButton.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {
//increment the counter (which is keeps track of what position we are displaying.
counter = counter+1;
//call method to update the view with the next items position
setNewData(counter);
}
});
private void setNewData(int position){
/*
* Get the data from the list of the new position
*/
String image = myThings.get(position).imageURL;
String text = myThings.get(position).text;
myTextView.setText(text);
myImageView.loadImage(image);
}