2013-01-08 35 views
0

我試圖從SharedPreferences對象創建密鑰列表。由於用戶可以添加新的鍵值對,所以我需要動態地執行它。將TextView添加到採用SharedPreferences密鑰的文本的LinearLayout中

這裏是我活動的onCreate功能:

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     LayoutInflater inflater = this.getLayoutInflater(); 
     LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.activity_show_saved, null); 

     this.add_here = (LinearLayout)layout.findViewById(R.id.saved_list); 

     // Loading shared preferences 
     SharedPreferences sp = this.getSharedPreferences("saved_sequences", MODE_PRIVATE); 
     Map<String, ?> kv = sp.getAll(); 

     int i = 0; 
     // Iterating through shared preferences 
     for(Map.Entry<String, ?> entry : kv.entrySet()) { 
      Toast.makeText(this, entry.getKey(), Toast.LENGTH_SHORT).show(); 
      TextView to_add = new TextView(this); 
      to_add.setText(entry.getKey()); 
      to_add.setId(i++); 
      this.add_here.addView(to_add, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
     } 

     this.setContentView(layout); 
    } 

哪裏this.add_here聲明爲private LinearLayout類變量。這裏是正在充氣的XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@color/background" 
    tools:context=".ShowSaved" > 

    <com.google.ads.AdView 
     android:id="@+id/adViewTop" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     ads:adUnitId="*****************" 
     ads:adSize="SMART_BANNER" 
     ads:testDevices="TEST_EMULATOR" 
     ads:loadAdOnCreate="true" /> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <LinearLayout 
      android:id="@+id/saved_list" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

     </LinearLayout> 

    </ScrollView> 


</LinearLayout> 

不幸的是我得到一個空白屏幕。問題在於添加TextViews,因爲Toast內部正確地顯示了保存的鍵,因爲它預期會做。

+0

問:爲什麼不使用ListView? – Eric

+0

您要導入哪些LayoutParams類? – Sam

+1

@Sam,我正在導入'android.widget.LinearLayout.LayoutParams'。我會給ListView一個嘗試,但我想明白爲什麼這不起作用。 – Zagorax

回答

0

正如有人已經在評論中已經指出,這不是做這件事的正確方法。這是使用ListView實現的結果。看一看。

public class ShowSaved extends Activity implements OnItemClickListener { 

    // Layout 
    private ListView add_here; 
    private TextView empty_view; 

    // String Array 
    private ArrayList<String> string = new ArrayList<String>(); 

    // SharedPreferences object and editor 
    private SharedPreferences sp; 

    // Array adapter for ListView add_here 
    private ArrayAdapter<String> saved; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     this.setContentView(R.layout.activity_show_saved); 

     this.add_here = (ListView)findViewById(R.id.saved_list); 
     this.empty_view = (TextView)findViewById(R.id.empty_text); 

     // Loading shared preferences and editor 
     this.sp = this.getSharedPreferences("saved_sequences", MODE_PRIVATE); 

     // Creating the Adapter with all strings in an array 
     this.saved = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, string); 
     this.saved.setNotifyOnChange(true); 

     // Retrieving all pairs in SharedPreferences sp 
     Map<String, ?> kv = sp.getAll(); 

     // Iterating through shared preferences 
     for(Map.Entry<String, ?> entry : kv.entrySet()) {  
      saved.add(entry.getKey()); 
     } 

     // Setting Adapter to ListView and empty View 
     this.add_here.setEmptyView(empty_view); 
     this.add_here.setAdapter(this.saved); 

     // Setting Listener for ListView add_here 
     this.add_here.setOnItemClickListener(this); 
    } 

注:此代碼不會對自己編譯,因爲它缺乏對OnItemClickListener接口所需的onItemClick功能。

0

我剛剛做了類似的動態列表,但我的TextView是獨立佈局的一部分。 也許這會爲你做的伎倆。

此代碼的工作對我來說:

 final ScrollView ll = (ScrollView) getLayoutInflater().inflate(
       R.layout.dropdown_list, null); 

     final LinearLayout container = ((LinearLayout) ll 
       .findViewById(R.id.dropdown_list_container)); 

     if (myObjectsCount > 0) { 

      for (final MyObject ci : myObjects()) { 

       LinearLayout item = ((LinearLayout) getLayoutInflater() 
         .inflate(R.layout.list_item, null)); 

       TextView tv = ((TextView) item 
         .findViewById(R.id.list_item_text)); 
       tv.setText("your text here"); 


       container.addView(item); 

      } 
     } 

和文本視圖中的佈局:

<?xml version="1.0" encoding="utf-8"?> 


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/list_item" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@drawable/buttons" 
android:clickable="true" 
android:gravity="center_vertical" 
android:orientation="horizontal" > 

<TextView 
    android:id="@+id/list_item_text" 
    style="@android:style/Widget.TextView" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:gravity="center_vertical" /> 

</LinearLayout>