RecyclerView不显示数据

时间:2017-12-16 20:54:14

标签: java android android-recyclerview

我正在尝试将数据设置为RecyclerView。

以下是回复数据。

[
    {
 "shopName": "Hello World.",
 "shopTeluguName": "మమ్మీ",
 "shopAddress": "Bomanahalli"
}, 
    {
 "shopName": "Hello World.",
 "shopTeluguName": "మమ్మీ",
 "shopAddress": "Bomanahalli"
}, 
    {
 "shopName": "Hello World.",
 "shopTeluguName": "మమ్మీ",
 "shopAddress": "Bomanahalli"
}, 
    {
 "shopName": "Hello.",
 "shopTeluguName": "మమ్మీ",
 "shopAddress": "Bomanahalli"
}
]

它解析并在Arraylist中获取它们都工作正常但回收器视图没有显示任何数据。空屏幕。

以下是活动

public class MainActivity extends AppCompatActivity implements WebServiceInterface {

    Toolbar toolbar;
    RecyclerView recyclerViewShops;
    private int FETCH_SHOPS_REQUEST_CODE = 1;
    ArrayList<Shop> arrayListShops;
    ShopsAdapter adapterShops;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();

        fetchShops();
    }

    private void init() {

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Shops List");
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        getSupportActionBar().setDisplayShowHomeEnabled(false);
        toolbar.setTitleTextColor(Color.WHITE);

        recyclerViewShops = (RecyclerView) findViewById(R.id.recyclerView);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
        recyclerViewShops.setLayoutManager(mLayoutManager);
        recyclerViewShops.setItemAnimator(new DefaultItemAnimator());
        recyclerViewShops.setHasFixedSize(true);

        arrayListShops = new ArrayList<>();

    }

    private void fetchShops() {

        HashMap<String, String> paramsList = new HashMap<>();

        WebServiceController webServiceController = new WebServiceController(
                this, this);
        String hitURL = LinksAndKeys.SHOPS_URL;
        webServiceController.sendGETRequest("", "Loading..", hitURL, paramsList, FETCH_SHOPS_REQUEST_CODE);

    }

    @Override
    public void getResponse(int responseCode, String responseString, String requestType, int requestCode) {
        if (requestCode == FETCH_SHOPS_REQUEST_CODE && responseCode == 200) {

            Gson gson = new Gson();
            Shop[] shops = gson.fromJson(responseString, Shop[].class);
            arrayListShops = new ArrayList<Shop>(Arrays.asList(shops));

            adapterShops = new ShopsAdapter(this, arrayListShops);
            recyclerViewShops.setAdapter(adapterShops);
        }
    }
}

以下是适配器:

public class ShopsAdapter extends RecyclerView.Adapter<ShopsAdapter.MyViewHolder> {

    Activity activity;
    private List<Shop> shopList;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        LinearLayout linearLayoutParent;
        public TextView textViewShopName, textViewShopTeluguName, textViewShopAddress;

        public MyViewHolder(View view) {
            super(view);
            linearLayoutParent = (LinearLayout) view.findViewById(R.id.linearLayoutParent);
            textViewShopName = (TextView) view.findViewById(R.id.textViewShopName);
            textViewShopTeluguName = (TextView) view.findViewById(R.id.textViewShopTeluguName);
            textViewShopAddress = (TextView) view.findViewById(R.id.textViewShopAddress);
        }
    }


    public ShopsAdapter(Activity activity, List<Shop> shopList) {
        this.activity = activity;
        this.shopList = shopList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.row_shop, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        final Shop shop = shopList.get(position);

        holder.textViewShopName.setText(shop.getShopName());
        holder.textViewShopTeluguName.setText(shop.getShopTeluguName());
        holder.textViewShopAddress.setText(shop.getShopAddress());

        holder.linearLayoutParent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(activity, shop.getShopName(), Toast.LENGTH_SHORT).show();
            }
        });
    }

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

布局:

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

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />

</LinearLayout>

响应很好,ArrayList有数据,但recyclerview没有显示。 这里有什么想法错了。?

3 个答案:

答案 0 :(得分:2)

当您使用arrayListShops = new ArrayList<Shop>(Arrays.asList(shops));时,将分配新内存位置,并在arrayListShops中分配地址,但您的adapterShops与旧内存位置相关联。因此,当您致电adapterShops.notifyDataSetChanged()时,它将检查旧内存位置并刷新列表。但是您已在新分配的内存位置添加了数据,因此数据未显示。您必须在旧内存位置添加数据,而不是分配新内存。更新您的getResponse方法,如下所示

@Override
public void getResponse(int responseCode, String responseString, String requestType, int requestCode) {
    if (requestCode == FETCH_SHOPS_REQUEST_CODE && responseCode == 200) {

        Gson gson = new Gson();
        Shop[] shops = gson.fromJson(responseString, Shop[].class);
        ArrayList<Shop> tmp = new ArrayList<>(Arrays.asList(shops));
        arrayListShops.addAll(tmp);
        Toast.makeText(this, arrayListShops.size() + " Shops", Toast.LENGTH_SHORT).show();
        adapterShops.notifyDataSetChanged();
    }
}

答案 1 :(得分:0)

常见错误。您需要先使用空列表在init()函数中设置适配器。然后在fetchShops()方法中将数据添加到列表中,然后在更新视图后调用adapterShops.notifyDataSetChanged()

答案 2 :(得分:0)

我的代码中没有看到任何内容阻止您显示RecyclerView及其内容。此外,您以错误的方式使用它,每次调用ShopsAdapter回调时都会生成getResponse的新实例。只需保留init方法之一,只处理arrayListShops列表添加和删除元素。完成后,请不要忘记调用adapterShops.notifyDataSetChanged()方法。

我认为您的主要问题来自您的布局。您应该看到遵循以下布局的Shop商品:

<强> 值/ styles.xml

<resources>
  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.AppCompat.Light" />
</resources>

<强> 布局/ activity_main.xml中

在您的情况下,请更好地使用RelativeLayout。您使用了LinearLayout,但错过了重要的android:orientation="vertical"属性,这肯定不会在您的示例中显示您的项目。

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

  <include
      android:id="@+id/toolbar"
      layout="@layout/toolbar" />

  <android.support.v7.widget.RecyclerView
      android:id="@+id/recyclerView"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:scrollbars="vertical" />

</RelativeLayout>

<强> 布局/ row_shop.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayoutParent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_orange_dark"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    >

  <TextView
      android:id="@+id/textViewShopName"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      />

  <TextView
      android:id="@+id/textViewShopTeluguName"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      />

  <TextView
      android:id="@+id/textViewShopAddress"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      />


</LinearLayout>