gpt4 book ai didi

android - 列表未在 android 中显示

转载 作者:行者123 更新时间:2023-11-29 01:28:29 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com