簡體   English   中英

Android為我的Sqlite數據庫中的每個字符串創建一個TextView

[英]Android Create a TextView for every string in my Sqlite DB

我正在嘗試為光標中的每個項目顯示一個新的吐司,我該怎么做? 我已經搜索了SO,但是找不到任何相關的,有用的答案。 這是我的代碼,但是不起作用:

 while(mNotesCursor.moveToNext()){ 
    Toast.makeText(getActivity(),  
    mNotesCursor.getString(mNotesCursor.getColumnIndex("title")),
            Toast.LENGTH_LONG).show();
    }

在游標中進行迭代時敬酒不是最好的主意。 原因如下:

您正在使用LENGTH_LONG ,這意味着烤面包將持續約3秒鍾。 而您的for循環可能會在不到一秒鍾的時間內完成執行。 因此,烤面包將按順序顯示,但它們過渡得太慢,以至於沒有意義。

因此,我建議您在警報對話框或活動本身中顯示內容,以便用戶可以從內容中獲得更多的含義。

編輯:

我假設您正在主線程上執行此操作。

LinearLayout root = (LinearLayout) getActivity().findviewById(R.id.rootLayout);
    while(mNotesCursor.moveToNext()){
        TextView tv = new TextView(getActivity());
        tv.setText(mNotesCursor.getString(mNotesCursor.getColumnIndex("title")));
        root.addView(tv);
    }

如果您想動態添加textview到視圖中,那么這是您可以做到的

<?xml version="1.0" encoding="utf-8"?>
  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<LinearLayout 
    android:id="@+id/lineralayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

</LinearLayout>
</ScrollView>

在你的活動課中

LinearLayout l = (LinearLayout) findViewById(R.id.lineralayout1);
while(mNotesCursor.moveToNext()){ 
    TextView tv = new TextView(this);
    tv.setText(mNotesCursor.getString(mNotesCursor.getColumnIndex("title")));
l.addView(tv);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM