2017-05-12 70 views
0

我想顯示一個應該包含發件人號碼和短信主體的列表視圖。我正在爲此創建一個自定義適配器。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

我建議你先在一個地方組織你的數據。您可以創建一個包含所有SMS詳細信息的普通舊Java類(即SMSData),然後使用此類來定義ArrayList的類型參數。除此之外,您不會將數據源傳遞到可以工作的適配器。 –

回答

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,您將覆蓋並執行該方法。現在,適配器認爲你沒有數據彙集到它(即getCount()返回0),所以它不加載任何東西。爲了解決您鞏固所有你想顯示到像Abhiroj Panwar自定義類的數據上面說的,把它們添加到一個ArrayList,然後在getView訪問每組使用在getView()方法給出的int position數據的這個問題,是最好的。

viewHolder.tvSenderNumber.setText(yourCustomArrayListClass.get(position).senderNumber); 
viewHolder.tvMessage.setText(yourCustomArrayListClass.get(position).smsBody); 
相關問題