作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用自定义 ArrayAdapter 显示项目列表。
我将通过单击一个按钮来插入数据。
MainFragment.java
@Override
public void onClick(View v) {
DatabaseClass feed = new DatabaseClass(getActivity());
new DatabaseClass(getActivity()).getList();
new MyFragment().updateList();
}
我的布局文件如下。
main.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/update"
android:name="com.android.MyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MyFragment.java
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MyFragment extends ListFragment {
// creating database reference
private static DatabaseClass feed;
private List<CustomModel> customList;
private static CustomListAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
feed = new DatabaseClass(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// To get List of CustomModel objects
feed.getList();
// CUSTOM_LIST is list constant
adapter = new CustomListAdapter(getActivity(),
R.layout.update, Utils.CUSTOM_LIST);
// setting adapter
setListAdapter(adapter);
View view = inflater.inflate(R.layout.update, container, false);
return view;
}
@Override
public void onStart() {
super.onStart();
}
// this method is called after inserting data into database
public void updateList() {
if (adapter != null) {
adapter.notifyDataSetChanged();
} else {
adapter = new CustomListAdapter(getActivity(),
R.layout.update, Utils.CUSTOM_LIST);
adapter.notifyDataSetChanged();
}
}
private class CustomListAdapter extends ArrayAdapter<CustomModel> {
private final Context context;
private final List<CustomModel> list;
private TextView name, message;
public CustomListAdapter(Context context, int resource,
List<CustomModel> list) {
super(context, resource, list);
this.context = context;
this.list = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.single_row, parent,
false);
name = (TextView) view.findViewById(R.id.name);
message = (TextView) view.findViewById(R.id.message);
CustomModel obj = list.get(position);
name.setText(obj.getName());
message.setText(obj.getMessage());
return view;
}
}
}
MyFragment.java 的布局文件是
更新.xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list"
/>
我的数据库类是
数据库类.java
public void getList(){
ourDatabase = this.getReadableDatabase();
String sql = "SELECT * FROM " + DATABASE_TABLE;
Cursor c = ourDatabase.rawQuery(sql, null);
int iName = c.getColumnIndex(KEY_NAME);
int iMessage = c.getColumnIndex(KEY_MESSAGE);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
CustomModel model = new CustomModel();
model.setName(c.getString(iFromName));
model.setMessage(c.getString(iMessage));
Utils.LIST.add(model);
}
}
点击按钮,数据被插入数据库。但它没有反射(reflect)在用户界面中。为什么我的 ListView 没有更新?
谢谢
最佳答案
如下更改适配器
private class CustomListAdapter extends ArrayAdapter<CustomModel> {
private final Context context;
private final List<CustomModel> list;
private TextView name, message;
public CustomListAdapter(Context context, int resource,
List<CustomModel> list) {
super(context, resource, list);
this.context = context;
this.list = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.single_row, parent, false);
name = (TextView) view.findViewById(R.id.name);
message = (TextView) view.findViewById(R.id.message);
CustomModel obj = list.get(position);
name.setText(obj.getName());
message.setText(obj.getMessage());
return view;
}
public void updateList(List<CustomModel> list) {
this.list = list;
notifyDataSetChanged();
}
}
因此在您的适配器本身中添加方法 updateList 并调用方法 notifyDataSetChanged();
所以从你的 Activity 中而不是调用 notifyDataSetChanged();
调用updateList(List<CustomModel> newList)
我相信这会奏效。
关于Android CustomListAdapter notifydatachange() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23341866/
我想使用自定义 ArrayAdapter 显示项目列表。 我将通过单击一个按钮来插入数据。 MainFragment.java @Override public void onClic
我有一个 fragment ,其中有一个带有 BaseAdapter 的 ListView。然后我有一个弹出菜单,我在其中选择一个选项并将项目添加到 Collection 夹。集合中的数据应该显示在列
我是一名优秀的程序员,十分优秀!