android recyclelerview

时间:2017-06-30 06:10:00

标签: android android-recyclerview

我的项目中有一个recyclerview,数据没有绑定到recyclerview。我从服务器获取数据并正确地获取数据。我在适配器类中放了一个Toast,它正在工作。我无法弄清楚这个问题。

活动类......

public class ViewDealerCompln extends AppCompatActivity {

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

        typeface = Typeface.createFromAsset(getAssets(), "productsans.ttf");

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

        compList = new ArrayList<>();

        toolbar = (Toolbar) findViewById(R.id.com_view_app_bar);
        TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("");
        mTitle.setText("Complain History");
        mTitle.setTypeface(typeface);

        repID = DealerListAdapter.getRepID();
        dealerID = DealerListAdapter.getDealerID();

        mLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(mLayoutManager);

        recyclerView.setAdapter(adcAdapter);

        getData();
    }

    private void getData() {

        String tag_string_req = "req_data";

        request = new StringRequest(Request.Method.POST, AppConfig.URL_JSON_GETCOMPLAIN, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.names().get(0).equals("feed")) {
                        JSONArray jsonFeedArray = jsonObject.getJSONArray("feed");
                        if (jsonFeedArray.length() > 0) {

                            for (int i = 0; i < jsonFeedArray.length(); i++) {
                                JSONObject currentObject = jsonFeedArray.getJSONObject(i);
                                String namemm = currentObject.getString("compId");
                                compList.add(namemm);
                            }
                            adcAdapter = new ViewDealerComplainAdapter(ViewDealerCompln.this, compList);

                        } else {
                            Toast.makeText(getApplicationContext(), "No data Available!", Toast.LENGTH_SHORT).show();
                        }

                    } else {
                        Toast.makeText(getApplicationContext(), "Invalid Response from the server!", Toast.LENGTH_SHORT).show();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {

        };

    }

}

适配器类......

public class ViewDealerComplainAdapter extends RecyclerView.Adapter<ViewDealerComplainAdapter.ItemViewHolder> {

    private LayoutInflater inflater;
    private ArrayList<String> subList;
    private Context context;

    public ViewDealerComplainAdapter(Context context, ArrayList<String> a) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.subList = a;
        Toast.makeText(context, subList.toString(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.custom_complain_list_row, parent, false);
        ViewDealerComplainAdapter.ItemViewHolder holder = new ViewDealerComplainAdapter.ItemViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        holder.title.setText(subList.get(position));
    }

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

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView title;

        public ItemViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.compHID);
        }
    }
}

自定义行布局......

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp">

    <TextView
        android:id="@+id/compHID"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:padding="10dp" />
</LinearLayout>

主要布局......

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/com_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp">

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

5 个答案:

答案 0 :(得分:1)

在你的活动中,在此行之前的recyclerView.setAdapter(adcAdapter); 写

adcAdapter=new ViewDealerComplainAdapter(this,new ArrayList<String>())

在适配器中创建一个函数,如下所示

public void setData(ArrayList<String> data){
this.sublist=data;
}

现在,在函数getData()中 在此之后

for (int i = 0; i < jsonFeedArray.length(); i++) {
                                JSONObject currentObject = jsonFeedArray.getJSONObject(i);
                                String namemm = currentObject.getString("compId");
                                compList.add(namemm);
                            }

adcAdapter.setData(compList);
adcAdapter.notifyDataSetChanged();

这将使你得到正确的结果

答案 1 :(得分:0)

从服务器加载数据后更新RecyclerView

   adcAdapter = new ViewDealerComplainAdapter(ViewDealerCompln.this, compList);
   adcAdapter.notifyDataSetChanged();

另一种方式

在Adapter

中创建一个方法
public void setDataChange(List<Object> asList) {
        this.List = asList;
        //now, tell the adapter about the update
        notifyDataSetChanged();
    }

adcAdapter.setDataChange(list);

答案 2 :(得分:0)

我认为这是因为你在创建对象之前设置了适配器。

recyclerView.setAdapter(adcAdapter);
getData();

您首先将适配器设置为recyclerView,然后调用getData()方法,在该方法中创建adcAdapter对象。

答案 3 :(得分:0)

你的适配器有两个参数context&amp; arrayList,因此在创建适配器实例时,请确保将这两个值传递给适配器并将相同的适配器设置为RecyclerView。

mAdapter = new MAdapter(this,list); mRecyclerView.setAdapter(mAdapter);

列表中有数据后,将该列表传递给适配器。

答案 4 :(得分:-2)

在初始化之前,您将adcAdapter设置为recyclerView

将代码更改为打击。

adcAdapter = new ViewDealerComplainAdapter(ViewDealerCompln.this, compList);
recyclerView.setAdapter(adcAdapter);

此外,您需要在适配器上调用notifyDataSetChanged()以在收到回复时更新recycleview。

 adcAdapter.notifyDataSetChanged();