0
我有一个列表视图,我想用自定义列表项。如何响应adapter.notifyDataSetChanged()
列表项(list_item.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="start time"
android:id="@+id/startTime"
android:layout_margin="10dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="finish time"
android:id="@+id/finishTime"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/payment"
android:layout_margin="10dp"/>
</LinearLayout>
和Java类:
private ListView listView;
private ArrayAdapter adapter;
private ArrayList shifts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
listView = (ListView)findViewById(R.id.listview);
listView.setOnItemClickListener(this);
adapter = new ArrayAdapter(this, R.layout.list_item, shifts);
我的问题是,当应用程序开始运行,我怎么能添加一个新的列表项(ArrayList)中,重写适配器上的方法notifyDataSetChanged(),以便listview将“刷新”,然后使用我希望的参数在列表项中设置编辑文本?
我希望我的身材够清晰.. 非常感谢!
您不要在适配器视图中的视图根本无需重写notifyDataSetChanged(),适配器将完成所有操作,只需更新适配器的数据并在其上调用notifyDataSetChanged即可。 – FWeigl
好吧,那么我怎么能做一个基于xml项目的新项目? –
要自定义布局,您需要自定义适配器。 ArrayAdapter是作为快速创建标准的基于文本的列表项目的实用程序。你可以创建自己的扩展BaseAdapter的类并从那里开始。 – RobCo