android - 列表未在 android 中显示

标签 android list listview

我正在通过以下代码填充列表。

public void refreshSmsInbox() {
        ContentResolver contentResolver = getContentResolver();
        Cursor smsInboxCursor = contentResolver.query(
                Uri.parse("content://sms/inbox"), null, null, null, null);
        int indexBody = smsInboxCursor.getColumnIndex("body");
        int indexAddress = smsInboxCursor.getColumnIndex("address");
        if (indexBody < 0 || !smsInboxCursor.moveToFirst())
            return;
        arrayAdapter.clear();
        List<String> smsBody = new ArrayList<String>();
        String fromNumber = "";
        do {
            if (pre_address.equals(smsInboxCursor.getString(indexAddress))) {
                String str = "SMS From: "
                        + smsInboxCursor.getString(indexAddress) + "\n"
                        + smsInboxCursor.getString(indexBody) + "\n";
                fromNumber = smsInboxCursor.getString(indexAddress);
                smsBody.add(smsInboxCursor.getString(indexBody));
            //  arrayAdapter.add(str);
            }
        } while (smsInboxCursor.moveToNext());
          arrayAdapter = new SmsArrayAdapter(this,R.layout.row_item,smsBody,fromNumber);
          smsListView.setAdapter(arrayAdapter);
    }

SmsArrayAdapter 的代码如下:

public class SmsArrayAdapter extends ArrayAdapter<String> {

    List<String> smsBody;
    List<Boolean> Status;
    Context context;
    private static LayoutInflater inflater = null;
    String fromNumber ; 

    public SmsArrayAdapter(Context context, int resource, List<String> smsBody,
             String fromNumber) {
        super(context, resource);
        this.smsBody = smsBody;
    //  this.Status = Status;
        this.context = context;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.fromNumber = fromNumber;
    }

     public static class ViewHolder{

         public TextView textfrom;
         public TextView text_sms; 

     }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        super.getView(position, convertView, parent);

        View view = convertView;
        ViewHolder holder;

        if (convertView == null) {

            /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
            view = inflater.inflate(R.layout.row_item, null);

            /****** View Holder Object to contain tabitem.xml file elements ******/

            holder = new ViewHolder();
            holder.textfrom = (TextView) view.findViewById(R.id.textView_from);
            holder.textfrom.setText(" SMS FROM "+fromNumber);
            holder.text_sms = (TextView) view.findViewById(R.id.textView_sms);
            holder.text_sms.setText(smsBody.get(position));


            /************ Set holder with LayoutInflater ************/
            view.setTag(holder);
        } else
            holder = (ViewHolder) view.getTag();

        return view;
    }

}

但是列表没有显示。为什么 ?错误在哪里?我该如何解决这个问题?

最佳答案

在您的 SmsArrayAdapter 构造函数中删除它

 super(context, resource);

添加这个

 super(context, resource,smsBody);

这由 ArrayAdapter 父类(super class)使用。否则列表总是空的。在您使用它之前,它不会调用您的 getView。

public class SmsArrayAdapter extends ArrayAdapter<String> {


    List<String> smsBody;
    List<Boolean> Status;
    Context context;
    private LayoutInflater inflater = null;
    String fromNumber ; 

    public SmsArrayAdapter(Context context, int resource, List<String> smsBody,
             String fromNumber) {
        super(context, resource,smsBody);
        this.smsBody = smsBody;
    //  this.Status = Status;
        this.context = context;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.fromNumber = fromNumber;               
    }  

     public class ViewHolder{

         public TextView textfrom;
         public TextView text_sms; 

     }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        // TODO Auto-generated method stub


        ViewHolder holder;

        if (view == null) {

            /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
            view = inflater.inflate(R.layout.row_item, null);

            /****** View Holder Object to contain tabitem.xml file elements ******/

            holder = new ViewHolder();
            holder.textfrom = (TextView) view.findViewById(R.id.textView_from);

            holder.text_sms = (TextView) view.findViewById(R.id.textView_sms);              


            /************ Set holder with LayoutInflater ************/
            view.setTag(holder);
        } else
            holder = (ViewHolder) view.getTag();
        holder.textfrom.setText(" SMS FROM "+fromNumber);
        holder.text_sms.setText(smsBody.get(position));
        return view;
    }

}

关于android - 列表未在 android 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32584461/

相关文章:

android - 如何提示用户更新应用程序,以及当用户点击更新时它应该在后台更新

android - 作业调度程序无法取消系统 uid 的作业

python - 在 python 中查找另一个项目最接近列表中哪两个元素的最快方法

python - 确保列表的所有元素都不同的最 pythonic 方法是什么?

python - 如果只有 int 1 是 [1, 2.3, '' ] 中的列表项,为什么 Python '' return ' in operator 'blabla' True' for float 1.0 ?

带标题的Android分类 ListView

android - 如何向代码库追溯添加测试?

android - 回收站 View : UnsupportedOperationException

c# - UpdatePanel 中 ListView 中的 LinkBut​​ton 导致完全回发

c# - 仅当 ListView 不为空(有项目)时才使 TextBox 可见