如何在customadapter中隐藏其值为null的textview

时间:2016-05-16 10:54:31

标签: android null custom-adapter

请告诉我如何在有时通过自定义适配器返回null值时隐藏textview。以下是我的代码。 Android代码:

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();
    }

我在列表视图中显示此结果。

3 个答案:

答案 0 :(得分:3)

或者只是覆盖 getView(....)方法,如下例

    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
        }
    };

我更喜欢null empty checkonData(new BoundedMatcher<Object, JSONObject>(JSONObject.class) { @Override public void describeTo(Description description) { description.appendText("Matching to JSONObject"); } @Override protected boolean matchesSafely(JSONObject jsonObject) { try { return "FIU-MMC Blood Drive".equals(jsonObject.getString("title")); } catch (JSONException e) { return false; } } }).inAdapterView(withId(R.id.homeListViewList)).check(matches(isDisplayed()));

  

如果字符串为null或0-length,则返回true。

答案 1 :(得分:1)

在适配器

中使用以下代码
 if(textView.getText().toString()==null || textView.getText().toString().isEmpty() ){
    textView.setVisibility(View.GONE);
    }
else 
{
        textView.setText(searchList.get(position).get(TAG_TITLE));
}

答案 2 :(得分:0)

您需要创建自定义适配器,然后在getView()方法内,您可以根据商品值更改TextView可见性。

public View getView (int position, View convertView, ViewGroup parent){
    //...
    if( yourItem == null ){
        textView.setVisibility(View.INVISIBLE); // or GONE if you want
    } else {
        textView.setVisibility(View.VISIBLE);
    }
    //....
}