我正在开发一款Android应用,它有一个活动和五个片段。
在其中一个片段上有一个从远程数据库填充的GridView。
我试图了解GridView的行为,但没有成功。
在新应用启动后,这些项目会按预期显示。但是稍后,如果我更改数据库行,添加,删除或更新它们,则不会更新gridview。
作为最后的解决方案,我已经包含了一个刷新gridview项目的按钮,但也没有成功。
这是获取远程项目的方法:
private void getUpicksFromDB(int id) {
upicks.clear();
AsyncTask<Integer, Void, Void> asyncTask = new AsyncTask<Integer, Void, Void>() {
@Override
protected Void doInBackground(Integer... addressesIds) {
// Log.d("HOLA PERFIL", "UID REGISTRADO ANTES DE CARGAR REECYCLER: " + user_id);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://.../upicks_todos.php")
.build();
try {
okhttp3.Response response = client.newCall(request).execute();
JSONArray array = new JSONArray(response.body().string());
Log.d("aNTES","EN ONCREATE 4.5 numero de objetos "+array.length());
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
Log.d("HOLA ADDRESSES", "DIRECCION LEIDA: " + i);
Upicks upicks = new Upicks(object.getInt("id_servicio"),
object.getString("driver_servicio"),
object.getString("inicio_servicio"),
object.getString("lat_origen_servicio"),
object.getString("lon_origen_servicio"),
object.getString("lat_destino_servicio"),
object.getString("lon_destino_servicio"),
object.getString("cliente_servicio"),
object.getString("final_servicio"),
object.getString("distancia_servicio"),
object.getString("tarifa_servicio"),
object.getString("origen_servicio"),
object.getString("destino_servicio"),
object.getString("estado_servicio"),
object.getString("tiempo_servicio")
);
Log.d("aNTES","EN ONCREATE 4.5 id leido "+object.getString("id_servicio"));
MisUpicksFragment.this.upicks.add(upicks);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
adapter.notifyDataSetChanged();
Log.d("aNTES","EN ONCREATE 5 estouy en getpicks despues de notify");
}
};
asyncTask.execute(id);
}
这是刷新项目的按钮:
botonrefrescar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recyclerView.setAdapter(null);
recyclerView.setAdapter(adapter);
getUpicksFromDB(0);
adapter.notifyDataSetChanged();
}
});
我试图找到一个解决方案两天。
谢谢
编辑:
public class MisUpicksFragment extends Fragment {
// 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";
private SessionManager session;
private ProgressDialog loading;
private EditText txtbusqueda;
private Button botonbuscar,botonrefrescar;
//movies
private static final String TAG = MainActivity.class.getSimpleName();
public List<Upicks> upicks;
private RecyclerView recyclerView;
private GridLayoutManager gridLayout;
private UpicksAdapter adapter;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private String user_id;
private Button btnNew;
private OnFragmentInteractionListener mListener;
public MisUpicksFragment() {
// Required empty public constructor
}
/**
* 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 MensajesFragment.
*/
// TODO: Rename and change types and number of parameters
public static MisUpicksFragment newInstance(String param1, String param2) {
MisUpicksFragment fragment = new MisUpicksFragment();
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) {
Log.d("MISUPICKS","ESTOE EN MISUPICKS ONCREATE");
View view = inflater.inflate(R.layout.fragment_misupicks, container, false);
txtbusqueda = (EditText) view.findViewById(R.id.txtbusqueda);
botonbuscar =(Button) view.findViewById(R.id.btnbuscar);
botonrefrescar =(Button) view.findViewById(R.id.btnRefrescar);
botonbuscar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String busqueda = txtbusqueda.getText().toString();
getUpicksBusquedaFromDB(0, busqueda);
}
});
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
upicks = new ArrayList<>();
gridLayout = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(gridLayout);
adapter = new UpicksAdapter(getActivity(), upicks);
recyclerView.setAdapter(adapter);
Log.d("aNTES","EN ONCREATE 1 ANTES DE CLEAR");
upicks.clear();
Log.d("aNTES","EN ONCREATE 2 DESPUES DE CLEAR");
getUpicksFromDB(0);
Log.d("aNTES","EN ONCREATE DESPUES DE GETUPICKS");
botonrefrescar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recyclerView.setAdapter(null);
recyclerView.setAdapter(adapter);
getUpicksFromDB(0);
adapter.notifyDataSetChanged();
}
});
return view;
}
private void getUpicksBusquedaFromDB(int id, final String busqueda) {
upicks.clear();
Log.d("estoy en directorio= ",busqueda);
Log.d("Texto buscado= ",busqueda);
AsyncTask<Integer, Void, Void> asyncTask = new AsyncTask<Integer, Void, Void>() {
@Override
protected Void doInBackground(Integer... addressesIds) {
String URL = "http://.../upicks_todos_busca.php?id="+busqueda;
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(URL)
.build();
try {
okhttp3.Response response = client.newCall(request).execute();
Log.d("Texto buscado=estoy ",busqueda);
JSONArray array = new JSONArray(response.body().string());
for (int i = 0; i < array.length(); i++) {
Log.d("Texto buscado loop= ",busqueda);
JSONObject object = array.getJSONObject(i);
Log.d("HOLA ADDRESSES", "DIRECCION LEIDA: " + i);
Upicks upicks = new Upicks(object.getInt("id_servicio"),
object.getString("driver_servicio"),
object.getString("inicio_servicio"),
object.getString("lat_origen_servicio"),
object.getString("lon_origen_servicio"),
object.getString("lat_destino_servicio"),
object.getString("lon_destino_servicio"),
object.getString("cliente_servicio"),
object.getString("final_servicio"),
object.getString("distancia_servicio"),
object.getString("tarifa_servicio"),
object.getString("origen_servicio"),
object.getString("destino_servicio"),
object.getString("estado_servicio"),
object.getString("tiempo_servicio")
);
MisUpicksFragment.this.upicks.add(upicks);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
recyclerView.swapAdapter(adapter, true);
}
};
asyncTask.execute(id);
}
private void getUpicksFromDB(int id) {
Log.d("aNTES","EN ONCREATE 3 estouy en getpicks antes de clear");
upicks.clear();
Log.d("aNTES","EN ONCREATE 4 estouy en getpicks despues de clear");
Log.d("MISUPICKS","ESTOE EN MISUPICKS getupicksfromddb");
AsyncTask<Integer, Void, Void> asyncTask = new AsyncTask<Integer, Void, Void>() {
@Override
protected Void doInBackground(Integer... addressesIds) {
// Log.d("HOLA PERFIL", "UID REGISTRADO ANTES DE CARGAR REECYCLER: " + user_id);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://j../upicks_todos.php")
.build();
try {
okhttp3.Response response = client.newCall(request).execute();
JSONArray array = new JSONArray(response.body().string());
Log.d("aNTES","EN ONCREATE 4.5 numero de objetos "+array.length());
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
Log.d("HOLA ADDRESSES", "DIRECCION LEIDA: " + i);
Upicks upicks = new Upicks(object.getInt("id_servicio"),
object.getString("driver_servicio"),
object.getString("inicio_servicio"),
object.getString("lat_origen_servicio"),
object.getString("lon_origen_servicio"),
object.getString("lat_destino_servicio"),
object.getString("lon_destino_servicio"),
object.getString("cliente_servicio"),
object.getString("final_servicio"),
object.getString("distancia_servicio"),
object.getString("tarifa_servicio"),
object.getString("origen_servicio"),
object.getString("destino_servicio"),
object.getString("estado_servicio"),
object.getString("tiempo_servicio")
);
Log.d("aNTES","EN ONCREATE 4.5 id leido "+object.getString("id_servicio"));
MisUpicksFragment.this.upicks.add(upicks);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
recyclerView.setAdapter(null);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
};
asyncTask.execute(id);
}
private void logoutUser() {
session.setLogin(false);
// Launching the login activity
Intent intent = new Intent(getActivity(), LoginActivity.class);
startActivity(intent);
//finish();
}
@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);
}
}
答案 0 :(得分:0)
If(null != upicks){
adapter = new UpicksAdapter(getActivity(), upicks);
recyclerView.setAdapter(adapter);
recyclerView.swapAdapter(adapter, true);
adapter.notifyDataSetChanged();
}