0

我正在尝试从 SharedPreferences 对象创建一个键列表。由于用户可以添加新的键值对,我需要动态地进行。

这是我活动的 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,因为 while 内的 Toast 正确地显示了已保存的键,正如预期的那样。

4

2 回答 2

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>
于 2013-01-08T23:11:15.607 回答
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 函数。

于 2013-01-14T13:11:07.010 回答