AMH AMH - 8 days ago 6
Android Question

Adapter,progress dialog doesn't work in fragment

I have class that extend fragment

public class oUnit extends Fragment {


I want to fill list and add it to linear layout so I use the following code

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ProgressDialog connectionProgressDialog = new

ProgressDialog( getActivity());
connectionProgressDialog.setCancelable(false);
connectionProgressDialog.setCanceledOnTouchOutside(false);
connectionProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
connectionProgressDialog.setMessage("Uploading Leads...");
connectionProgressDialog.show();

View view = inflater.inflate(
R.layout.chemounit,
container,
false);


//Intialize the record Grid
LinearLayout formLayout = (LinearLayout)view.findViewById(R.id.UnitGrid);
formLayout.removeAllViews();

MainGrid = new ListView(getActivity().getApplicationContext());
MainGrid.setVisibility(ListView.VISIBLE);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;
MainGrid.setLayoutParams(params);


//2. Call the web Service



GlobalVariables appState = (GlobalVariables) getActivity().getApplication();

Log.d(" in Chemo unit"," the array is has "+ appState.encounters.size());

MainGrid.setAdapter(new Encounteradapter(view.getContext(),R.id.ChemoUnitGrid , appState.encounters));
// Finally add it

formLayout.addView(MainGrid);


connectionProgressDialog.dismiss();

return view;
}


I am sure that the global variable list is not null and contains data (I used
log.d
to display its length) ,, the Adapter class is like

public class Encounteradapter extends ArrayAdapter<Encounter> {

private final Context context;

TextView textViewTime ;
TextView textViewPatientName;
Button ButottonArrive ;
Button ButottonEncounter;
Button ButottonExit;
ArrayList<Encounter> Encounters ;

public Encounteradapter(Context context, int ResourceId,
ArrayList<Encounter> items)
{


super(context, ResourceId, items);
this.context = context;
this.Encounters = items ;
}

public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View rowView = inflater.inflate(R.layout.chemounitgrid, parent, false);




Encounter EncounterObject = Encounters.get(position);

textViewTime = (TextView) rowView.findViewById(R.id.Time);
textViewTime.setText(EncounterObject.bookingDate);

textViewPatientName = (TextView) rowView.findViewById(R.id.Time);
textViewPatientName.setText(EncounterObject.Name);

ButottonArrive = (Button) rowView.findViewById(R.id.test1);
ButottonEncounter = (Button) rowView.findViewById(R.id.test2);
ButottonExit = (Button) rowView.findViewById(R.id.test3);

rowView.setTag(position);




return rowView;
}
}


but nothing appear the list doesn't appear, I also tried to remopve the section of properties I set but nothing either

this is the first issue the second I try to display progress on fragment OnCreate() but it doesn't appear I use the following code

connectionProgressDialog = new ProgressDialog(getActivity());
connectionProgressDialog.setCancelable(false);
connectionProgressDialog.setCanceledOnTouchOutside(false);
connectionProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
connectionProgressDialog.setMessage("Uploading Leads...");
connectionProgressDialog.show();


any idea how to fix that , do I miss anything in with fragment

Answer

It's normal that you don't have anything on the screen because in the onCreateView method you inflate the layout and setup the ListView but then when it's time to return the view(the layout for that fragment with the ListView where you put the data) you do:

return inflater.inflate(R.layout.chemounit, container, false);

which will inflate again the layout file with a empty ListView(so all the setup that you previously done was useless as you discarded the first inflated View). Instead you should return:

return view;
Comments