揭秘在ListView等AdapterView上动态添加删除项的陷阱_安卓软件开发教程-查字典教程网
揭秘在ListView等AdapterView上动态添加删除项的陷阱
揭秘在ListView等AdapterView上动态添加删除项的陷阱
发布时间:2016-12-28 来源:查字典编辑
摘要:如何避开在ListView等AdapterView上动态添加删除项的陷阱,下面就为大家分享,具体内容如下首先,定义如下array资源,作为列...

如何避开在ListView等AdapterView上动态添加删除项的陷阱,下面就为大家分享,具体内容如下

首先,定义如下array资源,作为列表的加载内容:

<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>

然后简单地写下布局文件,由于我需要不管列表有多长,始终在底部显示编辑框和按钮,所以将ListView中的layout_weight设为1。

<"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>

最后添上Activity的代码,似乎没什么问题了,运行一下。

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(""); } } }; }

界面成功显示,可是每次点击“添加”,就会抛出java.lang.UnsupportedOperationException,这看似很匪夷所思,因为按照android的api文档,确实能通过adapter上的add、remove等方法在运行时增删列表项啊,那问题到底出在哪里呢?

借助google的帮助,找到如下解答:

这里说到,如果要动态的改变列表的大小,必须使用一个可变大小的List(如ArrayList),而不能使用固定长度的array,否则将会得到UnsupportedOperationException。也就是说,由于需要从资源文件中加载内容,所以我自然就想到调用ArrayAdapter.createFromResource(Context context, int textArrayResId, int textViewResId)方法来构造adapter,而此方法导致ArrayAdapter中维护的是一个定长数组!对数组进行add,当然就会出错了。看到这里我不禁感慨,好一个陷阱!!!真相仿佛离我越来越近了,让我们再进入ArrayAdapter源码探个究竟。

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(); } } // 其他方法 }

通过源码可以发现,我上面的思考还是有误的。ArrayAdapter并没有单独维护array类型的数据,而是统一转换成了List,并存在了mObjects对象中。

createFromResource(...)调用了ArrayAdapter(Context context, int textViewResourceId, T[] objects)构造方法,而在该方法的内部实现中,android使用Arrays的静态方法asList(...)将一个数组转换为List。熟悉Java的程序员都知道,Arrays.asList(...)方法所返回的并不是一个java.util.ArrayList,而是一个Arrays类的内部类,该List实现是不能进行增删操作的!!(见jdk文档),对该List进行add将抛出UnsupportedOperationException!

哈哈,这下终于真相大白了,这样一来稍微改动一下原来的代码即可成功运行:D

FInal Solution:

/** * * @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(""); } } }; }

以上就是关于Android Listview相关内容介绍,希望对大家的学习有所帮助。

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新安卓软件开发学习
    热门安卓软件开发学习
    编程开发子分类