手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >安卓开发中动态添加lsitView 代码实例
安卓开发中动态添加lsitView 代码实例
摘要:这个很简单就是为了说明别忘了adapter.notifyDataSetChanged();唤醒一下,其实这个唤醒也就是在button下用或者...

这个很简单 就是为了说明别忘了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(yout.dynamic_list);

newValue = (EditText) findViewById(R.id.new_value_field);

setListAdapter(new SimpleAdapter(this, list, yout.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="/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="/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>

【安卓开发中动态添加lsitView 代码实例】相关文章:

Android 混淆代码详解及实例

Android开发:14个解决实际问题的代码片段

Android开发之BroadcastReceiver用法实例分析

android 通过向viewpage中添加listview来完成滑动效果(类似于qq滑动界面)

Android生存指南之:开发中的注意事项

ListView 分页加载更新实例分享

安卓开发之调试实例

vbs打开ie两种方法 在VBScript中启动IE浏览器的实现代码

解析Android开发中多点触摸的实现方法

安卓软件开发需要学习那些编程语言

精品推荐
分类导航