Reputation: 21
I have an application that reads data from an xml file and populates a ListFragment based on that data. When the app is started, I want the list to be populated and displayed, however it displays this
Yet when the devices button is clicked, the list is diplayed
The parsing of the file is done in onCreate()
of the main activity, the method to display the set the list adapter is updateDeviceList()
which is called both in the onActivityCreated()
method in the Fragment and in the OnClick()
listener of the button. This means the file is being parsed and the list is being set correctly, but the application won't show it until the button is clicked.
MainActivity
private static ArrayList<DeviceInfo> devices;
/*Other Code*/
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Get the list fragment
devList = (DeviceList) getFragmentManager().findFragmentById(R.id.device_list);
//Get the configuration file
File dir = Environment.getExternalStorageDirectory();
File xmlFile = new File(dir, "/homeService/configurationFile.xml");
//Set up the parser
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
MyXMLHandler handler = new MyXMLHandler();
try {
//Parse the file
SAXParser saxParser = saxParserFactory.newSAXParser();
saxParser.parse(xmlFile, handler);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
devices = handler.getDeviceList();
//Devices button
Button devButton = (Button) findViewById(R.id.device_button);
devButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
updateDeviceList(devices);
}
});
DeviceListFragment
private ArrayList<DeviceInfo> devList;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
updateDeviceList(MainActivity.getDevices());
}
public void updateDeviceList(ArrayList<DeviceInfo> devices){
this.devList = devices;
//Get the names of the devices as a string array
String[] values = getNames(devList);
//Code used to resize text views in list
int itemHeight = 0;
//Avoid division by zero
if(values.length > 0)
itemHeight = calculateItemHeight(values.length);
MyArrayAdapter adapter = new MyArrayAdapter(getActivity(),values,itemHeight);
setListAdapter(adapter);
}
private String[] getNames(ArrayList<DeviceInfo> devList) {
ArrayList<String> nameList = new ArrayList<String>();
for(DeviceInfo dev : devList){
nameList.add(dev.getName());
}
String[] nameArray = new String[nameList.size()];
nameArray = nameList.toArray(nameArray);
return nameArray;
}
MyArrayAdapter
public class MyArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private int itemHeight;
public MyArrayAdapter(Context context, String[] values) {
super(context, R.layout.list_item, values);
this.context = context;
this.values = values;
this.itemHeight = 0;
}
public MyArrayAdapter(Context context, String[] values, int itemHeight) {
super(context, R.layout.list_item, values);
this.context = context;
this.values = values;
this.itemHeight = itemHeight;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item = inflater.inflate(R.layout.list_item, parent, false);
TextView textView = (TextView) item.findViewById(R.id.text_view);
textView.setText(values[position]);
textView.setGravity(Gravity.CENTER);
textView.setHeight(itemHeight);
return item;
}
}
Any ideas where I might be going wrong?
Thanks
Upvotes: 0
Views: 102
Reputation: 4836
You only call updateDeviceList(devices) when the button is pressed.
Shouldn't you call this elsewhere if you want it to occur before the button is pressed?
I see you invoke it from onActivityCreated. But I only see where you set the fragment definition, not instantiate it
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transction.add(R.id.device_list, devList).commit();
Upvotes: 1