简体   繁体   English

从现有的LazyAdapter加载新的LazyAdapter

[英]Loading new LazyAdapter from existing LazyAdapter

Ok, here we go. 好的,我们开始。 I have provided full mockup to avoid question why you don't do this that way, or this way.. So blue box is just inflated header that works, below the blue header I have Activity with ListView (green) populated with a LazyAdapter. 我提供了完整的模型,以避免出现疑问,为什么您不采用这种方式或这种方式。.因此,蓝色框只是可膨胀的标头起作用,在蓝色标头下方,我有一个用LazyAdapter填充的带有ListView(绿色)的Activity。 Each row (white) have buttons that on click opens dialog (yellow) with another ListView populated with LazyAdapterTwo(black). 每行(白色)都有按钮,这些按钮在单击时会打开对话框(黄色),其中另一个列表视图填充了LazyAdapterTwo(黑色)。

在此处输入图片说明

Problem is that I have null exception while setting second adapter from first, so I don't know where the problem is: listStatusComments.setAdapter(adapterStatusComments); 问题是从第一个设置第二个适配器时出现空异常,所以我不知道问题出在哪里:listStatusComments.setAdapter(adapterStatusComments);

LazyAdapterStatus class LazyAdapterStatus类

public class LazyAdapterStatus extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    ProgressDialog progressDialog;
    DatabaseHandler db = new DatabaseHandler(activity);
    ListView listStatusComments;
    LazyAdapterStatusComments adapterStatusComments;


    public LazyAdapterStatus(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.activity_main_list_row, null);

        final Dialog dialog = new Dialog(activity);

        TextView title = (TextView)vi.findViewById(R.id.activityStatus); 
        ImageButton comments = (ImageButton)vi.findViewById(R.id.comments_ico);

        comments.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, String> item = new HashMap<String, String>();
                item = data.get(position);

                dialog.setContentView(R.layout.dialog_status_comment);
                dialog.setTitle("Comment");
                String statusId = item.get(MainActivity.STATUS_ID);         
                new GetStatusCommentsTask().execute(statusId);
            }
        });
        HashMap<String, String> item = new HashMap<String, String>();
        item = data.get(position);


        title.setText(item.get(MainActivity.STATUS_TEXT));

       return vi;
    }

    public class GetStatusCommentsTask extends AsyncTask<String, Void, ArrayList<StatusComments>> {

        protected void onPreExecute() {         
            progressDialog  = new ProgressDialog(activity);
            progressDialog.setMessage("Loading");
            progressDialog.setCancelable(true);
            progressDialog.show();
        }

        protected ArrayList<StatusComments> doInBackground(String... arg0) {
            String response = null;
            try {
                try {
                    HttpParams httpParameters = new BasicHttpParams();
                    HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
                    HttpConnectionParams.setSoTimeout(httpParameters, 20000);
                    HttpClient client = new DefaultHttpClient(httpParameters);

                    HttpPost postReq = new HttpPost();
                    postReq.setURI(new URI(com.seventy.in.util.ClientUrls.GET_STATUS_COMMENTS_URL));
                    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();                   

                    postParameters.add(new BasicNameValuePair(
                            "status_id", arg0[0]));

                    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
                            postParameters);


                    postReq.setEntity(formEntity);
                    HttpResponse httpResponse = client.execute(postReq);
                    if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                        response = NetworkUtil
                                .convertStreamToString(httpResponse.getEntity()
                                        .getContent());
                        Log.i("RESP", response);
                    }
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                if (response != null && response != "") {

                    JSONObject myAwway = new JSONObject(response);
                    JSONArray jsonArrComments = myAwway.getJSONArray("status_comments");
                    ArrayList<StatusComments> commentsList = new ArrayList<StatusComments>(); 

                    for (int i = 0; i < jsonArrComments.length(); i++) {
                        StatusComments comment = StatusComments.fromJson(jsonArrComments
                                .getJSONObject(i));

                        if (comment.getCommentUserId() != null) {                           
                            commentsList.add(comment);
                        }
                    }

                    return commentsList;


                }
                else { 
                    return null;
                }

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

        }

        @Override
        protected void onPostExecute(ArrayList<StatusComments> result) {
            super.onPostExecute(result);
            progressDialog.dismiss();

            final Dialog dialog = new Dialog(activity);
            dialog.setContentView(R.layout.dialog_status_comment);
            dialog.setTitle("Patka");

            final ArrayList<HashMap<String, String>> postList = new ArrayList<HashMap<String, String>>();


            for (int i = 0; i < result.size(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                StatusComments e = result.get(i);
                map.put(MainActivity.COMMENT_USER_ID, e.getCommentUserId());
                map.put(MainActivity.KEY_IMAGE_URL, e.getUserImageUrl());
                map.put(MainActivity.STATUS_TEXT, e.getCommentText());

                postList.add(map);              
            }       


            listStatusComments = (ListView)dialog.findViewById(R.id.list);


            adapterStatusComments=new LazyAdapterStatusComments(activity, postList);
            listStatusComments.setAdapter(adapterStatusComments); 

        }
    }
}

LazyAdapterStatusComments LazyAdapterStatusComments

public class LazyAdapterStatusComments extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 

    public LazyAdapterStatusComments(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }


    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.friends_list_row, null);

        TextView title = (TextView)vi.findViewById(R.id.name); // title
        TextView about = (TextView)vi.findViewById(R.id.item_description); // artist name
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.image_url); // thumb image

        HashMap<String, String> song = new HashMap<String, String>();
        song = data.get(position);

        // Setting all values in listview
        title.setText(song.get(MainActivity.STATUS_TEXT));  
        imageLoader.DisplayImage(song.get(MainActivity.KEY_IMAGE_URL), thumb_image);
        return vi;
    }
}

I found the problem. 我发现了问题。 Code works perfectly, only I forgot to enter list element into the xml layout so listStatusComments = (ListView)dialog.findViewById(R.id.list); 代码完美运行,只有我忘记将列表元素输入到xml布局中,因此listStatusComments =(ListView)dialog.findViewById(R.id.list); was searching for a element that was not there 正在寻找不存在的元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM