package com.AndroidDynList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class AndroidDynList extends Activity {
EditText input;
Button add, clear;
ListView listview;
ArrayAdapter<String> MyArrayAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
input = (EditText)findViewById(R.id.input);
add = (Button)findViewById(R.id.add);
clear = (Button)findViewById(R.id.clear);
listview = (ListView)findViewById(R.id.list);
MyArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
listview.setAdapter(MyArrayAdapter);
add.setOnClickListener(addOnClickListener);
clear.setOnClickListener(clearOnClickListener);
}
private Button.OnClickListener addOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String newInput = input.getText().toString();
MyArrayAdapter.add(newInput);
MyArrayAdapter.notifyDataSetChanged();
}};
private Button.OnClickListener clearOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
MyArrayAdapter.clear();
MyArrayAdapter.notifyDataSetChanged();
}};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText
android:id="@+id/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add"
/>
<Button
android:id="@+id/clear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Clear"
/>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
相關文章:
- 動態刪除列表視圖(ListView)的內容
沒有留言:
發佈留言