arraylist和thetab片段中的文字根本没有显示。我不知道什么是错的,你能帮我在列表和tab fragn中显示文本(第二张图片)。请注意,我添加了一个日志,这些值显示在logcat中。
代码: 这是profile.java
public class Profile extends ListActivity {
dbhelper sqlHandler;
ListView lvCustomList;
EditText etName, etPhone;
Button btnsubmit;
SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
sqlHandler = new dbhelper(this);
sqlHandler.openDataBase();
Log.v("on create","profile");
List<Comment> values = sqlHandler.getAllComments();
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
public void onClick(View view) {
@SuppressWarnings("unchecked")
ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();
Comment comment = null;
switch (view.getId()) {
case R.id.add:
String[] comments = new String[] { "Cool", "Very nice", "Hate it" };
int nextInt = new Random().nextInt(3);
// save the new comment to the database
comment = sqlHandler.createComment(comments[nextInt]);
adapter.add(comment);
break;
case R.id.delete:
if (getListAdapter().getCount() > 0) {
comment = (Comment) getListAdapter().getItem(0);
// sqlHandler.deleteComment(comment);
adapter.remove(comment);
}
break;
}
adapter.notifyDataSetChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
dbhelper.java
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class dbhelper extends SQLiteOpenHelper
{
private SQLiteDatabase database;
public final static int db_version = 1;
public final static String db_tab_name ="justtable";
public final static String db_col_name="f_name";
public final static String db_f_phone="f_phone";
public final static String db_col_id ="_id";
public final static String database_name="databasename";
public final static String db_create="Create table " + db_tab_name +" ("+db_col_id +" integer primary key autoincrement ," +db_col_name +" text,"+db_f_phone +" TEXT )";
public dbhelper(Context context) {
super(context, database_name, null, db_version);
}
public void openDataBase() {
try {
database = this.getWritableDatabase();
database.execSQL("DROP TABLE "+ db_tab_name);
database.execSQL(db_create);
Log.d("l", db_create);
}catch (Exception e) {
System.out.println("DATABASE ERROR " + e);
}
}
@Override
public void onCreate(SQLiteDatabase database) {
//database.execSQL("DROP TABLE IF NOT EXISTS "+ db_tab_name);
//database.execSQL(db_create);
}
@Override
public void onUpgrade(SQLiteDatabase database, int arg1, int arg2) {
database.execSQL("DROP TABLE IF NOT EXISTS "+ db_tab_name);
onCreate( database);
}
public boolean insertContact (String name)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put( "name",name);
db.insert("justtable", null, contentValues);
db.close();
return true;
}
public Comment createComment(String comment) {
ContentValues values = new ContentValues();
values.put(db_col_name, comment);
long insertId = database.insert(db_tab_name, null,
values);
Cursor cursor = database.query(db_tab_name,
new String[]{db_col_name,db_f_phone}, db_col_id + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
Comment newComment = cursorToComment(cursor);
cursor.close();
return newComment;
}
public List<Comment> getAllComments() {
List<Comment> comments = new ArrayList<Comment>();
Cursor cursor = database.query(db_tab_name,
new String[]{db_col_name,db_f_phone}, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Comment comment = cursorToComment(cursor);
comments.add(comment);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return comments;
}
private Comment cursorToComment(Cursor cursor) {
Comment comment = new Comment();
comment.setId(cursor.getLong(0));
comment.setComment(cursor.getString(1));
return comment;
}
}
这是代码style.xml
style.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
如果您有任何疑问或需要其他任何问题,请告诉我
答案 0 :(得分:1)
List<Comment> values = sqlHandler.getAllComments();
此列表获取值吗? 你检查过吗?