在列表视图中显示满足条件的那些数据?

时间:2016-03-10 05:07:05

标签: android json listview

这是我的自定义列表视图适配器。我的问题很简单,但我坚持这个。我在列表视图中以三种不同的文本视图显示一些数据。文本描述(txtDesc),文本标题和文本title2.I我想只显示那些文字描述为#34;视频" .Data来自URL作为JSON对象使用齐射库。
我尝试了如果条件和比较字符串,但应用程序崩溃。任何帮助将是伟大的。 // Details类是我的getter和setter类

 public class CustomListViewAdapter extends ArrayAdapter<Details> {

Context context;
String vid = "Video";

public CustomListViewAdapter(Context context, int resourceId,
                             List<Details> items) {
    super(context, resourceId, items);
    this.context = context;
}

/*private view holder class*/
private class ViewHolder {
    //ImageView imageView;
    TextView txtTitle;
    TextView txtTitle2;
    TextView txtDesc;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    Details rowItem = (Details) getItem(position);
    //Details rowItem = getItem(position);
    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    String vid = "Video";
    if (convertView == null )
    {
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);

             holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
             holder.txtTitle2 = (TextView) convertView.findViewById(R.id.link);            
              convertView.setTag(holder);
    } else


        holder = (ViewHolder) convertView.getTag();


    holder.txtDesc.setText(rowItem.getResType());
    holder.txtTitle.setText(rowItem.getName());
    holder.txtTitle2.setText(rowItem.getUrl());


    // holder.imageView.setImageResource(rowItem.getImageId());

    return convertView;
}

4 个答案:

答案 0 :(得分:1)

正如你在评论中所说,你正在查看vid,

holder.txtDesc.equals(vid)

但是holder.textDescTextView所以我认为你必须像这样检查

rowItem.getResType().toString().equals(vid)

<强>更新

像这样重新格式化您的代码

if (convertView == null || convertView.getTag() == null) {
    LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = vi.inflate(R.layout.list_item, null); //TODO: parent instead of null?
    holder = new ViewHolder();
    //Your initializations
    convertView.setTag(holder);
} else {
    holder = (ViewHolder) convertView.getTag();
}

答案 1 :(得分:1)

检查你的这个getView方法。

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        Details rowItem = (Details) getItem(position);
        //Details rowItem = getItem(position);
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        String vid = "Video";
        if (convertView == null )
        {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);

                 holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
                 holder.txtTitle2 = (TextView) convertView.findViewById(R.id.link);            
                  convertView.setTag(holder);
        } else


            holder = (ViewHolder) convertView.getTag();


        holder.txtDesc.setText(rowItem.getResType());
        holder.txtTitle.setText(rowItem.getName());
        holder.txtTitle2.setText(rowItem.getUrl());
        String url=rowItem.getUrl();
        if(url.contains("video")){
        holder.txtDesc.setvisibility(View.VISIBLE)
        holder.txtTitle.setvisibility(View.VISIBLE)
        holder.txtTitle2.setvisibility(View.VISIBLE)
        }else{
        holder.txtDesc.setvisibility(View.GONE)
        holder.txtTitle.setvisibility(View.GONE)
        holder.txtTitle2.setvisibility(View.GONE)
        }


        // holder.imageView.setImageResource(rowItem.getImageId());

        return convertView;
    }

答案 2 :(得分:1)

  1. 将一组数组传递给Adapter时,适配器大小将等于您的项目。而在getView()/其他方式,你不能跳过一个项目,因为它不符合你的条件。
  2. 解决方案:您可以做的是在将项目传递给适配器之前,使用您的条件过滤列表并将其设置为适配器。

答案 3 :(得分:0)

试试这段代码:

    if(rowItem.getResType().equalsIgnoreCase("video")){
    holder.txtDesc.setvisibility(View.VISIBLE);
    holder.txtTitle.setvisibility(View.VISIBLE);
    holder.txtTitle2.setvisibility(View.VISIBLE);
    holder.txtDesc.setText(rowItem.getResType());
    holder.txtTitle.setText(rowItem.getName());
    holder.txtTitle2.setText(rowItem.getUrl());
    }else{
    holder.txtDesc.setvisibility(View.GONE);
    holder.txtTitle.setvisibility(View.GONE);
    holder.txtTitle2.setvisibility(View.GONE);}