Уважаемые Stackoverflowers:)
AllAssetsFragment
открывается из MainActivity. В этом фрагменте у меня возникают проблемы с правильной настройкой setContentView()
и getApplicationContext()
.
Я знаю, что-то не так. Часть кода работает для нормальной деятельности, но в этом фрагменте они не работают.
AllAssetsFragment:
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.Room;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class AllAssetsFragment extends Fragment {
public AllAssetsFragment(){
}
private static final String HI = "https://uniqueandrocode.000webhostapp.com/hiren/favouritelist.php";
private List<AssetsItem>assetsItems;
private RecyclerView recyclerView;
AssetsAdapter adapter;
private JsonArrayRequest request;
private RequestQueue requestQueue;
public static FavoritesDatabase favoritesDatabase;
Button btn;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_allassets, container, false);
recyclerView = (RecyclerView)getView().findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
// +++ PROBLEM 1: Context doesn't work (same on Problem 3 and 4) +++
recyclerView.setLayoutManager(new LinearLayoutManager(this));
assetsItems = new ArrayList<>();
btn=(Button)getView().findViewById(R.id.favbtn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// +++ PROBLEM 2: This code doesn't work to open Fragment from Fragment +++
startActivity(new Intent(AllAssetsFragment.this, FavoritesFragment.class));
}
});
favoritesDatabase = Room.databaseBuilder(getActivity().getApplicationContext(), FavoritesDatabase.class, "myfavdb").allowMainThreadQueries().build();
getData();
}
private void getData() {
request = new JsonArrayRequest(HI, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject jsonObject = null;
for (int i = 0; i < response.length(); i++) {
try {
JSONObject ob = response.getJSONObject(i);
AssetsItem pr = new AssetsItem(ob.getInt("id"),
ob.getString("product_name"),
ob.getString("product_img"));
assetsItems.add(pr);
} catch (JSONException e) {
e.printStackTrace();
}
}
setupData(assetsItems);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
// +++ PROBLEM 3: Context doesn't work +++
requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);
}
private void setupData(List<AssetsItem>assetsItems){
adapter=new AssetsAdapter(assetsItems,getActivity().getApplicationContext());
recyclerView.setAdapter(adapter);
}
}
А вот и наш второй фрагмент: FavoritesFragment:
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class FavoritesFragment extends Fragment {
public FavoritesFragment() {
}
private RecyclerView rv;
private FavoritesAdapter adapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_favorites, container, false);
rv=(RecyclerView)getView().findViewById(R.id.rec);
rv.setHasFixedSize(true);
// +++ PROBLEM 4: doesn't work +++
rv.setLayoutManager(new LinearLayoutManager(this));
getFavData();
}
private void getFavData(){
List<FavoritesItem>favoritesItems=AllAssetsFragment.favoritesDatabase.favoritesDao().getFavoritesData();
adapter = new FavoritesAdapter(favoritesItems,getActivity().getApplicationContext());
rv.setAdapter(adapter);
}
}