足球游戏_中国足彩网¥体育资讯$

安卓开发中动态添加lsitView 代码实例
来源:易贤网 阅读:993 次 日期:2014-08-21 16:12:42
温馨提示:易贤网小编为您整理了“安卓开发中动态添加lsitView 代码实例”,方便广大网友查阅!

这个很简单 就是为了说明别忘了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>

中国足彩网信息请查看IT技术专栏

中国足彩网信息请查看网络编程
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 足球游戏_中国足彩网¥体育资讯$ 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标