1. Download our Official Android App: Forums for Android!

ListView not displaying custom arrayList adapter

Discussion in 'Android Development' started by specizripn, Sep 11, 2017.

  1. specizripn

    specizripn Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Sep 11, 2017

    Sep 11, 2017
    5
    0
    5
    Hello, I have been struggling with this for some time now. I am trying to populate a ListView from an arrayList, but my ListView isn't populating. I have gone through every relevant thread I have found on the internet and I still can't figure out why this isn't working. Also I checked to make sure the arrayList was populated and it is.
    I have no errors, but the ListView just displays blank. If anyone can figure out why it isn't working I will mail you a cookie! Debugging this is just getting depressing. Thanks!!!
    Adapter:
    Code (Text):
    1. public class ResultAdapter extends ArrayAdapter<User> implements View.OnClickListener {  //implements View.OnClickListener
    2.     // private Activity activity;
    3.  
    4.     Context mContext;
    5.     //private static LayoutInflater inflater = null;
    6.     private int layoutResourceId;
    7.     private static final String LOG_TAG = "ResultAdapter";
    8.     private ArrayList<User> users;
    9.  
    10.     private class ViewHolder {  //static
    11.         TextView usrName;
    12.         TextView usrSchool;
    13.         TextView usrEmail;
    14.     }
    15.  
    16.     //public ResultAdapter(Context context, ArrayList<User> users)
    17.     public ResultAdapter(ArrayList<User> data, Context context) {
    18.         super(context, R.layout.list_item, data);
    19.         this.users = data;
    20.         this.mContext = context;
    21.  
    22.     }
    23.  
    24.     @Override
    25.     public void onClick(View v) {
    26.         int position = (Integer) v.getTag();
    27.         Object object = getItem(position);
    28.         User user=(User)object;
    29.        
    30.     }
    31.     private int lastPosition = -1;
    32.  
    33.     @Override
    34.     public View getView(int position, View convertView, ViewGroup parent) {
    35.         User user = getItem(position);
    36.         ViewHolder viewHolder;
    37.         final View result;
    38.  
    39.  
    40.         if (convertView == null) {
    41.             viewHolder = new ViewHolder();
    42.             LayoutInflater inflater = LayoutInflater.from(getContext());
    43.             convertView = inflater.inflate(R.layout.list_item, parent, false); //activity_usermatch
    44.             viewHolder.usrName = (TextView) convertView.findViewById(R.id.resultName);
    45.             viewHolder.usrEmail = (TextView) convertView.findViewById(R.id.resultEmail);
    46.             viewHolder.usrSchool = (TextView) convertView.findViewById(R.id.resultSchool);
    47.             result = convertView;
    48.  
    49.             convertView.setTag(viewHolder);
    50.         } else {
    51.             viewHolder = (ViewHolder) convertView.getTag();
    52.             result = convertView;
    53.         }
    54.         Animation animation = AnimationUtils.loadAnimation(mContext, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
    55.         result.startAnimation(animation);
    56.         lastPosition = position;
    57.         viewHolder.usrName.setText(user.getName());
    58.         viewHolder.usrEmail.setText(user.getEmail());
    59.         viewHolder.usrSchool.setText(user.getSchool());
    60.  
    61.  
    62.         return result; //convertView
    63.     }
    64. }
    Main Activity for the function:
    Code (Text):
    1.  
    2. public class UserMatchActivity extends AppCompatActivity{
    3.     private static final String TAG = UserMatchActivity.class.getSimpleName();
    4.     private ProgressDialog pDialog;
    5.     private SessionManager session;
    6.     private SQLiteHandler db;
    7.     public ArrayList<User> matchList;
    8.     ListView listView;
    9.     private static ResultAdapter adapter;
    10.  
    11.  
    12.     @Override
    13.     public void onCreate(Bundle savedInstanceState) {
    14.         super.onCreate(savedInstanceState);
    15.         matchList = new ArrayList<>();
    16.         setContentView(R.layout.activity_match_main); //activity_match_main  activity_usermatch
    17.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    18.  
    19.         // Progress dialog
    20.         pDialog = new ProgressDialog(this);
    21.         pDialog.setCancelable(false);
    22.  
    23.         // Session manager
    24.         session = new SessionManager(getApplicationContext());
    25.  
    26.         // SQLite database handler
    27.         db = new SQLiteHandler(getApplicationContext());
    28.         HashMap<String, String> user = db.getUserDetails();
    29.         String name = user.get("name");
    30.         String email = user.get("email");
    31.         final String school = user.get("school");
    32.  
    33.         //get name,email,school of all matches who go to the same school
    34.  
    35.         String tag_string_req = "get_matches";
    36.         StringRequest strReq = new StringRequest(Request.Method.POST,
    37.                 AppConfig.URL_SEARCH, new Response.Listener<String>() { //{
    38.             @Override
    39.             public void onResponse(String response) {  //JSONObject response
    40.                 hideDialog();
    41.  
    42.                 try {
    43.                     JSONArray jObj = new JSONArray(response);
    44.                     boolean error = false;
    45.                     String errortest = String.valueOf(error);
    46.                     Log.d("error", errortest);
    47.                     error = false;
    48.  
    49.                     if (!error) {
    50.                         for (int i = 0; i < jObj.length(); i++) {
    51.                             JSONObject childJSONObject = jObj.getJSONObject(i);
    52.                             String name = childJSONObject.getString("name");
    53.                             //Log.d("name", String.valueOf(name));
    54.                             String email = childJSONObject.getString("email");
    55.                             //Log.d("email", String.valueOf(email));
    56.                             String sch = childJSONObject.getString("school");
    57.                             //Log.d("school", String.valueOf(sch));
    58.                             User usr = new User(name, email, sch);
    59.                             matchList.add(usr);
    60.  
    61.                         }
    62.                         //Testing to verify matchList my arrayList isn't empty, and it isn't
    63.                         String koala = Integer.toString(matchList.size());
    64.                         Log.d("testing123", koala);
    65.  
    66.                     } else {
    67.                         String errorMsg = ("brokelol");
    68.                         Toast.makeText(getApplicationContext(),
    69.                                 errorMsg, Toast.LENGTH_LONG).show();
    70.                     }
    71.                 } catch (JSONException e) {
    72.                     e.printStackTrace();
    73.                     Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
    74.                 }
    75.  
    76.             }
    77.  
    78.         }, new Response.ErrorListener() {
    79.  
    80.             @Override
    81.             public void onErrorResponse(VolleyError error) {
    82.                 Log.e(TAG, "Match Error: " + error.getMessage());
    83.                 Toast.makeText(getApplicationContext(),
    84.                         error.getMessage(), Toast.LENGTH_LONG).show();
    85.                 hideDialog();
    86.             }
    87.         }) {
    88.  
    89.             @Override
    90.             protected Map<String, String> getParams() {
    91.                 // Posting parameters to search url
    92.                 Map<String, String> params = new HashMap<>();
    93.                 params.put("school", school);
    94.                 return params;
    95.             }
    96.  
    97.         };
    98.         AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    99.  
    100.         adapter = new ResultAdapter(matchList, getApplicationContext());
    101.         listView.setAdapter(adapter);
    102.  
    103.         listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    104.             @Override
    105.                     public void onItemClick(AdapterView<?> parent, View view, int position, long id){
    106.                 User users = matchList.get(position);
    107.                 Snackbar.make(view, "Name: "+users.getName()+"\n"+"School: "+users.getSchool()+"\n"+"Email: "+users.getEmail(), Snackbar.LENGTH_LONG)
    108.                         .setAction("No action", null).show();
    109.             }
    110.         });
    111.  
    112.     }
    113.  
    114.     @Override
    115.     public boolean onCreateOptionsMenu(Menu menu) {
    116.         // Inflate the menu; this adds items to the action bar if it is present.
    117.         getMenuInflater().inflate(R.menu.menu_main, menu);
    118.         return true;
    119.     }
    120.  
    121.     @Override
    122.     public boolean onOptionsItemSelected(MenuItem item) {
    123.         // Handle action bar item clicks here. The action bar will
    124.         // automatically handle clicks on the Home/Up button, so long
    125.         // as you specify a parent activity in AndroidManifest.xml.
    126.         int id = item.getItemId();
    127.  
    128.         //noinspection SimplifiableIfStatement
    129.         if (id == R.id.action_settings) {
    130.             return true;
    131.         }
    132.  
    133.         return super.onOptionsItemSelected(item);
    134.     }
    135.  
    136.     private void hideDialog() {
    137.         if (pDialog.isShowing())
    138.             pDialog.dismiss();
    139.     }
    140. }
    141.  
    Here is my XML code.
    menu_main.xml:
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <menu xmlns:android="http://schemas.android.com/apk/res/android"
    4.     xmlns:tools="http://schemas.android.com/tools"
    5.     android:layout_width="match_parent"
    6.     android:layout_height="wrap_content"
    7.     tools:context=".UserMatchActivity">
    8.     <item android:id="@+id/action_settings"
    9.         android:title="action_settings"
    10.         android:orderInCategory="100"  />
    11.     <ListView
    12.         android:id="@+id/list"
    13.         android:layout_width="wrap_content"
    14.         android:layout_height="wrap_content"
    15.         />
    16. </menu>
    17.  
    activity_usermatch.xml:
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <ListView xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:orientation="vertical" android:layout_width="match_parent"
    5.     android:layout_height="wrap_content"
    6.     android:id="@+id/listMatches">
    7.  
    8.     <LinearLayout
    9.         android:layout_width="fill_parent"
    10.         android:layout_height="wrap_content"
    11.         android:layout_gravity="center"
    12.         android:orientation="vertical"
    13.         android:paddingLeft="20dp"
    14.         android:paddingRight="20dp"
    15.         android:id="@+id/MatchLayout">
    16.         <ProgressBar
    17.             android:id="@+id/progressBar"
    18.             android:layout_width="fill_parent"
    19.             android:layout_height="wrap_content"
    20.             style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>
    21.  
    22.  
    23.     </LinearLayout>
    24. </ListView>
    25.  
    list_item.xml:
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:layout_width="wrap_content"
    5.     android:layout_height="wrap_content">
    6.     <TextView
    7.         android:id="@+id/resultName"
    8.         android:layout_width="wrap_content"
    9.         android:layout_height="wrap_content"
    10.         android:text="Name"
    11.         />
    12.     <TextView
    13.         android:id="@+id/resultEmail"
    14.         android:layout_width="wrap_content"
    15.         android:layout_height="wrap_content"
    16.         android:text="Email"
    17.         />
    18.     <TextView
    19.         android:id="@+id/resultSchool"
    20.         android:layout_width="wrap_content"
    21.         android:layout_height="wrap_content"
    22.         android:text="School"
    23.         />
    24.  
    25. </LinearLayout>
    26.  
    And here is the User class:
    Code (Text):
    1.  
    2. public class User {
    3.     public String usrname;
    4.     public String usremail;
    5.     public String usrschool;
    6.  
    7.     public User(JSONObject object) {
    8.         try {
    9.             this.usrname = object.getString("name");
    10.             this.usremail = object.getString("email");
    11.             this.usrschool = object.getString("school");
    12.         } catch (JSONException e) {
    13.             e.printStackTrace();
    14.         }
    15.     }
    16.     public User(String name, String email, final String school){
    17.         this.usrname = name;
    18.         this.usremail = email;
    19.         this.usrschool = school;
    20.     }
    21.  
    22.     /*public User(String name, String email, String school) {
    23.         this.usrname = name;
    24.         this.usremail = email;
    25.         this.usrschool = school;}*/
    26.  
    27.     public void setUsrname(final String name) {
    28.         usrname = name;
    29.     }
    30.  
    31.     public void setUsremail(final String email) {
    32.         usremail = email;
    33.     }
    34.  
    35.     public void setUsrschool(final String school) {
    36.         usrschool = school;
    37.     }
    38.  
    39.     public String getName() {
    40.         return usrname;
    41.     }
    42.  
    43.     public String getEmail() {
    44.         return usremail;
    45.     }
    46.  
    47.     public String getSchool() {
    48.         return usrschool;
    49.     }
    50.  
    51.  
    52. }
    53.  
    Any input would be highly appreciated! Like I said, I have been struggling with this for quite some time now and it is driving me crazy! Also, I am not trying to be a mooch. I will gladly help out other members on the forum once I get this figured out and get my sanity back, as this seems like a great place. Please and thanks!
     

    Advertisement

    #1 specizripn, Sep 11, 2017
    Last edited: Sep 12, 2017
  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #10
    Points:
    1,988
    Posts:
    8,521
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    8,521
    12,692
    1,988
    Male
    Software developer
    South West of England
    Simply put, you're creating your ListAdapter before the matchList array has been populated.
    This line constructs your ListAdapter -

    Code (Text):
    1.  
    2. adapter = new ResultAdapter(matchList, getApplicationContext());
    3.  
    I would put money on the fact that the ResultAdapter constructor is called when matchList has nothing in it.
    Why is this?
    Well the point at which you populate the list is done in the response method for your HTTP request. The request is made asynchronously, so any code after you initiated the request is executed immediately, without waiting for the response.

    To confirm this, put a breakpoint in the ResultAdapter constructor method, and check the contents of the matchList parameter.

    What to do? Well in your onResponse() method, after populating matchList, you should tell the ListAdapter that the underlying data has changed. Do this by calling

    Code (Text):
    1.  
    2. adapter.notifyDataSetChanged()
    3.  
     
  3. specizripn

    specizripn Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Sep 11, 2017

    Sep 11, 2017
    5
    0
    5
    When I put breakpoints it said that data was size 0, this.mContext = null and this.users = null. I added adapter.notifyDataSetChanged(); after I constructed the adapter in my OnRespones() method, but it still isn't displaying. I'm going to use FindBugs to see if I can find a static cause for my issues.
     
  4. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #10
    Points:
    1,988
    Posts:
    8,521
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    8,521
    12,692
    1,988
    Male
    Software developer
    South West of England
    Here's a suggestion: Create your ListAdapter with some hardcoded test data. If it doesn't work with this setup, you're doing something fundamentally wrong.
     
    specizripn likes this.
  5. specizripn

    specizripn Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Sep 11, 2017

    Sep 11, 2017
    5
    0
    5
    I hardcoded some data into my ArrayList and it displays! So now I just have to play around with the code that populates the ArrayList from database data.
     
    #5 specizripn, Sep 19, 2017
    Last edited: Sep 19, 2017
  6. specizripn

    specizripn Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Sep 11, 2017

    Sep 11, 2017
    5
    0
    5
    I got it working! I added:
    Code (Text):
    1.  
    2. adapter.notifyDataSetChanged();
    3.  
    at the bottom of the loop where I populate the ArrayList! Thank you so much for all of your help LV426!!!!!
     

Share This Page

Loading...