为什么我的JSON数据未显示在我的recyclelerview

时间:2017-08-03 18:31:43

标签: android json android-recyclerview

这是MainActivity.class,哪里没有显示我的数据?** 在logcat中没有错误或其他。我是第一次尝试这个。但我必须知道这个问题。here the image of emultor。我在Onresponse metheod中使用了一个进度条。它没有隐形。因为我的响应不起作用。我怎么能解决这个问题?

public class MainActivity extends AppCompatActivity {
        View view;
        ArrayList<user> list=new ArrayList<>();
        RecyclerView recyclerView;
        RecyclerViewCardViewAdapter recycleExpert_adapter;
      //  private GoogleApiClient client;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            recyclerView = (RecyclerView) findViewById(R.id.recyclerView1);
            RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
            recyclerView.setLayoutManager(mLayoutManager);
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            recyclerView.setHasFixedSize(true);

          //  list= new ArrayList<user>();
            new AsynDemo().execute();

          //  setSupportActionBar(toolbar);
          //  client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
        }
        public class AsynDemo extends AsyncTask<String, Integer, String> {
          //  AlertDialog alertdialog;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                //  alertdialog.new SpotsDialog(MainActivity.this, "Please Wait", R.style.Custom);
                //  alertdialog.show();
            }

            @Override
            protected String doInBackground(String... params) {
                String response = "";
                URL url = null;
                try {
                    url = new URL("http://127.0.0.1:8080/app/con_get_data.php");
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setConnectTimeout(30000);
                    httpURLConnection.connect();
                    InputStream in = httpURLConnection.getInputStream();
                    // read response
                    response = readStream(in);
                    httpURLConnection.disconnect();
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray jsonArray = jsonObject.getJSONArray("result");
                    int count = jsonArray.length();
                    for (int i = 0; i < count; i++) {
                        JSONObject jsonObject2 = jsonArray.getJSONObject(i);
                        String name = jsonObject2.getString("name");
                        String email = jsonObject2.getString("email");

                        list.add(new user(name, email));
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return null;
            }
            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                try {
                    recycleExpert_adapter = new RecyclerViewCardViewAdapter(list,MainActivity.this);
                    recyclerView.setAdapter(recycleExpert_adapter);
                    recycleExpert_adapter.notifyDataSetChanged();

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

                }
            }

            private String readStream(InputStream is) {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
                String line = "\n";
                String result = "";
                try {
                    while ((line = bufferedReader.readLine()) != null) {
                        result += line;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return result;
            }
        }
    }

这是适配器类

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

    Context context;

    List<user> item;

    public RecyclerViewCardViewAdapter(List<user> getDataAdapter, Context context){

        super();

        this.item = getDataAdapter;

        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);

        ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;
    }

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

        user getDataAdapter1 =  item.get(position);

        holder.Name.setText(getDataAdapter1.getName());
        holder.Email.setText(getDataAdapter1.getEmail());

    }

    @Override
    public int getItemCount() {

        return item.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        public TextView Name,Email;


        public ViewHolder(View itemView) {

            super(itemView);

            Name = (TextView) itemView.findViewById(R.id.Name) ;
            Email = (TextView) itemView.findViewById(R.id.email) ;


        }
    }
}

1 个答案:

答案 0 :(得分:1)

我得到了我的em for Emulator。

SELECT DISTINCT e.pref_class_year "CLASS"
               ,(SELECT COUNT (*) FROM degrees d
                        WHERE d.id_number = e.id_number
                            AND (d.major_code1 = '12'
                              OR d.major_code2 = '12'
                              OR d.major_code3 = '12')) "COUNT"

FROM entity e

WHERE e.record_type_code = 'AL'
  AND e.pref_class_year BETWEEN '1965' AND '2017'

ORDER BY 1;

Output:
------------
1965    0
1965    1
1965    2
1966    0
1966    1
1966    2
1966    3
1966    4
1967    0
1967    1
1967    2
1968    0
1968    1
1968    2
1968    3
1969    0
1969    1
1969    2

将其更改为

 url = new URL("http://127.0.0.1:8080/app/con_get_data.php");