【发布时间】:2015-12-11 14:45:59
【问题描述】:
我正在使用以下代码填充列表。
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;
}
}
但是列表没有显示。为什么 ?错误在哪里?我该如何解决这个问题?
【问题讨论】:
-
logcat 中没有任何内容。
-
通过添加Log检查打印列表大小并检查列表是否为空。尝试调试它
-
适配器内
smsBody的打印尺寸 -
列表大小为 26。
-
把“if (convertView == null)”改成if (view== null),检查一下。