在这里,我试图显示测试的名称和价格。
而且我正在使用回收者视图使用JET Parsing GET方法执行相同的操作。
但是我在公司中什么都没有得到,并在那里表现出自己是黑人。
这是我的代码
请帮助我找到解决方案。

模型类

        public class TestListsModel {

        public String test_price;

        public String testlist_id;

        public String test_name;
    }


这是我的适配器:

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

        private Context context;
        private LayoutInflater inflater;
        List<TestListsModel> data= Collections.emptyList();
        TestListsModel current;
        int currentPos=0;

        // create constructor to innitilize context and data sent from MainActivity
        public AdapterTestList(Context context, List<TestListsModel> data){
            this.context=context;
            inflater= LayoutInflater.from(context);
            this.data=data;
        }

        // Inflate the layout when viewholder created
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view=inflater.inflate(R.layout.test_list_row, parent,false);
            MyHolder holder=new MyHolder(view);
            return holder;
        }

        // Bind data
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

            // Get current position of item in recyclerview to bind data and assign values from list
            MyHolder myHolder= (MyHolder) holder;
            TestListsModel current=data.get(position);
            myHolder.testName.setText(current.test_name);
            myHolder.testPrice.setText( current.test_price);


            // load image into imageview using glide
           /* Glide.with(context).load("http://192.168.1.7/test/images/" + current.fishImage)
                    .placeholder(R.drawable.ic_img_error)
                    .error(R.drawable.ic_img_error)
                    .into(myHolder.ivFish);*/

        }

        // return total item from List
        @Override
        public int getItemCount() {
            return data.size();
        }


        class MyHolder extends RecyclerView.ViewHolder{

            TextView testName;
            TextView testPrice;

            // create constructor to get widget reference
            public MyHolder(View itemView) {
                super(itemView);

                testName = (TextView) itemView.findViewById(R.id.test_name);
                testPrice = (TextView) itemView.findViewById(R.id.price_name);

            }

        }

    }


这是我的活动课:

public class HealthServicesActivity extends AppCompatActivity implements View.OnClickListener {

            SharePreferenceManager<LoginModel> sharePreferenceManager;

            // CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds
            public static final int CONNECTION_TIMEOUT = 10000;
            public static final int READ_TIMEOUT = 15000;
            private RecyclerView testListRecylerView;
            private AdapterTestList mAdapter;

         @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_health_services);
                ButterKnife.bind(this);

                sharePreferenceManager = new SharePreferenceManager<>(getApplicationContext());

                dayTimeDisplay();

                new AsyncLogin().execute();
            }

 private class AsyncLogin extends AsyncTask<String, String, String> {
        //ProgressDialog pdLoading = new ProgressDialog(getApplicationContext());
        HttpURLConnection conn;
        URL url = null;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            //this method will be running on UI thread
          /*  pdLoading.setMessage("\tLoading...");
            pdLoading.setCancelable(false);
            pdLoading.show();*/

        }

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

                String url_test="http://192.168.1.80/aoplnew/api/users/gettestlist/"+sharePreferenceManager.getUserLoginData(LoginModel.class).getResult().getCenterId();
                // Enter URL address where your json file resides
                // Even you can make call to php file which returns json data
                //url = new URL("http://192.168.1.7/test/example.json");
                url = new URL(url_test);

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return e.toString();
            }
            try {

                // Setup HttpURLConnection class to send and receive data from php and mysql
                conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(READ_TIMEOUT);
                conn.setConnectTimeout(CONNECTION_TIMEOUT);
                conn.setRequestMethod("GET");

                // setDoOutput to true as we recieve data from json file
                conn.setDoOutput(true);

            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
                return e1.toString();
            }

            try {

                int response_code = conn.getResponseCode();

                // Check if successful connection made
                if (response_code == HttpURLConnection.HTTP_OK) {

                    // Read data sent from server
                    InputStream input = conn.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                    StringBuilder result = new StringBuilder();
                    String line;

                    while ((line = reader.readLine()) != null) {
                        result.append(line);
                    }

                    // Pass data to onPostExecute method
                    return (result.toString());

                } else {

                    return ("[]");
                }

            } catch (IOException e) {
                e.printStackTrace();
                return e.toString();
            } finally {
                conn.disconnect();
            }


        }

        @Override
        protected void onPostExecute(String result) {

            //this method will be running on UI thread

            //pdLoading.dismiss();
            List<TestListsModel> data=new ArrayList<>();

            //pdLoading.dismiss();
            try {

                JSONArray jArray = new JSONArray(result);

                // Extract data from json and store into ArrayList as class objects
                for(int i=0;i<jArray.length();i++){
                    JSONObject json_data = jArray.getJSONObject(i);
                    TestListsModel testData = new TestListsModel();
                    testData.testlist_id= json_data.getString("testlist_id");
                    testData.test_name= json_data.getString("test_name");
                    testData.test_price= json_data.getString("test_price");

                    data.add(testData);
                }

                // Setup and Handover data to recyclerview
                testListRecylerView = (RecyclerView)findViewById(R.id.test_list_recycler_view);
                mAdapter = new AdapterTestList(HealthServicesActivity.this, data);
                testListRecylerView.setAdapter(mAdapter);
                testListRecylerView.setLayoutManager(new LinearLayoutManager(HealthServicesActivity.this));

            } catch (JSONException e) {
                Toast.makeText(HealthServicesActivity.this, e.toString(), Toast.LENGTH_LONG).show();
            }

        }

    }
}


预先感谢您的任何回答!

最佳答案

您需要先设置setLayoutManager,然后再设置如下所示的适配器。在您的代码中,在setAdapter()之前有setLayoutManager,因此适配器设置不正确。

请参阅此以获得进一步的解释https://developer.android.com/guide/topics/ui/layout/recyclerview

    testListRecylerView = (RecyclerView)findViewById(R.id.test_list_recycler_view);


    mAdapter =  new AdapterTestList(HealthServicesActivity.this, data);
    /**
     * SET THE LAYOUT MANAGER BEFORE SETTING THE ADAPTER
     */

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(HealthServicesActivity.this);
    testListRecylerView.setLayoutManager(mLayoutManager);
    testListRecylerView.setItemAnimator(new DefaultItemAnimator());

     /**
      * AND THAN SET THE ADAPTER
      */

    testListRecylerView.setAdapter(mAdapter);

07-27 21:39