簡體   English   中英

為什么 RecyclerView 沒有顯示項目?

[英]Why no items displayed by RecyclerView?

在問這個問題之前,我盡可能仔細地查看了以下內容:

Android - RecyclerView 數據未顯示

RecyclerView 沒有顯示任何項目

回收站視圖不顯示任何內容

android recyclerview 不顯示項目

我盡量遵循上面的建議。 但是,我的應用程序的 RecyclerView 中仍然沒有顯示任何項目。

下面,您將找到我的代碼。 請注意:

1)我只包含了我認為是我的代碼的相關部分

2) 我正在使用 GreenRobot EventBus 來更新適配器。 這就是 onEvent 方法的作用。

3) 運行應用程序時,會生成以下代碼中的所有日志語句(操作前/操作后)。 我查看了 logcat。 我找不到任何相關的警告/錯誤。

在這一點上,我不知所措。 我覺得好像我錯過了一些非常明顯/基本的東西。 我只是不知道是什么。 UI 不是我的專長。 對於解決此問題的任何建議,我將不勝感激。

謝謝,

代碼

public class MainActivity extends FragmentActivity
{

private UserLocationInformationAdapter mUserLocationInformationAdapter;
private RecyclerView recyclerView;

private ArrayList<Classes.UserLocationInformation> _mWifidetails;


@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.wifiData);


    init();


}

private void init()
{

    _mWifidetails = new ArrayList<Classes.UserLocationInformation>();

    Log.i(TAG, " - init - _mWifidetails.size() - "+_mWifidetails.size());

    mUserLocationInformationAdapter = new UserLocationInformationAdapter(MainActivity.this, _mWifidetails);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(mUserLocationInformationAdapter);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(MainActivity.this);
    recyclerView.setLayoutManager(mLayoutManager);
    mUserLocationInformationAdapter.notifyDataSetChanged();

    Log.i(TAG, " - init - Recycler view set up --- ");

}

@Subscribe
public void onEvent(MessageEvent event)
{

    wifi_data = new ArrayList<>();

    Log.i(TAG, " - onEvent - Event posted. Taking action --- ");

    _mWifidetails = event.getMwifiModal();

    Log.i(TAG, " - onEvent - _mWifidetails.size() - "+_mWifidetails.size());

    mUserLocationInformationAdapter.notifyDataSetChanged();

    Log.i(TAG, " - onEvent - Event posted. Action completed --- ");

}

public static class UserLocationInformationAdapter extends RecyclerView.Adapter<UserLocationInformationAdapter.ViewHolder>
{
    private ArrayList<UserLocationInformation> mDataset;
    private LayoutInflater mInflater;
    private WifiDetails.ItemClickListener mClickListener;

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
    {

        public TextView ssidTV , wifiLevelTV, disTextView;


        ViewHolder(View textView)
        {
            super(textView);
            ssidTV = (TextView) textView.findViewById(R.id.ssidTV);
            wifiLevelTV = (TextView) textView.findViewById(R.id.wifi_level);
            disTextView = (TextView) textView.findViewById(R.id.distanceTv);

        }

        @Override
        public void onClick(View view)
        {
            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
        }

    }

     public UserLocationInformationAdapter(Context context, ArrayList<UserLocationInformation> myDataset)
    {
        this.mInflater = LayoutInflater.from(context);
        mDataset = myDataset;
    }

    @Override
    public UserLocationInformationAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                                          int viewType)
    {
         TextView v = (TextView) mInflater
                .inflate(R.layout.wifi_detail_adapter, parent, false);

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position)
    {

        String address = "Address: " + mDataset.get(position).address;
        String latitude = "Latitude: " + mDataset.get(position).lat;
        String longitude = "Longitude: " + mDataset.get(position).lon;

       //SHOULD THE ABOVE THREE LINES BE REPLACED BY THESE THREE?
            //String address = "Address: " + mDataset.get(mDataset.size() - 1).address;
            //String latitude = "Latitude: " + mDataset.get(mDataset.size() - 1).lat;
            //String longitude = "Longitude: " + mDataset.get(mDataset.size() - 1).lon;

        holder.ssidTV.setText(address);
        holder.wifiLevelTV.setText(latitude);
        holder.disTextView.setText(longitude);

        holder.ssidTV.setVisibility(View.VISIBLE);
        holder.wifiLevelTV.setVisibility(View.VISIBLE);
        holder.disTextView.setVisibility(View.VISIBLE);

    }

    @Override
    public int getItemCount()
    {
        return mDataset.size();
    }

}

}

布局

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/wifiData"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_margin="2dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:clickable="true"
            android:splitMotionEvents="false"
            android:text="User Location Information" />

</LinearLayout>

wifi_detail_adapter.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.CardView
    android:id="@+id/view2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:clickable="true"
    android:elevation="12dp"
    app:cardCornerRadius="10dp"
    android:layout_margin="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_margin="10dp">


    <TextView
        android:id="@+id/ssidTV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Address:: "/>

    <TextView
        android:id="@+id/wifi_level"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Latitude:: "/>

    <TextView
        android:id="@+id/distanceTv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Longitude:: "/>

    </LinearLayout>
</android.support.v7.widget.CardView>

</LinearLayout>

您正在將一個空列表傳遞給適配器

_mWifidetails = new ArrayList<Classes.UserLocationInformation>();

mUserLocationInformationAdapter = new UserLocationInformationAdapter(MainActivity.this, _mWifidetails);

_mWifidetails為空

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM