Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to update a list adapter
    primarykey
    data
    text
    <p>I am creating an android application where i show a list of items and the user can select from them. I am trying to update list if items but the application is crashing all the time, no matter what code i write. the below is what i am doing</p> <p>in the OnCreate method i have the below code</p> <pre><code>adapter = new ArrayAdapter&lt;String&gt;(getActivity(), layout, showArray); setListAdapter(adapter); </code></pre> <p>at the moment, showArray is an empty array and for the now the applciation works.</p> <p>next, I am inserting data in showArray and I am trying to update the list adapter in another method. i am using the below code:</p> <pre><code>adapter = new ArrayAdapter&lt;String&gt;(getActivity(), layout, showArray); setListAdapter(adapter); </code></pre> <p>but the application crashes. I tried to change my code to:</p> <pre><code>adapter = new ArrayAdapter&lt;String&gt;(getActivity(), layout, showArray); adapter.notifyDataSetChanged(); </code></pre> <p>but once again no luck.</p> <p>My full code is below:</p> <pre><code>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&lt;String&gt; 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 &gt;= Build.VERSION_CODES.HONEYCOMB ? android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1; adapter = new ArrayAdapter&lt;String&gt;(getActivity(), layout, showArray); setListAdapter(adapter); } public void updateAllCommunities(JSONObject[] commArr) throws JSONException { commDetails = commArr; showArray = new String[commDetails.length]; for(int i=0;i&lt;commDetails.length;i++) { showArray[i] = commDetails[i].getString("name").toString(); } //adapter = new ArrayAdapter&lt;String&gt;(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"; } } </code></pre> <p>can anyone tell me what I am doing wrong pls?</p> <p>thanks in advance appreciate all responses</p> <p>clive</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload