gpt4 book ai didi

android - RecyclerView 中的 notifyDatasetChanged 无法在 Fragment 中工作

转载 作者:搜寻专家 更新时间:2023-11-01 08:39:30 26 4
gpt4 key购买 nike

我在数据库更改后调用我的适配器上的 notifyDataSetChanged 方法,但此方法未更新 View 。

在 cardview 上点击我的类 fragment 打开一个外部 Activity 我使用接口(interface)接收事件 onActivityResult(这正确触发)

public interface StartActivityForResultInterface {
public void myStartActivityForResult(Intent intent, int requestCode);
}

我的类 fragment

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

}

我的适配器类

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

}
}
}

我确定数据库已更改,当我重新启动应用程序时会显示新数据。

最佳答案

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

尝试

关于android - RecyclerView 中的 notifyDatasetChanged 无法在 Fragment 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34119275/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com