<resources> <string name="app_name">MyListView</string> <string-array name="language"> <item>Java</item> <item>C</item> <item>C++</item> <item>PHP</item> </string-array>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/addLangEdit" android:layout_width="200px" android:layout_height="wrap_content" /> <Button android:id="@+id/addButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="添加" /> </LinearLayout> </LinearLayout>
public class MyListView extends ListActivity {
private ArrayAdapter<CharSequence> mAdapter;
private ListView mListView;
private EditText mLanguageText;
private Button mAddButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylist1);
//get the view
mListView = getListView();
mLanguageText = (EditText) findViewById(R.id.addLangEdit);
mAddButton = (Button) findViewById(R.id.addButton);
//array adapter created from string array resources
mAdapter = ArrayAdapter.createFromResource(
this,
R.array.language,
android.R.layout.simple_list_item_1);
//set the adapter
mListView.setAdapter(mAdapter);
//add listener
mAddButton.setOnClickListener(mOnClickListener);
}
private OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = mLanguageText.getText().toString();
if(null == text || "".equals(text.trim())) {
Toast.makeText(MyListView.this, "输入不能为空", Toast.LENGTH_SHORT).show();
}else {
mAdapter.add(text);
mAdapter.notifyDataSetChanged();
mLanguageText.setText("");
}
}
};
}
public class ArrayAdapter<T> extends BaseAdapter implements Filterable {
// 代表ArrayAdapter中的数据
private List<T> mObjects;
// 其他fields
public ArrayAdapter(Context context, int textViewResourceId, List<T> objects) {
init(context, textViewResourceId, 0, objects);
}
public ArrayAdapter(Context context, int textViewResourceId, T[] objects) {
// 注意这里的Arrays.asList(...)方法!!!
init(context, textViewResourceId, 0, Arrays.asList(objects));
}
public static ArrayAdapter<CharSequence> createFromResource(Context context,
int textArrayResId, int textViewResId) {
CharSequence[] strings = context.getResources().getTextArray(textArrayResId);
return new ArrayAdapter<CharSequence>(context, textViewResId, strings);
}
private void init(Context context, int resource, int textViewResourceId, List<T> objects) {
mContext = context;
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mResource = mDropDownResource = resource;
mObjects = objects;
mFieldId = textViewResourceId;
}
public void add(T object) {
if (mOriginalValues != null) {
synchronized (mLock) {
mOriginalValues.add(object);
if (mNotifyOnChange) notifyDataSetChanged();
}
} else {
mObjects.add(object); // 若该mObjects为固定长度List,此处将抛异常!!!
if (mNotifyOnChange) notifyDataSetChanged();
}
}
// 其他方法
}
/**
*
* @author CodingMyWorld
* 2011-7-31 下午04:43:48
*/
public class MyListView extends ListActivity {
private ArrayAdapter<CharSequence> mAdapter;
private ListView mListView;
private EditText mLanguageText;
private Button mAddButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylist1);
//get the view
mListView = getListView();
mLanguageText = (EditText) findViewById(R.id.addLangEdit);
mAddButton = (Button) findViewById(R.id.addButton);
//array adapter created from string array resources
List<CharSequence> objects = new ArrayList<CharSequence>(
Arrays.asList(getResources().getTextArray(R.array.language)));
mAdapter = new ArrayAdapter<CharSequence>(
this,
android.R.layout.simple_list_item_1,
objects);
//set the adapter
mListView.setAdapter(mAdapter);
//add listener
mAddButton.setOnClickListener(mOnClickListener);
}
private OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = mLanguageText.getText().toString();
if(null == text || "".equals(text.trim())) {
Toast.makeText(MyListView.this, "输入不能为空", Toast.LENGTH_SHORT).show();
}else {
mAdapter.add(text);
mAdapter.notifyDataSetChanged(); //not required
mLanguageText.setText("");
}
}
};
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有