RecyclerView中的cardView未显示任何项目

时间:2018-02-21 18:37:02

标签: android android-recyclerview android-viewpager recycler-adapter

我正在尝试制作一个在ViewPager中有两个标签的应用程序,每个标签都是片段,并且应该在RecyclerView中有一个卡片视图。我从一个API中获取数据,并将其添加到ArrayList中。我在Adapter类和DailyMenuFrag中没有看到任何错误。我将不胜感激任何帮助

DailyMenuFrag.java

public class DailyMenuFrag extends Fragment {

    private List<DailyData> data_list;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        data_list = new ArrayList<>();
        load_data();
        View view = inflater.inflate(R.layout.fragment_daily_menu, container, false);
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        DailyDataAdapter adapter = new DailyDataAdapter(getActivity(), data_list);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);
        TextView textView = (TextView) view.findViewById(R.id.tt);
        textView.setText("hellp");
        return view;
    }

    public void load_data() {
        task.execute("http://yemekapp.kuarkdijital.com.tr/home.php");
    }
        AsyncTask<String, Void, String> task = new AsyncTask<String, Void, String>() {

            @Override
            protected String doInBackground(String... params) {

                URL url;
                HttpURLConnection URLConnection = null;
                String current = "";

                try {
                    url = new URL(params[0]);

                    URLConnection = (HttpURLConnection) url.openConnection();
                    URLConnection.connect();

                    InputStream inputStream = URLConnection.getInputStream();

                    InputStreamReader reader = new InputStreamReader(inputStream);

                    int data = reader.read();

                    while (data != -1) {
                        current += (char) data;
                        data = reader.read();
                    }

                    JSONObject dailyObject = null;
                    JSONObject popularObject = null;
                    JSONObject jsonObject = new JSONObject(current);
                    JSONObject banner = jsonObject.getJSONObject("banner");
                    String daily = jsonObject.getString("daily");
                    String popular = jsonObject.getString("popular");
                    JSONArray dailyArray = new JSONArray(daily);
                    JSONArray popularArray = new JSONArray(popular);
                    for (int i = 0; i < dailyArray.length(); i++) {
                        dailyObject = dailyArray.getJSONObject(i);
                        popularObject = popularArray.getJSONObject(i);
                        DailyData singleData = new DailyData(dailyObject.getInt("id"), dailyObject.getString("Servings"), dailyObject.getString("Title"), dailyObject.getString("CookTime"), dailyObject.getString("Image"));
                        data_list.add(singleData);
                        Log.i("data", data_list.size() + "");


                        }

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

                return current;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);


            }
        };
    }

DailyDataAdapter.java

public class DailyDataAdapter extends RecyclerView.Adapter<DailyDataAdapter.ViewHolder> {

    Context context;
    List<DailyData> dailyDataList;

    public DailyDataAdapter(Context context, List<DailyData> dailyDatas) {
        this.context = context;
        this.dailyDataList = dailyDatas;
    }

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

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.title.setText(dailyDataList.get(position).getTitle());
        holder.cookTime.setText(dailyDataList.get(position).getCookTime());
        holder.servings.setText(dailyDataList.get(position).getServings());

        //Image
        Glide.with(context).load(dailyDataList.get(position).getImage_link()).into(holder.thumbnail);


    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{
        public TextView cookTime,servings,title;
        public ImageView thumbnail;

        public ViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
            servings = (TextView) itemView.findViewById(R.id.servings);
            cookTime = (TextView) itemView.findViewById(R.id.cooktime);
            thumbnail = (ImageView) itemView.findViewById(R.id.thumbnail);
        }

    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

        if(savedInstanceState == null){
            ViewPagger viewPagger = new ViewPagger();
            FragmentManager manager = getSupportFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.replace(R.id.fragment_container,viewPagger);
            transaction.commit();
        }
    }
}

ViewPAgger.java

public class ViewPagger extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_view_pagger,container,false);
        ViewPager viewPager = (ViewPager) view.findViewById(R.id.view_pager);
        TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab);
        viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return position == 0? new DailyMenuFrag():new PopularMenuFrag();
            }

            @Override
            public int getCount() {
                return 2;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return position == 0? "Daily":"Popular";
            }
        });
        tabLayout.setupWithViewPager(viewPager);
        return view;
    }
}

ViewPagger.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.nejat.yemektarifiproject.ViewPagger">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp">

    </android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v4.view.ViewPager>

</FrameLayout>

card.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:card_view="http://schemas.android.com/tools">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:elevation="3dp"
        card_view:cardCornerRadius="0dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/thumbnail"
                android:layout_width="match_parent"
                android:layout_height="160dp"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:clickable="true"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/thumbnail"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:paddingTop="10dp"
                android:textColor="#4c4c4c"
                android:textSize="15dp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/title"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/servings"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/title"
                    android:paddingBottom="10dp"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:textSize="10dp"
                    android:layout_weight="1"/>
                <TextView
                    android:id="@+id/cooktime"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/title"
                    android:layout_weight="1"
                    android:paddingBottom="10dp"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:textSize="10dp" />


            </LinearLayout>



        </RelativeLayout>

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

</LinearLayout>

Fragment_DailyMenuFrag.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.nejat.yemektarifiproject.DailyMenuFrag">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        >
    </android.support.v7.widget.RecyclerView>
    <TextView
        android:id="@+id/tt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />


</FrameLayout>

2 个答案:

答案 0 :(得分:1)

我认为您在获取数据后忘记通知适配器有关数据更改的信息。

声明您的Adapter Global,以便您可以从任何地方调用

DailyDataAdapter adapter = null;

并在onCreateView

中将其初始化
adapter = new DailyDataAdapter(getActivity(), data_list);

所以在你的onPostExecute中的doInBackground之后调用这个notifyDataSetChanged()

 @Override
 protected void onPostExecute(String s) {
 super.onPostExecute(s);
    adapter.notifyDataSetChanged();
 }

这对你有用。

答案 1 :(得分:1)

添加此内容 adapter.notifyDataSetChanged()

中的postExecute