下载相应的文件也是RecyclerView行

时间:2017-02-06 09:21:34

标签: android json

我需要Android Studio中编程部分的帮助。

我有一个与我的数据库中的json数组相对应的RecyclerView。在这个数组中有一些填充RecyclerView的信息。每个json对象都有一个Web服务器上相应文件的URL。 在这一部分,我需要你的帮助。我需要从每个json对象获取URL并将相应的文件下载到设备并将其设置为RecyclerView行。

json中的URL名称为:download_url(目前不在我的代码中,而是在json数组中。

下载状态应显示在状态栏中,并带有进度视图(我需要在通知名称中添加一些信息)。当用户单击该行时,PDF文件应显示在PFD查看器中。 接下来是从设备中删除这些文件。如果用户刷新RecyclerView并且给定的json不再具有该文件,则应自动删除它们。 URL的末尾对应于服务器上的文件。 谢谢你的想法。我是初学者,并尽力从教程中学到很多东西,但这个问题对我来说非常困难和具体。

这是我的适配器:

public class PlansAdapter extends RecyclerView.Adapter<PlansAdapter.MyPlanHolder> {

    private List<Plans> planList;
    private final Context customContext;

    public class MyPlanHolder extends RecyclerView.ViewHolder {

        public TextView planTitle, planType, planDate;

        public MyPlanHolder(View view) {
            super(view);
            planTitle = (TextView) view.findViewById(R.id.planTitle);
            planType = (TextView) view.findViewById(R.id.planTyp);
            planDate = (TextView) view.findViewById(R.id.planDate);
        }
    }

    public PlansAdapter(List<Plans> planList, Context customContext) {
        this.planList = planList;
        this.customContext = customContext;
    }

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

        return new MyPlanHolder(itemView);
    }

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

        final Plans plan = planList.get(position);

        // Format timestamp to date
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        String resultDate = null;

        try {
            date = fmt.parse(plan.getPlanDate());
            SimpleDateFormat fmtOut = new SimpleDateFormat("dd.MM.yyyy");
            resultDate = fmtOut.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }

        holder.planTitle.setText(plan.getPlanTitle());
        holder.planType.setText(plan.getEatingPlanType() + " " + plan.getTrainingPlanType());
        holder.planDate.setText("resultDate);

    }

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

这是我的Request.class,我得到了json对象:

public class PlansRequest extends StringRequest {
    public static final String PLANS_REQUEST_URL = "http://hiddenwebserver.de/de/json";
    private Map<String, String> params;

    public PlansRequest(String loginEmail, Response.Listener<String> listener) {
        super(Request.Method.POST, PLANS_REQUEST_URL, listener, null);
        params = new HashMap<>();
        params.put("loginEmail", loginEmail);
    }

    @Override
    public Map<String, String> getParams() {
        return params;
    }
}

这是我在Fragment上设置RecyclerView的地方:

@Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
       // Inflate the layout for this fragment
       View view = inflater.inflate(R.layout.fragment_training, container, false);

       // Get Refresh Layout
       swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.training_swiperefresh);

       // Connectivity manager for network check
       final ConnectivityManager connectivityManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);


       // Change color for the refresh layout
       swipeRefreshLayout.setColorSchemeColors(Color.rgb(99, 195, 195));


       swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
           @Override
           public void onRefresh() {
               // Check if network connection exists
               if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
                       connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {

                   // Get plans data from web
                   getPlansData();
               } else {
                   // Error message if internet connection is not available
                   Toast.makeText(getActivity(), getString(R.string.noInternetConnection), Toast.LENGTH_LONG).show();
               }
           }
       });

       trainingRecyclerView = (RecyclerView) view.findViewById(R.id.training_recycler_view);

       pAdapter = new PlansAdapter(planList, getContext());
       RecyclerView.LayoutManager pLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());

       trainingRecyclerView.setLayoutManager(pLayoutManager);
       trainingRecyclerView.setItemAnimator(new DefaultItemAnimator());
       trainingRecyclerView.setAdapter(pAdapter);

       preparePlansData();

       return view;
	
	}

这是我从json数组中准备数据的地方:

private void preparePlansData() {

    // Connectivity manager for network check
    final ConnectivityManager connectivityManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);

    // Check if network connection exists
    if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
            connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {

        // Get shared preferences
        SharedPreferences plansPreferenceData = getActivity().getSharedPreferences("plansData", MODE_PRIVATE);

        if (plansPreferenceData.contains("plansPreferenceData")) {
            String plansData = plansPreferenceData.getString("plansPreferenceData", "").replace("\"success\":true,", "");
            // Remove success string from jsonResponse
            //plansData.replace("success\":true,", "");
            Log.d("plansData", "Result:" + plansData);

            Gson gson = new Gson();

            Type type = new TypeToken<Map<String, Plans>>(){}.getType();
            Map<String, Plans> myPlansMap = gson.fromJson(plansData, type);

            planList.addAll(myPlansMap.values());

            // Notify data changes
            pAdapter.notifyDataSetChanged();

            // Now we call setRefreshing(false) to signal refresh has finished
            swipeRefreshLayout.setRefreshing(false);
        } else {
            getPlansData();
        }
    } else {
        // Error message if internet connection is not available
        Toast.makeText(getActivity(), getString(R.string.noInternetConnectionOnStart), Toast.LENGTH_LONG).show();
    }
}

0 个答案:

没有答案