Issue
I have to display a recyclerview inside a PopupWindow after a Volley call. I'm able to get the data after volley call and display it in the activity or a spinner but I not sure how to do that in a PopupWindow. Since I dont get where to initialize the recycleview and the adapter. Kindly tell me where shall I call the GET_MALL_WEB_CALL()
mathod and how to pass the data into the popup window, so that I can access the data on the popup window. Could any one help in achieving this.Should I do this in some other way? I'm kinda stuck here.
SetupActivity.java
public class SetupActivity extends AppCompatActivity {
private RequestQueue requestQueue2;
private RecyclerView mallRecyclerView;
private MallAdapter mallRecyclerAdapter;
List<ModelMall> newMallModels = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup);
initViews();
EditText popupButton = findViewById(R.id.editTextMall);
popupButton.setInputType(InputType.TYPE_NULL);
popupButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopup(v)
}
});
}
private void initViews() {
imageVProfileSmall = findViewById(R.id.profileImg);
//This is how I used to Initialize the recyclerview and adapter if I display the data in the same activity
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mallRecyclerView = findViewById(R.id.recyclerView);
mallRecyclerView.setLayoutManager(layoutManager);
mallRecyclerAdapter = new MallAdapter(this);
mallRecyclerView.setAdapter(mallRecyclerAdapter);
}
private void GET_MALL_WEB_CALL() {
String HTTP_SERVER_URL = "http://GETMALLAPI";
JsonArrayRequest jsArrRequest = new JsonArrayRequest
(Request.Method.GET, HTTP_SERVER_URL, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
GET_MALL_PARSE(response);
if (response.length() > 1) {
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
Log.i("ERROR", "Event Web call Error");
}
mallRecyclerAdapter.notifyDataSetChanged();
clear();
mallRecyclerAdapter.updateModels(newMallModels);
mallRecyclerView.setAdapter(mallRecyclerAdapter);
}
}) {
//This is for Headers If You Needed
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/x-www-form-urlencoded");
params.put("Authorization", "Bearer " + getFromSP("token"));
return params;
}
};
requestQueue2.add(jsArrRequest);
}
public void clear() {
int size = newMallModels.size();
newMallModels.clear();
Log.i("LOL", String.valueOf(size));
}
public void GET_MALL_PARSE(JSONArray array) {
clear();
//List<EventsDataModel> newModels = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
ModelMall GetMallDataModel = new ModelMall();
Log.i("SUCCESS", "Event web call success");
JSONObject json = null;
try {
json = array.getJSONObject(i);
GetMallDataModel.setMallId(json.getString("$id"));//
GetMallDataModel.setMallName(json.getString("Resourcetype_en"));//
newMallModels.add(GetMallDataModel);
} catch (JSONException e) {
e.printStackTrace();
}
}
if (array.length() != 0) {
mallRecyclerAdapter.updateModels(newMallModels);
}
}
public void showPopup(View view){
//Create a View object yourself through inflater
LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(view.getContext().LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.layout_mall_recycler, null);
//Specify the length and width through constants
int width = LinearLayout.LayoutParams.MATCH_PARENT;
int height = LinearLayout.LayoutParams.MATCH_PARENT;
//Make Inactive Items Outside Of PopupWindow
boolean focusable = true;
//Create a window with our parameters
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
//Set the location of the window on the screen
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
//How to get the values from GET_MALL_PARSE() in the popup window ????
// RecyclerView recyclerView = (RecyclerView) popupView.findViewById(R.id.re);
// ArrayList<String> data = new ArrayList<>();
// data.add("my data");
// data.add("my test data");
// PopupRecyclerViewAdapter adapter = new PopupRecyclerViewAdapter(mContext,data);
// recyclerView.setAdapter(adapter);
//Handler for clicking on the inactive zone of the window
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//Close the window when clicked
popupWindow.dismiss();
return true;
}
});
}
private void saveInSp(String key, String value) {
SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
editor.apply();
}
private String getFromSP(String key) {
SharedPreferences preferences = this.getSharedPreferences("PROJECT_NAME", Context.MODE_PRIVATE);
return preferences.getString(key, "");
}
@Override
protected void onResume() {
super.onResume();
}
}
MallAdapter.java
public class MallAdapter extends RecyclerView.Adapter<MallAdapter.ViewHolder> {
private Context context;
private final List<ModelMall> mallDataModels;
private static ProgressDialog mProgressDialog;
public MallAdapter(Context context) {
super();
this.context = context;
this.mallDataModels = new ArrayList<ModelMall>();
}
public void updateModels(List<ModelMall> newModels) {
mallDataModels.clear();
mallDataModels.addAll(newModels);
notifyDataSetChanged();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_layout, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
final ModelMall dataAdapter = mallDataModels.get(position);
viewHolder.tVMallName.setText(dataAdapter.getMallName());
saveInSp("SelectedMallName", String.valueOf(dataAdapter.getMallId()));
}
private static void removeSimpleProgressDialog() {
try {
if (mProgressDialog != null) {
if (mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
mProgressDialog = null;
}
}
} catch (Exception ie) {
ie.printStackTrace();
}
}
public static void showSimpleProgressDialog(Context context, String title,
String msg, boolean isCancelable) {
try {
if (mProgressDialog == null) {
mProgressDialog = ProgressDialog.show(context, title, msg);
mProgressDialog.setCancelable(isCancelable);
}
if (!mProgressDialog.isShowing()) {
mProgressDialog.show();
}
} catch (Exception ie) {
ie.printStackTrace();
}
}
@Override
public int getItemCount() {
return mallDataModels.size();
}
private void saveInSp(String key, String value) {
SharedPreferences preferences = context.getSharedPreferences("PROJECT_NAME", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
editor.apply();
}
class ViewHolder extends RecyclerView.ViewHolder {
private TextView tVMallName;
public ViewHolder(View itemView) {
super(itemView);
tVMallName = itemView.findViewById(R.id.textMallName);
}
}
}
popup_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:gravity="center">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="5dp">
<LinearLayout
android:id="@+id/recyclerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/recycle_box"
android:layout_margin="@dimen/value40_50_60"
android:orientation="vertical">
<TextView
android:id="@+id/text1"
style="@style/selectOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:background="@drawable/recycle_select_box"
android:fontFamily="@font/four_c_seven"
android:gravity="center"
android:text="Select Mall"
android:textColor="#fff"
android:textSize="@dimen/value12_16_20"
android:textStyle="bold" />
<androidx.recyclerview.widget.RecyclerView
style="@style/selectOne"
android:layout_marginBottom="40dp"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
android:orientation="vertical"
tools:listitem="@layout/recycler_item_layout"
tools:itemCount="20"
android:visibility="visible"
>
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
Solution
Remove following from onCreate()
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mallRecyclerView = findViewById(R.id.recyclerView);
mallRecyclerView.setLayoutManager(layoutManager);
mallRecyclerAdapter = new MallAdapter(this);
mallRecyclerView.setAdapter(mallRecyclerAdapter);
You have to rewite the showPopup()
public void showPopup(View view){
//Create a View object yourself through inflater
LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(view.getContext().LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.layout_mall_recycler, null);
//Specify the length and width through constants
int width = LinearLayout.LayoutParams.MATCH_PARENT;
int height = LinearLayout.LayoutParams.MATCH_PARENT;
//Make Inactive Items Outside Of PopupWindow
boolean focusable = true;
//Create a window with our parameters
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
//Set the location of the window on the screen
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
//Initialize the elements of our window, install the handler
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mallRecyclerView = popupView.findViewById(R.id.recyclerView);
mallRecyclerView.setLayoutManager(layoutManager);
mallRecyclerAdapter = new MallAdapter(this);
mallRecyclerView.setAdapter(mallRecyclerAdapter);
GET_MALL_WEB_CALL();
//Handler for clicking on the inactive zone of the window
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//Close the window when clicked
popupWindow.dismiss();
return true;
}
});
}
Answered By - Kumza Ion
Answer Checked By - Robin (JavaFixing Admin)