I have two fragments in my Activity. Both of these fragments have unique listView, these fragments have methods that get values from database, One fragment shows used data and the other shows added data so I use the same Model. Also these fragments use two diffrence instance of the same arrayAdapter class which extends ArrayAdapter<>.
Class FragmentPartTaken extends Fragment
//Instance Variables
ListView listPartTaken;
AdapterPartsIntakenOutMinimized adapter;
private ArrayList<ModelPartIntakenOuttake> modelPartOuttakes;
//onCreateView
listPartTaken = view.findViewById(R.id.list_part_taken);
modelPartOuttakes = new ArrayList<>();
adapter = new AdapterPartsIntakenOutMinimized(modelPartOuttakes,getContext());
Class FragmentPartAdded extends Fragment
//Instance Variables
ListView listPartAdded;
AdapterPartsIntakenOutMinimized adapter;
private ArrayList<ModelPartIntakenOuttake> modelPartAdded;
//onCreateView
listPartAdded = view.findViewById(R.id.list_part_added);
modelPartAdded = new ArrayList<>();
adapter = new AdapterPartsIntakenOutMinimized(modelPartAdded,getContext());
When the Activity is Launched, the list in the first Fragment selected(Displayed) is the one that is populated with data, and the other list is left blanks. If I change the order of position of these Fragments it does the same, the list in the first fragment get populated but not the second.
The arrayAdapter Override a single public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {}
method.
My question is do I need to have each Fragment with its own Adapter class and Model. So as all can be populated at once when the activity is launched.
Edit:
public class AdapterPartsIntakenOut extends ArrayAdapter<ModelPartIntakenOuttake> {
private ArrayList<ModelPartIntakenOuttake> dataSet;
Context mContext;
public AdapterPartsIntakenOut(ArrayList<ModelPartIntakenOuttake> data, Context context) {
super(context, R.layout.raw_intake_outake_parts, data);
this.dataSet = data;
this.mContext=context;
}
private static class ViewHolder {
TextView textAction;
TextView textDate;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ModelPartIntakenOuttake modelPartIntakenOuttake = getItem(position);
AdapterPartsIntakenOut.ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
if (convertView == null) {
viewHolder = new AdapterPartsIntakenOut.ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.raw_intake_outake_parts, parent, false);
viewHolder.textAction = (TextView) convertView.findViewById(R.id.txt_action_intake_outtake);
viewHolder.textDate = convertView.findViewById(R.id.txt_date_intake_outtake);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (AdapterPartsIntakenOut.ViewHolder) convertView.getTag();
result=convertView;
}
viewHolder.textAction.setText(modelPartIntakenOuttake.getActionType());
viewHolder.textDate.setText(modelPartIntakenOuttake.getOnDate());
return convertView;
}
}
Edit: I am using volley library to populate the adapters in both fragment
private void getPartAdded(){
getActivity().runOnUiThread(mRunnable);
String partsTakenURL = "....";
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, partsTakenURL, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
String actionType = response.getJSONObject(i).getString("action_type");
String onDate = response.getJSONObject(i).getString("on_date");
modelPartAdded.add(new ModelPartIntakenOuttake(actionType,onDate));
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(jsonArrayRequest);
listPartAdded.setAdapter(adapter);
}
Runnable mRunnable = new Runnable() {
public void run() {
modelPartAdded.clear();
adapter.notifyDataSetChanged();
}
Also on another class
private void getPartOutTaken() {
getActivity().runOnUiThread(mRunnable);
String partsTakenURL = "...";
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, partsTakenURL, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
String actionType = response.getJSONObject(i).getString("action_type");
String onDate = response.getJSONObject(i).getString("on_date");
modelPartOuttakes.add(new ModelPartIntakenOuttake(actionType,onDate));
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(jsonArrayRequest);
listPartTaken.setAdapter(adapter);
}
Runnable mRunnable = new Runnable() {
public void run() {
modelPartOuttakes.clear();
adapter.notifyDataSetChanged();
}
};;
-
You left out the key piece of code - the adapter logic that actually loads data.dominicoder– dominicoder2023年04月10日 17:19:58 +00:00Commented Apr 10, 2023 at 17:19
-
@dominicoder just added the adapter class.ThanksGabriel Rogath– Gabriel Rogath2023年04月10日 18:11:22 +00:00Commented Apr 10, 2023 at 18:11
-
You still haven't shown what actually adds elements to the adapter. Your sample creates two empty ArrayLists to populate each adapter. Where do you actually add items to the adapters?dominicoder– dominicoder2023年04月10日 19:21:38 +00:00Commented Apr 10, 2023 at 19:21
-
@dominicoder, I have just added methods that populate both adapter with volley LibraryGabriel Rogath– Gabriel Rogath2023年04月11日 06:26:51 +00:00Commented Apr 11, 2023 at 6:26
-
I don't see anything immediately obvious from your posted code, but it's also very difficult to get a full sense of what is happening with so many snippets out of context. Debug your code and try to narrow down the issue: developer.android.com/studio/debugdominicoder– dominicoder2023年04月11日 14:22:47 +00:00Commented Apr 11, 2023 at 14:22
1 Answer 1
You can use the same adapter and model class again and again. You are probably doing something wrong while populating your array lists.
1 Comment
Explore related questions
See similar questions with these tags.