使用从SharedPreferences键获取的文本将TextView添加到LinearLayout

时间:2013-01-08 22:17:53

标签: android view android-linearlayout

我正在尝试从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正确显示了预期的保存键。

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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函数。