在db更改後,我在適配器上調用notifyDataSetChanged
方法,但此方法不更新視圖。上cardview上抽頭RecyclerView中的notifyDatasetChanged不能從片段中工作
MyClass的片段打開我使用的界面處的外部活動接收事件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);
}
}
}
我確定數據庫已更改,新數據顯示爲我重新啓動應用程序。
是您的應用程序在onActivityResult進入()方法? –