【发布时间】:2017-05-12 19:41:58
【问题描述】:
我正在尝试显示一个列表视图,它应该包含发件人号码和短信正文。为此,我正在创建一个自定义适配器。
这是我在其中创建和设置自定义适配器(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 的类型参数。除此之外,您没有将数据源传递给它可以工作的适配器。
标签: android xml android-layout listview android-arrayadapter