使用Volley在片段内填充自定义布局ListView

时间:2017-06-30 22:03:51

标签: android listview android-fragments

我的应用程序中有一个导航抽屉,我想显示一个自定义布局的列表视图,这些布局通过齐射填充。 **代码没有给我任何错误,但列表视图中没有条目。**有人能告诉我错误是什么吗?我究竟做错了什么? 这是片段的代码。

package com.example.vishal.navtest2;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */




public class BlankFragment extends Fragment {


    public BlankFragment() {
        // Required empty public constructor
    }


    RequestQueue requestQueue;
    String reqUrl = "http://192.168.1.7/Tutorial/requestGetTry.php";

    final List<String> requestNoList = new ArrayList<String>();
    final List<String> requestDescriptionList = new ArrayList<String>();
    final List<String> dateList = new ArrayList<String>();
    final List<String> statusList = new ArrayList<String>();
    String[] menuItems = {"item 1","Item 2","Item 3","item 1","Item 2","Item 3","item 1","Item 2","Item 3","item 1","Item 2","Item 3",};

    String disp = "";
    int len ;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        final View view = inflater.inflate(R.layout.fragment_blank, container, false);

        requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
        ListView listView = (ListView) view.findViewById(R.id.listView);

//        String[] reqDetPass = new String[5];


        JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST, reqUrl, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                try {
                    JSONArray requests = response.getJSONArray("request");
                    len = requests.length();
                    for(int i = 0; i < requests.length(); i++){
                        JSONObject request = requests.getJSONObject(i);
                        String no = request.getString("requestno");
                        String desc = request.getString("description");
                        String date = request.getString("date");
                        String status = request.getString("status");

                        Toast.makeText(getActivity(), no+desc+date+status, Toast.LENGTH_SHORT).show();

                        requestNoList.add(no);
                        requestDescriptionList.add(desc);
                        dateList.add(date);
                        statusList.add(status);

                        disp = disp + requestNoList.get(i);



                    }
                    //Toast.makeText(getActivity(), "Disp: "+disp, Toast.LENGTH_SHORT).show();

                    if(requestNoList == null){
                        Toast.makeText(getActivity(), "Null In ReqList", Toast.LENGTH_SHORT).show();
                    }

                    ListView listView = (ListView) view.findViewById(R.id.listView);



                    CustomAdapter customAdapter = new CustomAdapter();

                    String[] stringArray = requestNoList.toArray(new String[0]);
                    String[] dummy = {"Item 1","Item 2","Item 3","Item 4","Item 5","Item 6","Item 7","Item 8","Item 9","Item 10","Item 11",};

                    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, stringArray);

                    final ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, dummy);

                    listView.setAdapter(customAdapter);

                    Toast.makeText(getContext(), String.valueOf(requestDescriptionList.toArray()[0]), Toast.LENGTH_LONG).show();

                    customAdapter.notifyDataSetChanged();


                } catch (JSONException e) {
                    e.printStackTrace();
                    //requestNoList.add(e.toString());
                    Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_SHORT).show();
                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //requestNoList.add(error.toString());
                Toast.makeText(getContext(), error.toString(), Toast.LENGTH_SHORT).show();
            }
        });

        requestQueue.add(req);



        String[] data = new String[len];
        for(int i = 0; i<len ; i++){
            data[i] = requestNoList.get(i);

        }





        /*ListView listView = (ListView) view.findViewById(R.id.listView);

        String[] stringArray = requestNoList.toArray(new String[10]);

        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, stringArray);

        listView.setAdapter(adapter);*/

        //Toast.makeText(getActivity(), stringArray[0], Toast.LENGTH_SHORT).show();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


//                int color = parent.getAdapter().getItem(position);
//                position = position - 1 ;
                Toast.makeText(getActivity(), "position: "+position+"id: "+id, Toast.LENGTH_SHORT).show();

                String data = parent.getAdapter().getItem(position).toString();

                Toast.makeText(getActivity(),"data "+ data, Toast.LENGTH_SHORT).show();

                String reqNo = requestNoList.get(position);

                Toast.makeText(getActivity(),"reqno "+reqNo , Toast.LENGTH_SHORT).show();   //String.valueOf(position)


                Intent intent = new Intent(getActivity(), ReqDisp.class);
                intent.putExtra("Position",reqNo);
                startActivity(intent);
            }
        });

        return view;
    }



    class CustomAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @SuppressLint("ViewHolder")
        @Override
        public View getView(int i, View view, ViewGroup parent) {

            view = getActivity().getLayoutInflater().inflate(R.layout.custom_layout_listview, null);

            TextView textView_request = (TextView)view.findViewById(R.id.tvReq);
            TextView textView_description = (TextView)view.findViewById(R.id.tvDesc);
            TextView textView_dates = (TextView)view.findViewById(R.id.tvDate);
            ImageView imageView = (ImageView)view.findViewById(R.id.imageView4);


            if(String.valueOf(requestNoList.toArray()[i]) == null){
                textView_request.setText("Empty "+String.valueOf(i));
            }
            //textView_request.setText(String.valueOf(requestNoList.toArray()[i]));
            textView_description.setText(String.valueOf(requestDescriptionList.toArray()[i]));
            textView_dates.setText(String.valueOf(dateList.toArray()[i]));

            if(String.valueOf(statusList.toArray()[i]).equals("0")){
                imageView.setImageResource(R.drawable.ic_new_releases_black_24dp);
            }
            else{
                imageView.setImageResource(R.drawable.ic_home_black_24dp);
            }




            return null;
        }
    }


}

请帮忙。

1 个答案:

答案 0 :(得分:0)

无法指出确切的解决方案,但这里有一些指针/输入:

  1. 您的onResponse方法是否收到回应?
  2. 我猜您没有将数据设置到您已设置为列表视图的customAdapter。(我们通常会向其添加数据 - &gt; customAdapter.addAll(listOfData);
  3. 尝试使用RecyclerView而不是listView - &gt;更好的性能。