【问题标题】:Show listview depending on condition根据条件显示列表视图
【发布时间】:2015-12-22 07:50:56
【问题描述】:

我有一个自定义适配器,现在可以正确显示我的所有联系人。我还在检查 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,我不希望显示该行。

listview的代码如下

@覆盖 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;
}

我应该如何调节返回的内容? 我尝试使用布尔值和设置以及 if() 返回 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() 放在哪里?

感谢大家的帮助

【问题讨论】:

  • 刚刚做了花药尝试。

标签: android listview custom-adapter


【解决方案1】:

这可能有效

在 Layout 文件夹中创建一个 null_item 并通过 inlfate 将其返回

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>

【讨论】:

  • 感谢您的帮助,我添加了 convertView.setVisibility((Integer.parseInt(c.getString(TAG_ERROR)) == 1) ? View.GONE : View.VISIBLE);在 try() 中,现在我得到空行,我怎样才能让它们根本不显示?
【解决方案2】:

这里有一个更好的方法。必须自己测试它,不确定它是否有效,但在看到我们可以让它不可见后,我认为这应该有效。

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

【讨论】:

  • 谢谢@ashley-alvarado 我不需要它们。我如何删除它们?你能写一个 if() 吗?我有一个布尔值“a”设置为真假。 if(a){ // 不要删除 } else { // 删除 }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多