繁体   English   中英

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

[英]Android: How to display string on textview from console

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

数据库处理程序

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

您需要使ListView在其中显示数据库中的数据。因为您有许多从数据库中获取的数据。 我建议使用下一个版本:

在您创建的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);

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

它的答案是“谢谢。您能告诉我如何在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());
    }

或者,如果在“历史记录”字段中有日期尝试,则在简易操作后将进行排序:

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

要在顶部添加最后一项,则需要创建规格。 内部类:

 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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM