Search code examples
androidnullcustom-adapter

how to hide textview whose value is null in customadapter


Please tell me how to hide the textview when it sometimes return null value through custom adapter. Here below is my code. Android code:

public void showList() {
    try {
        JSONObject jsonObj = new JSONObject(myJSON);
        search = jsonObj.getJSONArray(TAG_RESULT);
        for (int i = 0; i < search.length(); i++) {
            JSONObject c = search.getJSONObject(i);
            String title = c.getString(TAG_TITLE);
            String phone = c.getString(TAG_PHONE);
            String email = c.getString(TAG_EMAIL);
            String description = c.getString(TAG_DESCRIPTION);
            String postDate = c.getString(TAG_DATE);
            String username=c.getString(TAG_USERNAME);
            String city=c.getString(TAG_CITY);
            String locality= c.getString(TAG_LOCALITY);
            HashMap<String, String> search = new HashMap<String, String>();

            search.put(TAG_TITLE, title);
            search.put(TAG_PHONE, phone);
            search.put(TAG_EMAIL, email);
            search.put(TAG_DESCRIPTION, description);
            search.put(TAG_DATE, postDate);
            search.put(TAG_USERNAME, username);
            search.put(TAG_CITY, city);
            search.put(TAG_LOCALITY, locality); /* in some case it is null...at that time i want to hide tvlocality textview.*/

            searchList.add(search);


        }



            ListAdapter adapter = new SimpleAdapter(
                    ResultDetail.this, searchList, R.layout.activity_show__result,
                    new String[]{TAG_TITLE, TAG_PHONE, TAG_EMAIL, TAG_DESCRIPTION, TAG_DATE, TAG_USERNAME, TAG_CITY, TAG_LOCALITY},
                    new int[]{R.id.tvTitle, R.id.tvMobile, R.id.tvEmail, R.id.tvDesp, R.id.tvDate, R.id.tvUserName, R.id.tvCityName, R.id.tvLocality}
            );

            listView.setAdapter(adapter);

    } catch (JSONException e) {
        e.printStackTrace();
    }

i am showing this result in listview .


Solution

  • Or simply override the getView(....) method like below example

        ListAdapter adapter = new SimpleAdapter(this, searchList, R.layout.your_adapter_view, new String[]{"city"
        }, new int[]{R.id.city}) {
            @Override
    
            public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                ViewHolder holder;
                if (v == null) {
                    holder = new ViewHolder();
                    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.your_adapter_view, null);
                    holder.textView = (TextView) v.findViewById(R.id.city);
                    //other stuff
                    v.setTag(holder);
                } else {
                    holder = (ViewHolder) v.getTag();
                }
    
                Map<String, String> data = searchList.get(position);
    
                if (!TextUtils.isEmpty(data.get("city"))) {
                    holder.textView.setText(data.get("city"));
                    holder.textView.setVisibility(View.VISIBLE);
                } else {
                    holder.textView.setVisibility(View.GONE);
                }
                //do the same thing for other possible views.
                return v;
            }
    
            class ViewHolder {
                TextView textView;
                //your other views
            }
        };
    

    I prefer TextUtils.isEmpty(str) for null and empty check.

    Returns true if the string is null or 0-length.