我想顯示一個應該包含發件人號碼和短信主體的列表視圖。我正在爲此創建一個自定義適配器。ListView在設置自定義列表視圖適配器後不顯示
這裏是我創建和設置自定義適配器(MapInbox.java)的類代碼。
View baseView;
ArrayList<String> sms_id = new ArrayList<>();
ArrayList<String> sms_num = new ArrayList<>();
ArrayList<String> sms_Name = new ArrayList<>();
ArrayList<String> sms_dt = new ArrayList<>();
ArrayList<String> sms_body = new ArrayList<>();
String smsBody, senderNumber;
ListView lvSms;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
baseView = inflater.inflate(R.layout.fragment_map_inbox, container, false);
lvSms = (ListView) baseView.findViewById(R.id.lv_sms);
Uri myMessage = Uri.parse("content://sms/");
ContentResolver cr = getActivity().getContentResolver();
Cursor c = cr.query(myMessage, new String[]{"_id", "address", "date",
"body", "read"}, "address = '03414503584'", null, null);
getActivity().startManagingCursor(c);
getSmsLogs(c);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
CustomMessagesAdapter customMessagesAdapter = new CustomMessagesAdapter
(getActivity(), R.layout.listview_messages);
lvSms.setAdapter(customMessagesAdapter);
}
}, 150);
return baseView;
}
public void getSmsLogs(Cursor c) {
if (sms_num.size() > 0) {
sms_id.clear();
sms_num.clear();
sms_Name.clear();
sms_body.clear();
sms_dt.clear();
}
try {
if (c.moveToFirst()) {
do {
if (c.getString(c.getColumnIndexOrThrow("address")) == null) {
c.moveToNext();
continue;
}
senderNumber = c.getString(
c.getColumnIndexOrThrow("address"));
smsBody = c.getString(c.getColumnIndexOrThrow("body"));
Log.e("Body-->", "" + smsBody);
Log.e("Number-->", "" + senderNumber);
sms_num.add(senderNumber);
sms_body.add(smsBody);
} while (c.moveToNext());
}
c.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private class CustomMessagesAdapter extends ArrayAdapter<String> {
CustomMessagesAdapter(Context context, int resource) {
super(context, resource);
}
@NonNull
@Override
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.listview_messages, parent, false);
viewHolder.tvSenderNumber = (TextView) convertView.findViewById(R.id.tv_sender_number);
viewHolder.tvMessage = (TextView) convertView.findViewById(R.id.tv_message);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tvSenderNumber.setText(senderNumber);
viewHolder.tvMessage.setText(smsBody);
return convertView;
}
}
private static class ViewHolder {
TextView tvSenderNumber;
TextView tvMessage;
}
代碼MapInbox佈局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/messages_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/lv_sms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp">
</ListView>
</LinearLayout>
而對於消息自定義的ListView佈局代碼(listview_messages.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:background="@drawable/messages_background"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/tv_sender_number"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:textColor="#FFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="3dp"
android:textColor="#FFF"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
messages_background
<solid android:color="#00000000" />
<stroke
android:width="2dp"
android:color="#FFF" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners
android:bottomLeftRadius="15dip"
android:bottomRightRadius="15dip"
android:topLeftRadius="15dip"
android:topRightRadius="15dip" />
</shape>
我我全力以赴日誌中所需的消息以及我將文本顏色設置爲白色的原因是應用程序的背景爲黑色。
我在這裏做錯了什麼? 謝謝!
我建議你先在一個地方組織你的數據。您可以創建一個包含所有SMS詳細信息的普通舊Java類(即SMSData),然後使用此類來定義ArrayList的類型參數。除此之外,您不會將數據源傳遞到可以工作的適配器。 –