为什么我无法在Android中查看我的Listview?

时间:2016-10-25 15:30:29

标签: android listview

我试图将一些信息从我的片段传递到我的适配器以显示一个ListView,它确实通过但是ListView在完成加载后没有显示任何内容。 我在下面附上我的CustomAdapter类。

package com.example.rama.hello.Adapters;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.rama.hello.Bean.RowItem;
import com.example.rama.hello.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by RAMA on 10/25/2016.
 */
public class CustomAdapter extends ArrayAdapter<RowItem> {

    Context mcontext;
    ArrayList<RowItem> rowItem = new ArrayList<RowItem>();
    private RowItem row;
    RowItem data;


    public CustomAdapter(Context context, int resourceId,
                                 ArrayList<RowItem> items) {
        super(context, resourceId, items);
        this.mcontext = context;
    }

    @Override
    public int getCount()
    {
        return rowItem.size();
    }


    @Override
    public long getItemId(int position) {
        return rowItem.indexOf(getItem(position));
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        if (convertView == null)
        {
            LayoutInflater mInflater = (LayoutInflater) mcontext
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.list_item,parent,false);
        }

        ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
        TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
        TextView txtSubTitle = (TextView) convertView.findViewById(R.id.sub_title);
        TextView txtRightTitle = (TextView) convertView.findViewById(R.id.right_title);

        RowItem row_pos = getItem(position);

        // setting the image resource and title,subtitle,Righttitle
        File imgFile = new File(row_pos.getIcon());

        if(imgFile.exists()){

            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
            imgIcon.setImageBitmap(myBitmap);
        }


        if(row_pos.getTitle() == " ")
        txtTitle.setText(row_pos.getPhone_number());
        else
        txtTitle.setText(row_pos.getTitle());

        txtSubTitle.setText(row_pos.getSub_title());
        txtRightTitle.setText(row_pos.getRight_title());

        return convertView;
    }
}

1 个答案:

答案 0 :(得分:1)

ArrayAdapter中,您不需要获取项目列表的参考(在RowItem的案例列表中),这不是ArrayAdapter的,您只需要覆盖getViewArrayAdapter将在内部为您处理列表,以下是它的外观:

public class CustomAdapter extends ArrayAdapter<RowItem> {


    public CustomAdapter(Context context, int resourceId,
                         ArrayList<RowItem> items) {
      super(context, resourceId, items);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      if (convertView == null) {
        LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
        convertView = mInflater.inflate(R.layout.list_item, parent, false);
      }

      ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
      TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
      TextView txtSubTitle = (TextView) convertView.findViewById(R.id.sub_title);
      TextView txtRightTitle = (TextView) convertView.findViewById(R.id.right_title);

      RowItem row_pos = getItem(position);

      // setting the image resource and title,subtitle,Righttitle
      File imgFile = new File(row_pos.getIcon());

      if (imgFile.exists()) {

        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
        imgIcon.setImageBitmap(myBitmap);
      }


      if (row_pos.getTitle() == " ")
        txtTitle.setText(row_pos.getPhone_number());
      else
        txtTitle.setText(row_pos.getTitle());

      txtSubTitle.setText(row_pos.getSub_title());
      txtRightTitle.setText(row_pos.getRight_title());

      return convertView;
    }
  }