2013-08-01 98 views
0

我正在创建一个android应用程序,其中显示项目列表,用户可以从中进行选择。我试图更新列表,如果项目,但应用程序一直崩溃,无论我写什么代码。下面就是我在OnCreate方法做如何更新列表适配器

我有下面的代码

adapter = new ArrayAdapter<String>(getActivity(), layout, showArray); 
setListAdapter(adapter); 

的那一刻,showArray是一个空数组和现在applciation作品。

接下来,我在showArray中插入数据,并试图在另一个方法中更新列表适配器。我正在使用下面的代码:

adapter = new ArrayAdapter<String>(getActivity(), layout, showArray); 
setListAdapter(adapter); 

但应用程序崩溃。我试图将我的代码更改为:

adapter = new ArrayAdapter<String>(getActivity(), layout, showArray); 
adapter.notifyDataSetChanged(); 

但是再一次没有运气。

我的全代码如下:

public class Fragment_AllCommunities extends Fragment { 
    OnHeadlineSelectedListener callBack; 

    // The container Activity must implement this interface so the frag can deliver messages 
    public interface OnHeadlineSelectedListener { 
     public void onCommunitySelected(int position); 
    } 

    private ProgressDialog progress; 
    private Activity activity; 
    private JSONArray communities; 
    private JSONObject[] commDetails; 
    private String[] showArray = {""}; 
    private int layout = 0; 
    private ArrayAdapter<String> adapter; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // We need to use a different list item layout for devices older than Honeycomb 
     layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? 
      android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1; 

     adapter = new ArrayAdapter<String>(getActivity(), layout, showArray); 
     setListAdapter(adapter); 
    } 

    public void updateAllCommunities(JSONObject[] commArr) throws JSONException { 
     commDetails = commArr; 

     showArray = new String[commDetails.length]; 

     for(int i=0;i<commDetails.length;i++) 
     { 
      showArray[i] = commDetails[i].getString("name").toString(); 
     } 

     //adapter = new ArrayAdapter<String>(getActivity(), layout, showArray); 
     //adapter.notifyDataSetChanged(); 

     try 
     { 
      adapter.notifyDataSetChanged(); 
     } 
     catch(Exception ex) 
     { 
      String s = ex.getMessage(); 
     } 

    } 


    @Override 
    public void onStart() { 
     super.onStart(); 
     if (getFragmentManager().findFragmentById(R.id.view_community_fragment) != null) { 
      getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
      //getListView().setItemChecked(0, true); 
     } 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     try { 
      callBack = (OnHeadlineSelectedListener) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() 
       + " must implement OnHeadlineSelectedListener"); 
     } 
    } 

    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
     callBack.onCommunitySelected(position); 

     getListView().setItemChecked(position, true); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     String a = "S"; 
    } 
} 

谁能告诉我什么,我做错了PLS?如果您想更新您的列表只是把这个code.It将更新您的列表视图更新数据的适配器

在此先感谢 感谢所有响应

克莱夫

+0

我们需要LogCat的堆栈跟踪。 –

+0

添加有问题的堆栈跟踪。 –

+0

你能告诉我该怎么做吗?我是Android的初学者。谢谢 –

回答

0

adapter.notifyDataSetChanged(); 
+0

发生空指针异常,我不知道y –

+0

在调用adapter.notifyDataSetChanged()之前检查是否初始化了适配器并设置了数据。 –

1

我敢肯定,问题是你不止一次地致电setListAdapter。您应该只在构建ListFragment时调用它。如果要修改列表,则必须通过调用适配器的add()remove()方法将数据添加到基础数据结构中。

即:

adapter.add(object); 
adapter.notifyDataSetChanged() 

通常我创建将数据添加到适配器的包装方法。

+0

始终为空指针异常。我不知道为什么会发生这种情况。我可能会尝试改变整个班级,也许我会让它工作 –

+0

@CliveCauchi NPE是什么?你能给我任何有关的信息吗? –

+0

@CliveCauchi我只想到了一件事。尝试将代码从'onCreate'移动到'onActivityCreated',如本教程所示:http://www.vogella.com/articles/AndroidListView/#listfragments NPE的问题可能是它返回null,因为Activity不是尚未创建。 –