安卓开发中动态添加lsitView 代码实例【转】
2013-05-01 21:30
399 查看
这个很简单 就是为了说明别忘了adapter.notifyDataSetChanged();
唤醒一下,其实这个唤醒也就是在button下用 或者在别的控件下调用 如果是在getView下就不必了
java代码:
public class DynamicListItems extends ListActivity {
private static final String ITEM_KEY = "key";
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
private SimpleAdapter adapter;
private EditText newValue;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dynamic_list);
newValue = (EditText) findViewById(R.id.new_value_field);
setListAdapter(new SimpleAdapter(this, list, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.list_value }));
((ImageButton) findViewById(R.id.button)).setOnClickListener(getBtnClickListener());
}
private OnClickListener getBtnClickListener() {
return new OnClickListener() {
public void onClick(View view) {
try {
HashMap<String, String> item = new HashMap<String, String>();
item.put(ITEM_KEY, newValue.getText().toString());
list.add(item);
adapter.notifyDataSetChanged();
} catch (NullPointerException e) {
Log.i("[Dynamic Items]", "Tried to add null value");
}
}
};
}
}
Java代码:
<?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"
>
<ListView android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1"
android:cacheColorHint="#00000000">
</ListView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6px"
android:layout_marginBottom="8px"
android:layout_marginLeft="8px"
android:layout_marginRight="8px"
android:background="#00000000">
<ImageButton
android:id="@+id/button"
android:layout_width="100px"
android:layout_height="100px"
android:src="@android:drawable/ic_menu_add" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="230px"
android:layout_height="wrap_content"
android:layout_marginTop="8px"
android:layout_marginBottom="8px"
android:layout_marginLeft="0px"
android:layout_marginRight="28px"
android:background="#cccccc00" >
<EditText android:id="@+id/new_value_field"
android:textColor="#020905"
android:textSize="18sp"
android:layout_marginTop="6px"
android:layout_marginBottom="8px"
android:layout_marginLeft="12px"
android:layout_marginRight="12px"
android:layout_width="210px"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
ROw:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00000000">
<TextView
android:id="@+id/list_value"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
唤醒一下,其实这个唤醒也就是在button下用 或者在别的控件下调用 如果是在getView下就不必了
java代码:
public class DynamicListItems extends ListActivity {
private static final String ITEM_KEY = "key";
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
private SimpleAdapter adapter;
private EditText newValue;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dynamic_list);
newValue = (EditText) findViewById(R.id.new_value_field);
setListAdapter(new SimpleAdapter(this, list, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.list_value }));
((ImageButton) findViewById(R.id.button)).setOnClickListener(getBtnClickListener());
}
private OnClickListener getBtnClickListener() {
return new OnClickListener() {
public void onClick(View view) {
try {
HashMap<String, String> item = new HashMap<String, String>();
item.put(ITEM_KEY, newValue.getText().toString());
list.add(item);
adapter.notifyDataSetChanged();
} catch (NullPointerException e) {
Log.i("[Dynamic Items]", "Tried to add null value");
}
}
};
}
}
Java代码:
<?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"
>
<ListView android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1"
android:cacheColorHint="#00000000">
</ListView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6px"
android:layout_marginBottom="8px"
android:layout_marginLeft="8px"
android:layout_marginRight="8px"
android:background="#00000000">
<ImageButton
android:id="@+id/button"
android:layout_width="100px"
android:layout_height="100px"
android:src="@android:drawable/ic_menu_add" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="230px"
android:layout_height="wrap_content"
android:layout_marginTop="8px"
android:layout_marginBottom="8px"
android:layout_marginLeft="0px"
android:layout_marginRight="28px"
android:background="#cccccc00" >
<EditText android:id="@+id/new_value_field"
android:textColor="#020905"
android:textSize="18sp"
android:layout_marginTop="6px"
android:layout_marginBottom="8px"
android:layout_marginLeft="12px"
android:layout_marginRight="12px"
android:layout_width="210px"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
ROw:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00000000">
<TextView
android:id="@+id/list_value"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
相关文章推荐
- jQuery动态添加与删除tr行实例代码
- 【Android】如何动态根据需要的用Java添加安卓控件——以ImageView为例~~
- Android 动态添加view或item并获取数据的实例
- JS动态添加option和删除option(附实例代码)
- iOS开发之UITableView与UISearchController实现搜索及上拉加载,下拉刷新实例代码
- JS 通过系统时间限定动态添加 select option的实例代码
- JS动态添加option和删除option(附实例代码)
- Android 如何在代码中动态的添加View 及 指定位置
- 安卓java 代码中动态实现 TextView的 DrawableTop属性
- 使用代码动态添加、删除view,通过tag寻找控件
- 安卓中在TextView中每行的特定位置修改字体颜色以及添加滚动条(纯代码实现)
- 安卓开发——为动态添加的EditText加载xml文档属性
- Angular动态添加、删除输入框并计算值实例代码
- JS动态添加option和删除option(附实例代码)
- 微信支付开发动态链接Native支付实例代码
- Android View添加 Listener 实例代码
- Javascript实现动态菜单添加的实例代码
- android开发仿ios的UIScrollView实例代码
- js动态添加表格逐行添加、删除、遍历取值的实例代码