我有一个带有tabhost的MainActivity
,tabhost
有三个tab(Fragment
)。 SecondTab具有RecyclerView
。在MainActivity
中,使用ActionBar按钮转到另一个活动(NewClass)并添加新值(MainActivity仍处于打开状态)。添加新值后,我想回到MainActivity
并刷新RecyclerView
以显示新值。
我正在使用reactToPublishedEvent()方法刷新RecyclerView,但是不起作用。
使用答案无法解决我的问题。
主要活动
public class MainActivity extends AppCompatActivity implements Publisher{
private Listener listener;
protected void onCreate(Bundle savedInstanceState) {
//. . .
public void onClick(View view)
{
Intent intent = new Intent();
intent.setClass(this , NewClass.class);
startActivityForResult(intent , 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == RESULT_OK)
{
if(data != null)
{
listener.reactToPublishedEvent();
}
}
}
@Override
public void setListener(SecondTab listener) {
this.listener = listener;
}
interface Listener{
void reactToPublishedEvent();
}}
新类
boolean check = database.SaveClass(className, classDay, classTimeStart, classTimeEnd, Integer.parseInt(classNumber), uniName , checkMidTerm , checkEndTerm , checkPractical , checkProject , checkConference);
if(check)
{
Intent intent = new Intent();
setResult(RESULT_OK , intent);
finish();
}
SecondTab:
public class SecondTab extends Fragment implements MainActivity.Listener{
private RecyclerView recyclerView;
private DatabaseHandler database;
private RecyclerViewAdapter recyclerAdapter;
private List<ClassTable> classData;
@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_second_tab, container, false);
database = new DatabaseHandler(getContext());
recyclerView = view.findViewById(R.id.recyclerView_SecondTab);
classData = new ArrayList<>();
prepareAdapter();
return view;
}
private void prepareAdapter()
{
classData = database.getClassData();//Receive table data
recyclerAdapter = new RecyclerViewAdapter(this , getContext(), classData);
recyclerView.setAdapter(recyclerAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if(context instanceof Publisher){
((Publisher)context).setListener(this);
}
}
@Override
public void reactToPublishedEvent() {
recyclerAdapter.notifyDataSetChanged();//this line is not correct
}
发行人
public interface Publisher {
void setListener(SecondTab listener);}
最佳答案
我建议您使用发布者/侦听器模式。
发布者-发布一些事件。侦听器对此事件做出反应。
因此,您需要创建两个接口:
interface Publisher{
void setListener(Listener listener)
}
和
interface Listener{
void reactToPublishedEvent()
}
您的MainActivity是发布者。它发出一些事件,因此您的MainActivity应该更新:
class MainActivity extends AppCompatActivity implements Publisher{
private Listener listener;
... // Some your code here
@Ovveride
public void setListener(Listener listener){
this.listener = listener
}
}
您的SecondTab是侦听器,应更新为:
public class SecondTab extends Fragment implements AlertDialogShowClass.OnItemChange, Listener{
... //Your code here
@Ovveride
public void onAttach(Context context){
//Set this fragment in activity as Listener
if(context instance of Publisher){
((Publisher)context).setListener(this)
}
}
}
然后在onActivityResult方法中,您可以编写如下内容:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == RESULT_OK)
{
if(data != null && listener!=null)
{
listener.reactToPublishedEvent()
}
}
}
关于android - 在添加值后从另一个 Activity 刷新RecyclerView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54762108/