0

我正在尝试在 textview 上显示字符串。我成功地能够从数据库在控制台上打印它,但我无法弄清楚如何在不同的不同文本视图上打印所有字符串。这是我的代码:

MainActivity.java

public class MainActivity extends Activity implements OnClickListener {

EditText search;
Button insert;
TextView txt1, txt2, txt3, txt4, txt5;
DatabaseHandler db;
List<History> history;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    db = new DatabaseHandler(this);

    search = (EditText) findViewById(R.id.search_word);
    insert = (Button) findViewById(R.id.insert);
    txt1 = (TextView) findViewById(R.id.txt1);
    txt2 = (TextView) findViewById(R.id.txt2);
    txt3 = (TextView) findViewById(R.id.txt3);
    txt4 = (TextView) findViewById(R.id.txt4);
    txt5 = (TextView) findViewById(R.id.txt5);

    insert.setOnClickListener(this);

    history = db.getAllHistory();
}

public void onClick(View v) {
    db.addHistory(new History(search.getText().toString(), null));
    Toast.makeText(getApplicationContext(),
            "Inserted: " + search.getText().toString(), Toast.LENGTH_LONG)
            .show();
}

@Override
protected void onStart() {
    super.onStart();
    List<History> history = db.getAllHistory();
    for (History cn : history) {
        String log = "Search Strings: " + cn.getName();
        Log.d("Search Strings: ", log);
    }
}
}

这是我的活动,我将我的所有数据库值带到 onStart() 函数上。现在在这里我必须在 textview 上设置来自数据库的所有数据。这是我取出每一行的 DabaseHandler 类。

数据库处理程序.java

public class DatabaseHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_NAME = "historyManager";

private static final String TABLE_HISTORY = "histories";

private static final String KEY_NAME = "history";

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_HISTORY_TABLE = "CREATE TABLE " + TABLE_HISTORY + "("
            + KEY_NAME + " TEXT" + ")";
    db.execSQL(CREATE_HISTORY_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_HISTORY);

    onCreate(db);
}

void addHistory(History history) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, history.getName());

    db.insert(TABLE_HISTORY, null, values);
    db.close();
}

History getHistory(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_HISTORY, new String[] { KEY_NAME },
            "=?", new String[] { String.valueOf(id) }, null, null, null,
            null);
    if (cursor != null)
        cursor.moveToFirst();

    History history = new History(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1), cursor.getString(2));
    return history;
}

public List<History> getAllHistory() {
    List<History> historyList = new ArrayList<History>();

    String selectQuery = "SELECT  * FROM " + TABLE_HISTORY;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            History contact = new History();
            contact.setName(cursor.getString(0));
            historyList.add(contact);
        } while (cursor.moveToNext());
    }

    return historyList;
}

public int updateHistory(History history) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, history.getName());
    return db.update(TABLE_HISTORY, values, KEY_NAME + " = ?",
            new String[] { String.valueOf(history.getName()) });
}

public void deleteHistory(History history) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_HISTORY, KEY_NAME + " = ?",
            new String[] { String.valueOf(history.getName()) });
    db.close();
}

public int getHistoryCount() {
    String countQuery = "SELECT  * FROM " + TABLE_HISTORY;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    return cursor.getCount();
}
}

请帮助在 textview 上打印数据。在 Log.d 上,我可以看到我所有的数据,一个接一个。但我无法打印所有数据。

4

3 回答 3

0

您需要制作 ListView,其中将显示来自 DB 的数据。因为您有许多从 db 获取的项目数据。我建议下一个版本:

在您创建的 xml 文件中:

<ListView
android:id="@+id/list_names"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
...>

您需要使用下一个 xml 资源为您的列表创建适配器:

<TextView
android:id="@+id/text_name"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

在 java 代码中:在 onCreate 中:

ListView list = (ListView) findViewById(R.id.list);
OurAdapter adapter = new OurAdapter(..., List<String> yourListWithName);
list.setAdapter(adapter);

如果您想要更详细的代码描述,请告诉我。

于 2012-09-12T06:52:51.080 回答
0

这是“谢谢你”的答案。你能告诉我如何设置我在 MainActivity onStart() 方法 (Log.d("")) 上打印的数据。如果你能给我代码,那对我来说会容易得多。” 试试这个:

List<String> listNames = new ArrayList<String>();//global variable

List<History> history = db.getAllHistory();
    for (History cn : history) {
         listNames.add(cn.getName());
    }

或者,如果在历史字段中尝试日期,则在轻​​松排序后:

Map<String, Date> historyMap = new HashMap<String, Date>();
    List<History> history = db.getAllHistory();
        for (History cn : history) {
             historyMap.put(cn.getName, cn.getDate);
        }
于 2012-09-12T07:58:26.363 回答
0

要在顶部添加最后一项,您接下来需要创建规范。内部类:

 class Holder implements Comparable<Holder> {
        String key;
        Double value;

        public int compareTo(Holder another) {           
              return another.value.compareTo(value);
        }
    }

并如何使用他:

List<this.Holder> listSortforLastInTop = new ArrayList<this.Holder>();

for(...){
   Holder holder = new Holder();
   holder.key=...;
   older.value=..;
   listSortforLastInTop.add(holder);
}
于 2012-09-12T08:17:57.097 回答