Android:如何在控制台上显示textview上的字符串

时间:2012-09-12 06:36:44

标签: java android sqlite textview

我正在尝试在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类,我将每行取出。

DatabaseHandler.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上,我可以看到所有数据一个接一个地出现。但是我无法打印所有数据。

3 个答案:

答案 0 :(得分: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代码中: in onCreate:

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

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

答案 1 :(得分:0)

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

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

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

或者,如果在历史记录字段中的日期尝试,则在easy之后将排序:

Map<String, Date> historyMap = new HashMap<String, Date>();
    List<History> history = db.getAllHistory();
        for (History cn : history) {
             historyMap.put(cn.getName, cn.getDate);
        }

答案 2 :(得分: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);
}