我正在尝试显示一个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>
我在日志中收到所有必需的消息,以及我将文本颜色设置为白色的原因是因为应用程序的背景为黑色。
我在这里做错了什么? 谢谢!
答案 0 :(得分:0)
您正在编写的ListView适配器目前不知道您的数据来源是什么..
@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;
}
适配器有一个名为getCount()
的方法,您应该覆盖该方法,并提供它应显示的项目数。您提供的此号码用于为您覆盖和实施的int position
方法中的getView()
提供信息。现在,适配器认为你没有数据流入它(即getCount()
返回0)所以它不加载任何东西。要解决这个问题,最好将要显示的所有数据合并到自定义类中,如Abhiroj Panwar上面所述,将它们添加到ArrayList中,然后在getView中使用{{1访问每组数据在int position
方法中给出。
getView()