仅在活动开始时显示Android ListFragment错误

时间:2014-01-07 14:09:47

标签: android android-listview android-fragments android-activity android-listfragment

我有一个应用程序从xml文件中读取数据,并根据该数据填充ListFragment。应用程序启动后,我希望填充并显示列表,但it displays this

然而,当点击设备按钮时,the list is diplayed

文件的解析是在主活动的onCreate()中完成的,显示列表适配器的集合的方法是updateDeviceList(),它在onActivityCreated()方法中都被调用片段和按钮的OnClick()侦听器。这意味着正在解析文件并正确设置列表,但是在单击按钮之前应用程序不会显示它。

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;

}
}

我可能会出错的任何想法?

由于

1 个答案:

答案 0 :(得分:1)

按下按钮时,只调用updateDeviceList(devices) 如果你想在按下按钮之前发生这种情况,你不应该在别处打电话吗? 我看到你从onActivityCreated调用它。但我只看到你设置片段定义的位置,而不是实例化它

FragmentTransaction transaction = getFragmentManager().beginTransaction();
transction.add(R.id.device_list, devList).commit();