我在db更改后在我的适配器上调用notifyDataSetChanged
方法,但此方法不更新视图。
myclass片段点击cardview打开外部活动我使用接口接收事件onActivityResult(此火正确)
public interface StartActivityForResultInterface {
public void myStartActivityForResult(Intent intent, int requestCode);
}
myclass片段
public class ContentFragment extends Fragment implements StartActivityForResultInterface {
private PaffVocaliAdapter m_adapter;
@Override
public void myStartActivityForResult(Intent intent, int requestCode) {
// do pre-processing here if you need to
startActivityForResult(intent, requestCode);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.content_fragment,container,false);
dbmanPaffVocali loc_dbmanPafflocali = new dbmanPaffVocali((getActivity()));
List<PaffVocali> items = loc_dbmanPafflocali.lista();
if (items.size() == 0)
{
for (int i = 0; i < 10; i++) {
PaffVocali p = new PaffVocali();
p.setTitolo("Tab #" + " item #" + i);
items.add(p);
}
}
m_adapter = new PaffVocaliAdapter(items);
m_adapter.setCallback(this);
RecyclerView recyclerView = (RecyclerView)v.findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(m_adapter);
return v;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
m_adapter.notifyDataSetChanged();
}
}
myadapter类
public class PaffVocaliAdapter extends RecyclerView.Adapter<PaffVocaliAdapter.ViewHolder> {
private List<PaffVocali> mItems;
private StartActivityForResultInterface myInterface;
PaffVocaliAdapter(List<PaffVocali> items) {
mItems = items;
}
public void setCallback (StartActivityForResultInterface myInterface) {
this.myInterface = myInterface;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_listapaff, viewGroup, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
final PaffVocali item = mItems.get(i);
viewHolder.mTextViewTitolo.setText(item.getTitolo());
viewHolder.mTextViewEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Context context = view.getContext();
//context.startActivityForResult(new Intent(context, crud_activity_paff.class));
Intent intent = new Intent(context, crud_activity_paff.class);
intent.putExtra("id",item.getId());
myInterface.myStartActivityForResult(intent, 1);
}
});
}
@Override
public int getItemCount() {
return mItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final TextView mTextViewTitolo;
private final TextView mTextViewEdit;
ViewHolder(View v) {
super(v);
mTextViewTitolo = (TextView) v.findViewById(R.id.cardpaff_titolo);
mTextViewEdit = (TextView) v.findViewById(R.id.cardpaff_edit);
}
}
}
我确定数据库已更改,重新启动应用程序时会显示新数据。
答案 0 :(得分:3)
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
dbmanPaffVocali loc_dbmanPafflocali = new dbmanPaffVocali((getActivity()));
List<PaffVocali> items = loc_dbmanPafflocali.lista();
if (items.size() == 0)
{
for (int i = 0; i < 10; i++) {
PaffVocali p = new PaffVocali();
p.setTitolo("Tab #" + " item #" + i);
items.add(p);
}
}
m_adapter.setMItems(items);//add the method
m_adapter.notifyDataSetChanged();
}
尝试
答案 1 :(得分:0)
您正在调用notifyDataSetChanged
,但您没有更新适配器类中的数据,这就是您没有看到RecyclerView
中的任何更改的原因。在调用notifyDataSetChanged
之前,您需要更新数据(将新数据传递给适配器)。
将onActivityResult
更改为:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
dbmanPaffVocali loc_dbmanPafflocali = new dbmanPaffVocali((getActivity()));
List<PaffVocali> items = loc_dbmanPafflocali.lista();
if (items.size() == 0)
{
for (int i = 0; i < 10; i++)
{
PaffVocali p = new PaffVocali();
p.setTitolo("Tab #" + " item #" + i);
items.add(p);
}
}
m_adapter.setData(items);
m_adapter.notifyDataSetChanged();
}
在你的适配器类中实现一个传递新数据的公共方法:
public void setData(List<PaffVocali> items)
{
mItems = items;
}