根据条件显示列表视图

Show listview depending on condition

我有一个自定义适配器,现在可以正确显示我所有的联系人。我也在检查 jsonobject 中的一些条件以在列表视图中显示一些图像。这很好用。

现在我想实现的不是显示一个或另一个图像,而是显示或不显示整行。

判断条件的部分如下

JSONObject c;
                try {
                    c = android.getJSONObject(position);
                    error = c.getString(TAG_ERROR);
                    if(Integer.parseInt(c.getString(TAG_ERROR)) == 0) {contactViewHolder.imgTiene
                        .setImageResource(R.drawable.ic_launcher);}
                    if(Integer.parseInt(c.getString(TAG_ERROR)) == 1) {contactViewHolder.imgTiene
                        .setImageResource(R.drawable.ic_action_cancel);}
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

所以如果结果是 1,我不想显示该行。

列表视图的代码如下

@Override public View getView(int position, View convertView, ViewGroup parent) { // TODO 自动生成的方法存根

    // View MyView = convertView;

    ContactViewHolder contactViewHolder;

    if (convertView == null) {

        // Inflate the layout
        LayoutInflater li = getLayoutInflater();
        convertView = li.inflate(
                R.layout.contactos_list_item, null);

        contactViewHolder = new ContactViewHolder();

        contactViewHolder.imgContact = (ImageView) convertView
                .findViewById(R.id.flag);
        contactViewHolder.txtViewContactName = (TextView) convertView
                .findViewById(R.id.txtView_name);
        contactViewHolder.txtViewPhoneNumber = (TextView) convertView
                .findViewById(R.id.txtview_number);
        //contactViewHolder.txtViewError = (TextView) convertView
          //      .findViewById(R.id.txtview_check);
        contactViewHolder.imgTiene = (ImageView) convertView
                .findViewById(R.id.flag2);

        convertView.setTag(contactViewHolder);
    } else {
        contactViewHolder = (ContactViewHolder) convertView.getTag();
    }

        cur.moveToPosition(position);

        name = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

        if (name != null)
            contactViewHolder.txtViewContactName.setText(name);
        else
            contactViewHolder.txtViewContactName.setText("Unknown");


        // Add Phone Number //

        String phoneNumber = cur
                .getString(cur
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        if (phoneNumber != null)
            contactViewHolder.txtViewPhoneNumber.setText(phoneNumber);
        else
            contactViewHolder.txtViewPhoneNumber.setText("Unknown");

        Uri uri = getPhotoUri(Long.parseLong(fetchContactIdFromPhoneNumber(phoneNumber)));


        if (uri != null) {
            contactViewHolder.imgContact.setImageURI(uri);
            if (contactViewHolder.imgContact.getDrawable() == null){contactViewHolder.imgContact.setImageResource(R.drawable.ic_action_person);}
        } else {
            contactViewHolder.imgContact
                    .setImageResource(R.drawable.ic_action_person);
        }

        JSONObject c;
        try {
            c = android.getJSONObject(position);
            error = c.getString(TAG_ERROR);
            if(Integer.parseInt(c.getString(TAG_ERROR)) == 0) {contactViewHolder.imgTiene
                .setImageResource(R.drawable.ic_launcher);}
            if(Integer.parseInt(c.getString(TAG_ERROR)) == 1) {contactViewHolder.imgTiene
                .setImageResource(R.drawable.ic_action_cancel);}
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //contactViewHolder.txtViewError.setText(error);


    return convertView;
}

我应该如何调节 returned 的内容? 我尝试使用布尔值和设置和 if() 到 return convertView 但它不允许它。

进一步搜索我发现您可以更改充气

if(condition)
{
  convertView=layoutInflater.inflate(R.layout.row_null,null);
  return convertView;
}
else
{
   convertView=layoutInflater.inflate(R.layout.row_content,null);
   return convertView;
}

我应该把这个 if() 放在哪里?

感谢大家的帮助

这是一个更好的方法。不得不亲自测试它,不确定它是否有效,但在看到我们可以使其不可见后,我认为这应该有效。

  if (result == 1) {
      ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
       params.height = 0;
       convertView.setLayoutParams(params);  
 }

这可能有效

在 Layout 文件夹中创建一个 null_item 并 return 它虽然像在

中一样膨胀
if((Integer.parseInt(c.getString(TAG_ERROR)) == 1)
{
return inflater.inflate(R.layout.null_item, null);
}
return convertView;

null_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</LinearLayout>