在列表视图中显示string.xml中的数据

时间:2011-08-08 05:49:26

标签: android android-listview

我对Android有点新,并正在进行实验室作业

https://sites.google.com/site/androidcoursearchive/labs/lab-2-1

我尝试了很多,但发现,要在活动中创建list view,我们必须扩展listactivity。但是在这个任务中,我无法扩展listActivity。请查看链接并告诉我代码如何实现此分配。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

朋友我得到了我的问题的答案开始我们可以这样继续这里是我的xml -

            <?xml version="1.0" encoding="utf-8"?>
            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<Button 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" 
    android:id="@+id/addjoke"
    android:text="@string/addJoke"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
></Button>
<EditText 
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:hint="@string/enterJokesHere"
    android:id="@+id/jokebox"
    android:inputType="textMultiLine" 
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/addjoke">
</EditText>

<ListView 
    android:layout_width="fill_parent"
    android:layout_below="@id/jokebox"
    android:id="@+id/jokelist"
    android:layout_height="fill_parent">
</ListView>


    </RelativeLayout>

这是我的string.xml

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


<string name="hello">Hello World, AssignmentsActivity!</string>
<string name="app_name">Assignments</string>
 <string name="addJoke">Add Jokes</string>
<string name="enterJokesHere">Enter the jokes here</string>
<string name="plus">+</string>

<string-array name="jokeListStr">

    <item>Cartoonist found dead in home. Details are sketchy.</item>
    <item>I wondered why the baseball was getting bigger. Then it hit me.</item>
    <item>A small boy swallowed some coins and was taken to a hospital. 
    When his grandmother telephoned to ask how he was a nurse said, 
    No change yet.</item>



</string-array>

这是我的java代码: -

    import java.util.ArrayList;

    import android.app.Activity;
    import android.content.res.Resources;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.BaseAdapter;
    import android.widget.Button;import android.widget.EditText;
    import android.widget.ListView;

    public class SimpleJokeList extends Activity {
ListView lv;
EditText jokeBox;
Button addJoke;
MyAdapter adapter;
private ArrayAdapter<String> mAdapter;
    private ArrayList<String> mStrings = new ArrayList<String>();
    String jokesToBeAdded;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.simplejokeui);

    Resources res = getResources();
    String[] jok = res.getStringArray(R.array.jokeListStr);

    lv=(ListView)findViewById(R.id.jokelist);
    addJoke=(Button)findViewById(R.id.addjoke);//these are also in my XML but 
                                               //not  shown here in XML
    jokeBox=(EditText)findViewById(R.id.jokebox);


    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,   mStrings);
    for(int i=0;i<jok.length;i++){
        lv.setAdapter(mAdapter);
        mAdapter.add(jok[i]);
    }
    addJoke.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            jokesToBeAdded=jokeBox.getText().toString();
            lv.setAdapter(mAdapter);
            mAdapter.add(jokesToBeAdded);
            jokeBox.setText(null);

        }   
    });

}

enter code here