首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用NullPointerException实现ListView的CustomAdapter

使用NullPointerException实现ListView的CustomAdapter
EN

Stack Overflow用户
提问于 2017-12-18 01:55:12
回答 1查看 63关注 0票数 0

CustomAdapter文件包括..。

1.初始化ArrayList。

2.初始化ListView。

3.从URL中提取数据并存储在ArrayList中。

4.用ListView设置CustomAdapter

5.请检查CustomAdapter代码,该代码位于MainActivity下面。

代码语言:javascript
运行
复制
public class Screen2 extends AppCompatActivity {     

ProgressDialog pDialog;
        ArrayList<Information> record_list;
        ListView list_view;
        CustomAdapter listAdapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.screen2);
            record_list=new ArrayList<Information>();
            list_view=(ListView) findViewById(R.id.list_view);
            new Test().execute();
        }

        public class Test extends AsyncTask<String, Void, ArrayList<Information>> {
            @Override
            protected void onPreExecute() {

                super.onPreExecute();

                // Showing progress dialog
                pDialog = new ProgressDialog(Screen2.this);
                pDialog.setMessage("Please wait...");
                pDialog.setCancelable(false);
                pDialog.show();
            }

            @Override
            protected ArrayList<Information> doInBackground(String... params) {

                String jsonStr = makeServiceCall();
                try {
                    JSONArray jsonArray=new JSONArray(jsonStr);
                    for(int i=0;i<jsonArray.length();i++){
                        JSONObject jsonObject=jsonArray.getJSONObject(i);
                        String id=jsonObject.getString("id");
                        String name=jsonObject.getString("label");
                        String email=jsonObject.getString("email");
                        Information information=new Information(id,name,email);
                        record_list.add(information);
                        return record_list;
                    }
                    System.out.println(jsonStr);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return null;
            }

            @Override
            protected void onPostExecute(ArrayList<Information> s) {
                super.onPostExecute(s);
                // Dismiss the progress dialog
                //listAdapter=new CustomAdapter(Screen2.this,record_list);
                listAdapter=new CustomAdapter(Screen2.this,record_list);
                list_view.setAdapter(listAdapter);
                if (pDialog.isShowing())
                    pDialog.dismiss();

                /**
                 * Updating parsed JSON data into ListView
                 * */
            }

            public String makeServiceCall(){
                String response = null;
                try {
                    URL url = new URL("http://192.168.1.109:9000/tasks2");
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    // read the response
                    InputStream in = new BufferedInputStream(conn.getInputStream());
                    response = convertStreamToString(in);
                } catch (MalformedURLException e) {
                    System.out.println("MalformedURLException: " + e.getMessage());
                } catch (ProtocolException e) {
                    System.out.println("MalformedURLException: " + e.getMessage());
                } catch (IOException e) {
                    System.out.println("MalformedURLException: " + e.getMessage());
                } catch (Exception e) {
                    System.out.println("MalformedURLException: " + e.getMessage());
                }
                return response;
            }

            private String convertStreamToString(InputStream is) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                StringBuilder sb = new StringBuilder();

                String line;
                try {
                    while ((line = reader.readLine()) != null) {
                        sb.append(line).append('\n');
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(sb);
                return sb.toString();
            }
        }

}

CustomAdapter.java:(这是我的CustomAdapter文件)我也没有看到这个文件有任何错误.

代码语言:javascript
运行
复制
public class CustomAdapter extends ArrayAdapter<Information> {

        public CustomAdapter(Context c, ArrayList<Information> record) {
            super(c, R.layout.custom_row, record);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            // Get the data item for this position

            Information information = getItem(position);

            //LayoutInflater inflater=LayoutInflater.from(getContext());

            // Check if an existing view is being reused, otherwise inflate the view

            convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_row, parent, false);

            // Lookup view for data population

            TextView record_id = (TextView) convertView.findViewById(R.id.record_id);

            TextView record_name = (TextView) convertView.findViewById(R.id.record_name);

            TextView record_email = (TextView) convertView.findViewById(R.id.record_email);

            // Populate the data into the template view using the data object

            record_id.setText(information.id);
            record_name.setText(information.name);
            record_email.setText(information.email);

            return convertView;
        }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-18 02:15:09

代码中存在逻辑错误:

  1. for循环中有一个return语句,而您必须迭代,因此为了进行良好的设计,请为Arraylist定义另一个变量,比如s,代码如下所示: @重写受保护的ArrayList doInBackground(字符串.{ String jsonStr = makeServiceCall();ArrayList s=新ArrayList();尝试{ JSONArray jsonArray=new JSONArray(jsonStr);for(int i=0;i
  2. 然后在您的onPostExecute上,让record_list成为s,如下所示: @重载受保护的void onPostExecute(ArrayList s) { super.onPostExecute(s);// parsed //listAdapter=新CustomAdapter(屏幕2.this,record_list);record_list = s;listAdapter=new CustomAdapter(Screen2.this,record_list);list_view.setAdapter(listAdapter);if (pDialog.isShowing()pDialog.dismiss());/** *将解析的JSON数据更新为ListView * */ }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47861194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档