单击按钮添加ListView项

时间:2012-12-19 22:39:07

标签: android listview button user-input

经过几天的尝试,搜索并再次尝试,我想请求你们帮忙。

我想通过点击带有随机用户输入项的按钮来填充ListView。

这是我的activity_main.xml

的示例图片

http://s1.directupload.net/file/d/3109/x6qnnj5s_png.htm

我尝试从此处和其他网站获取severel代码段,但每次我的虚拟设备都显示应用程序test_1(进程com.example.test_1)意外停止。好心的再试一次。

我目前的代码是:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<ListView
    android:id="@+id/tPlanView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/addTplan" >
</ListView>

<Button
    android:id="@+id/addTplan"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:text="Button" />

list_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText
    android:id="@+id/textIn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />
<TextView 
    android:id="@+id/textOut"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"

    />

MainActivity.java

public class MainActivity extends ListActivity {

/** Items entered by the user is stored in this ArrayList variable */
ArrayList<String> list = new ArrayList<String>();

/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter<String> adapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /** Setting a custom layout for the list activity */
    setContentView(R.layout.activity_main);

    /** Reference to the button of the layout main.xml */
    Button btn = (Button) findViewById(R.id.addTplan);

    /** Defining the ArrayAdapter to set items to ListView */
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);

    /** Setting the event listener for the add button */
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            EditText edit = (EditText) findViewById(R.id.textIn);
            list.add(edit.getText().toString());
            edit.setText("");
            adapter.notifyDataSetChanged();

        }
    });

    /** Setting the adapter to the ListView */
    setListAdapter(adapter);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

我很高兴得到任何帮助。 非常感谢allready: - )

-------更新------

logcat的

    12-19 22:45:02.854: D/AndroidRuntime(330): Shutting down VM
12-19 22:45:02.854: W/dalvikvm(330): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-19 22:45:02.864: E/AndroidRuntime(330): FATAL EXCEPTION: main
12-19 22:45:02.864: E/AndroidRuntime(330): java.lang.RuntimeException: Unable to start activity ComponentInfo{ de.example.test_1test_1/ de.example.test_1.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-19 22:45:02.864: E/AndroidRuntime(330):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-19 22:45:02.864: E/AndroidRuntime(330):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-19 22:45:02.864: E/AndroidRuntime(330):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-19 22:45:02.864: E/AndroidRuntime(330):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-19 22:45:02.864: E/AndroidRuntime(330):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 22:45:02.864: E/AndroidRuntime(330):  at android.os.Looper.loop(Looper.java:123)
12-19 22:45:02.864: E/AndroidRuntime(330):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-19 22:45:02.864: E/AndroidRuntime(330):  at java.lang.reflect.Method.invokeNative(Native Method)
12-19 22:45:02.864: E/AndroidRuntime(330):  at java.lang.reflect.Method.invoke(Method.java:507)
12-19 22:45:02.864: E/AndroidRuntime(330):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-19 22:45:02.864: E/AndroidRuntime(330):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-19 22:45:02.864: E/AndroidRuntime(330):  at dalvik.system.NativeStart.main(Native Method)
12-19 22:45:02.864: E/AndroidRuntime(330): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-19 22:45:02.864: E/AndroidRuntime(330):  at android.app.ListActivity.onContentChanged(ListActivity.java:243)
12-19 22:45:02.864: E/AndroidRuntime(330):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:210)
12-19 22:45:02.864: E/AndroidRuntime(330):  at android.app.Activity.setContentView(Activity.java:1657)
12-19 22:45:02.864: E/AndroidRuntime(330):  at  de.example.test_1.MainActivity.onCreate(MainActivity.java:33)
12-19 22:45:02.864: E/AndroidRuntime(330):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-19 22:45:02.864: E/AndroidRuntime(330):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-19 22:45:02.864: E/AndroidRuntime(330):  ... 11 more
12-19 22:45:05.964: I/Process(330): Sending signal. PID: 330 SIG: 9

2 个答案:

答案 0 :(得分:2)

如果您要使用extends ListActivity,则主要布局中的ListView必须使用此ID进行编写:

<ListView
    android:id="@android:id/list" <---------
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/addTplan" >
</ListView>

至于主要问题调查,也许您会发现以下问题的答案很有用,因为它涵盖了相同的主题。

How to display item after clicked the button in eclipse?

答案 1 :(得分:1)

更改

<ListView
    android:id="@+id/tPlanView"

<ListView
    android:id="@android:id/list"