java - 如何在不重新启动应用程序的情况下在 recycleview 中显示文本?
问题描述
我的问题是,除非我重新启动应用程序,否则按下 OK 按钮后我无法在 recycleview 中添加文本,每次我在对话框内写一个单词然后按 OK,但只有在重新启动应用程序后该单词才会出现在 recycleview 中,我想输入单词然后按OK按钮然后单词直接出现在recycleview里面
// 类 Tab1 :
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.inject.Inject;
import static android.R.attr.fragment;
import static com.example.chirag.slidingtabsusingviewpager.R.id.parent;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link Tab1.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link Tab1#newInstance} factory method to
* create an instance of this fragment.
*/
public class Tab1 extends Fragment {
@Inject
Context mContext_;
View rootView;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
Context context;
SimpleAdapter adapter;// = new SimpleAdapter(generateSimpleList());
RecyclerView recyclerView ;//= (RecyclerView) view.findViewById(R.id.simple_recyclerview);
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private List<Organization>organizationList;
private OnFragmentInteractionListener mListener;
private static Context mContext;
LayoutInflater inflater2;
ViewGroup Container = null;
View vr;
public Tab1(){
// Required empty public constructor
// test ();
//context = getContext();
Organization organization = new Organization();
organizationList = SQLite.select().from(Organization.class).queryList();
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Tab1.
*/
// TODO: Rename and change types and number of parameters
public static Tab1 newInstance(String param1, String param2) {
Tab1 fragment = new Tab1();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_tab1, container,false);
vr = view;//inflater.inflate(R.layout.fragment_tab1, container,false);
initViews();
showInputDialog();
return view;
}
private void initViews(){
adapter = new SimpleAdapter(generateSimpleList());
recyclerView = (RecyclerView)vr.findViewById(R.id.simple_recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString() + "must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
private List<SimpleViewModel> generateSimpleList(){
List<SimpleViewModel>simpleViewModelList = new ArrayList<>();
for(int i=0; i<organizationList.size(); i++){
simpleViewModelList.add(new SimpleViewModel(String.format(Locale.US,organizationList.get(i).getName().toString(),""+organizationList.get(i).getName().toString())));
// Toast.makeText(MainActivity.this,"as"+ organizationList.get(i).getName(), Toast.LENGTH_LONG).show();
}
return simpleViewModelList;
}
protected void showInputDialog(){
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
//LayoutInflater layoutInflater = LayoutInflater.from(getContext().getApplicationContext());
final View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setView(promptView);
final EditText editText = (EditText) promptView.findViewById(R.id.edittext);
// final EditText editText = (EditText) promptView.findViewById(R.id.edittext);
// setup a dialog window
alertDialogBuilder.setCancelable(false).setPositiveButton("OK",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
Organization organization = new Organization();
organization.setId(organizationList.size()+1);
organization.setName(editText.getText().toString());
organization.save();
//initViews();
}}).setNegativeButton("Cancel",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
//create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
/*
AlertDialog alert = alertDialogBuilder.create();
alert.show();
*/
}
}
/
/Organization class :
import com.raizlabs.android.dbflow.annotation.Column;
import com.raizlabs.android.dbflow.annotation.PrimaryKey;
import com.raizlabs.android.dbflow.annotation.Table;
import com.raizlabs.android.dbflow.structure.BaseModel;
// **Note:** Your class must extend from BaseModel
@Table(database = MyDatabase.class)
public class Organization extends BaseModel {
@Column
@PrimaryKey
int id;
@Column
String name;
private Organization organization;
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setOrganization(Organization organization) {
this.organization = organization;
}
public void setName(String name) {
this.name = name;
}
}
// input_dialog.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Enter Name"
android:id="@+id/textView"/>
<EditText
android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter text here.."
android:padding="10dp"/>
</LinearLayout>
解决方案
在 setPositiveButton 的 onClick 中添加这两行
(就在组织.save() 之后)
.......
organization.save();
organizationList.add(organization);
adapter.notifyDataSetChanged();
推荐阅读
- vue.js - Vuex 在执行路由器操作之前等待承诺
- angular - 以角度获取HttpErrorResponse错误
- video - 在处理中播放视频
- java - 执行 Post Methode 时获取响应代码 400(语法错误)
- c# - 如何在 .NET 核心中快速将巨大的文本文件从 Unix 转换为 Windows
- javascript - Chrome 中的 mouseenter/mouseover 事件不起作用
- javascript - 如何正确使用 WOW.JS?
- python - When importing a module in another .py , return of function is different to directly run it?
- c# - 使用 .Lookup() 进行聚合查询很热门?(MongoDB 驱动程序,c#)
- hyperledger-fabric - 尝试访问 Hyperledger Fabric 身份提供者上下文时,角色和从属关系为空