我有2个标签。
A - RecycleFragment /////// B - DiaryFragment
我希望在DiaryFragment中点击按钮时更新RecycleFragment。
public class RecycleFragment extends Fragment {
static DBHelper dbHelper;
public List<Contact> items = new ArrayList<Contact>();
MyAdapter adapter = new MyAdapter(items);
@Override
public void onCreate(Bundle savedInstanceState) {
dbHelper = new DBHelper(getActivity());
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.recyle_fragment, container, false);
RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
// MyAdapter adapter = new MyAdapter(items);
rv.setLayoutManager(llm);
rv.setAdapter(adapter);
adapter.addTestData();
adapter.notifyDataSetChanged();
return rv;
}
}
====================
public class MyAdapter extends RecyclerView.Adapter<DataManager> {
static List<Contact> items = new ArrayList<Contact>();
public MyAdapter(List<Contact> items){
this.items = items;
}
@Override
public DataManager onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.entries, viewGroup, false);
return new DataManager(v);
}
@Override
public void onBindViewHolder(DataManager holder, int position) {
Contact item = items.get(position);
holder.mTitle.setText(item.get1(Contact.Field.TITLE));
holder.mDate.setText(item.get1(Contact.Field.DATE));
holder.mMonth.setText(item.get1(Contact.Field.MONTH));
holder.mTime.setText(item.get1(Contact.Field.TIME));
holder.mDay.setText(item.get1(Contact.Field.DAY));
holder.mContent.setText(item.get1(Contact.Field.CONTENT));
holder.mMood.setImageResource(item.get2(Contact.Field.MOOD));
holder.mWeather.setImageResource(item.get2(Contact.Field.WEATHER));
}
@Override
public int getItemCount() {
return items.size();
}
public void setItem(List<Contact> items){
this.items = items;
notifyItemChanged (0, null);
}
public void addTestData(){
Cursor cursor = RecycleFragment.dbHelper.D_getAllData();
if(cursor != null) {
if (cursor.moveToFirst()) {
do{
add(new Contact(cursor.getString(0),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3),
cursor.getString(5),
cursor.getString(5),
cursor.getString(6),
cursor.getInt(7),
cursor.getInt(8)));
}while (cursor.moveToNext());
}
}
}
public void add(Contact data){
items.add(data);
setItem(items);
}
}
========================================
按钮点击事件中的DiaryFragment
case R.id.IV_diary_save:
if(EDT_diary_title.getText().toString().trim() != null && EDT_diary_title.getText().toString().trim().length() != 0){
if( EDT_diary_content.getText().toString().trim() != null && EDT_diary_content.getText().toString().trim().length() != 0){
dbHelper.D_insertData(EDT_diary_title.getText().toString(), EDT_diary_content.getText().toString(),
TV_diary_month.getText().toString(), TV_diary_date.getText().toString(), TV_diary_day.getText().toString(),
TV_diary_time.getText().toString(),
diaryInfoHelper.getMoodResourceId(SP_diary_weather.getSelectedItemPosition()),
diaryInfoHelper.getWeatherResourceId(SP_diary_mood.getSelectedItemPosition())
);
myAdapter.add(new Contact
(String.valueOf(dbHelper.getRow()),EDT_diary_title.getText().toString(), EDT_diary_content.getText().toString(),
TV_diary_month.getText().toString(), TV_diary_date.getText().toString(), TV_diary_day.getText().toString(),
TV_diary_time.getText().toString(),
diaryInfoHelper.getMoodResourceId(SP_diary_weather.getSelectedItemPosition()),
diaryInfoHelper.getWeatherResourceId(SP_diary_mood.getSelectedItemPosition())
));
Toast.makeText(v.getContext(), "Success to Insert", Toast.LENGTH_SHORT).show();
((TabActivity)getActivity()).getViewPager().setCurrentItem(0);
EDT_diary_title.setText("");
EDT_diary_content.setText("");
}else {
Toast.makeText(v.getContext(), "Content is Empty ", Toast.LENGTH_SHORT).show();
EDT_diary_content.setText("");
}
}else {
Toast.makeText(v.getContext(), "Title is Empty ", Toast.LENGTH_SHORT).show();
EDT_diary_title.setText("");
}
break;
=============================================== ====
按钮单击DiaryFragment然后
移动标签RecycleFragment和
RecycleFragment内容会立即更改新项目插入数据。
但现在当按钮点击DiaryFragment时 RecycleFragment不会改变。
想要更改RecycleFragment。我退出RecycleFragment并重新启动RecycleFragment。
请帮帮我
感谢
答案 0 :(得分:1)
执行以下步骤:
1. create and interface in dairy.
2. Write A method in it.
3. create the object of this interface and initialize.
4. create a setter for this object.
5. Now in Recycle Fragment implement this interface.
6. Now finally write you code inside the overridden method of interface
我们来看这个例子。 用于声明接口。
private getBusinessList list;
public void setList(getBusinessList list) {
this.list = list;
}
public interface getBusinessList {
void result(boolean b, String msg,JSONArray array);
}
现在回收内部片段。
public class SplashActivity extends AppCompatActivity implements ServerCall.getBusinessList
现在初始化接口对象。
serverCall.setBusinessList(this);
这是我的接口实现方法。
@Override
public void getBuisnessList(boolean b, String msg,JSONArray array) {
progressDialog.dismiss();
if(b) {
//displayBusinessList(array);
}
else {
Log.e("Exception e",""+msg+" "+array.toString());
}
}
您可以在该方法中编写代码。