使用api照片和onItem单击侦听器进行gridView注入

时间:2014-01-30 08:27:13

标签: android gridview

That is actually what I want. All titles, gridviews and textViews are created dynamically.

我正在使用这样的视图注入。 GridViews是根据API数据创建的。

       if(followup.size()>0)
    {

        for (Followup fData : followup) 
        {  
            String followupId= fData.get("id");
            LinearLayout lLayout=(LinearLayout) findViewById(R.id.lLayout); 
            LinearLayout ll1= new LinearLayout(this);
            ll1.setOrientation(LinearLayout.VERTICAL);
            TextView txtfup= new TextView(this);                
            txtfup.setBackground(getResources().getDrawable(R.drawable.bg));
            txtfup.setTypeface(face);
            txtfup.setText("    Followup " + i++);              
            txtfup.setTextColor(Color.WHITE);
            txtfup.setGravity(Gravity.LEFT);
            txtfup.setTextSize(18);
            LayoutParams params = new LayoutParams(
                    LayoutParams.FILL_PARENT,      
                    LayoutParams.WRAP_CONTENT
            );
            txtfup.setLayoutParams(params);             
            ll1.addView(txtfup);

            TextView txtEmpty= new TextView(this);
            txtEmpty.setText("");
            ll1.addView(txtEmpty);
            lLayout.addView(ll1);

            RelativeLayout rLayout=new RelativeLayout(this);
            LinearLayout.LayoutParams relativeParams = new LinearLayout.LayoutParams(
                    new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT));
            relativeParams.setMargins(40, 8, 40, 30);// left, top, right, bottom
            rLayout.setLayoutParams(relativeParams);
            rLayout.requestLayout();

            gridViewF= new GridView(this);
            gridViewF.setHorizontalSpacing(10);
            gridViewF.setNumColumns(5); 



            rLayout.addView(gridViewF);
            lLayout.addView(rLayout);
            //gridViewF.setOnItemClickListener(gridViewOnclickListener_followup);
            //---------------------------------

            RelativeLayout rLayout1=new RelativeLayout(this);
            LinearLayout.LayoutParams relativeParams1 = new LinearLayout.LayoutParams(
                    new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT));
            relativeParams1.setMargins(40, 10, 40, 40);// left, top, right, bottom
            rLayout1.setLayoutParams(relativeParams);
            rLayout1.requestLayout();

            TableRow row=new TableRow(this.getApplicationContext());
            TableRow row1=new TableRow(this.getApplicationContext());

            TableLayout tlayout=new TableLayout(this.getApplicationContext());

            row=new TableRow(this);
            row1=new TableRow(this);

            TableLayout.LayoutParams tableRowParams=
                      new TableLayout.LayoutParams
                      (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);

                    tableRowParams.setMargins(6, 2, 6, 2);

                    row.setLayoutParams(tableRowParams);
                    row1.setLayoutParams(tableRowParams);

            TextView text1=new TextView(this);
            TextView text2=new TextView(this);
            TextView text3=new TextView(this);
            TextView text4=new TextView(this);

            text1.setTypeface(face); 
            text2.setTypeface(face1);
            text3.setTypeface(face);
            text4.setTypeface(face1);
            text1.setTextSize(18);
            text2.setTextSize(16);
            text3.setTextSize(18);
            text4.setTextSize(16);
            text1.setHeight(70);
            text2.setHeight(70);
            text3.setHeight(70);
            text4.setHeight(70);

            text1.setText("Description :  ");
            row.addView(text1);
            text2.setText(fData.get("note"));
            row.addView(text2);
            text3.setText("Followup Date :  ");
            row1.addView(text3);
            text4.setText(fData.get("followup_date"));
            row1.addView(text4);
            tlayout.addView(row);
            tlayout.addView(row1);
            rLayout1.addView(tlayout);
            lLayout.addView(rLayout1);
            //-----------------------------

            photo= new ArrayList<Followup_Photos>();
            for (Followup_Photos photos : followupPhoto) {
                if(followupId.equalsIgnoreCase(photos.get("photo_type_id")))

                    photo.add(new Followup_Photos(photos.get("photo_id"), photos.get("photo_name"), photos.get("date_added"),
                            photos.get("photo_type_id")));
            }
            setGridView_Followup(photo);
            Log.i(TAG, "followup photos"+ followupPhoto.toString());
            Log.i(TAG, "photo array=>"+ photo.toString());
            //------------------------------
            LinearLayout ll4= new LinearLayout(this);
            ll4.setOrientation(LinearLayout.HORIZONTAL);
            TextView txtView= new TextView(this);
            txtView.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
            txtView.setHeight(30);
            ll4.addView(txtView);

        }

但是当我调用gridViewF.setOnItemClickListener时会出现问题。因为在循环内创建了多个gridViews,但是名称为gridViewF。那么,请问我有什么建议吗?

2 个答案:

答案 0 :(得分:0)

<强>步骤

  1. 获取后续尺寸(循环
  2. 使GridView数组像GridView [] gridview = new GridView [后续大小];(不要在for循环中执行。在它之前执行
  3. 使用int我使用旧模式循环(为了方便..可能你有更好的主意)并做gridview [i] =新的GridView(上下文)(循环内部
  4. 现在设置gridview [i] .setonItemClickListener(------------------------);

答案 1 :(得分:0)

非常感谢您的建议,MGDroid。我解决了这些问题。我按照你的建议更改了我的代码,当我动态创建gridview时,我为每个gridview设置了id。 gridview的[I] .setId(i)中。然后在gridView.setOnItemClickListener中,我通过parent.getId()获取该id。它工作正常。这是我的代码。

final int size= followup.size();
    GridView[] gridview = new GridView[size];
    fphotos= new ArrayList[size];

    if(followup.size()>0)
    {
    for(i=0;i< size;i++)
    {
        fphotos[i]= new ArrayList<Followup_Photos>();
        gridview[i]=new GridView(this);
        gridview[i].setHorizontalSpacing(5);
        gridview[i].setVerticalSpacing(15);
        gridview[i].setNumColumns(5);
        gridview[i].setId(i);
        rLayout.addView(gridview[i]);

        lLayout.addView(rLayout);

        //gridview[i].setOnItemClickListener(FollowupGridListener);
        gridview[i].setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                // TODO Auto-generated method stub
                imageSelectedPosition = position;
                int i= parent.getId();
                Log.i(TAG, "selectedGrid id=>" + i);
                for(int l=0;l<size;l++)
                {
                    if(i==l)
                             initiatePopupImage_followup(imageSelectedPosition, fphotos[l]);
                }

            }
        });     
        for (Followup_Photos photos : followupPhoto) {

            if(followupId.equalsIgnoreCase(photos.get("photo_type_id")))

                fphotos[i].add(new Followup_Photos(photos.get("photo_id"), photos.get("photo_name"), photos.get("date_added"),
                        photos.get("photo_type_id")));

        }
        setGridView_Followup(fphotos[i], gridview[i]);

    }

 //-------------------------------

private void initiatePopupImage_followup(int p, ArrayList<Followup_Photos> photos){

try{
    // We need to get the instance of the LayoutInflater 
    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View layout = inflater.inflate(R.layout.image_layout_sever,(ViewGroup)findViewById(R.id.popup_element_sever)); 

    pwindo = new PopupWindow(layout, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, true);
    pwindo.setBackgroundDrawable(new BitmapDrawable()); //this one to affect setOutsideTouchable
    pwindo.setOutsideTouchable(true); //To close when touch back button
    pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
    final ImageView myImage = (ImageView) layout.findViewById(R.id.imgView);
    AsyncHttpClient client = new AsyncHttpClient();
    String[] allowedContentTypes = new String[] { "image/png", "image/jpeg" , "image/gif" , "image/jpg"};

    imageURL= photos.get(p).get("photo_name");



    System.out.println("imageURL=>" + imageURL);
    client.get(StaticHelper.AMAZON_BASE_URL + imageURL, new BinaryHttpResponseHandler(allowedContentTypes) {
        @Override
        public void onSuccess(byte[] fileData) {

            Log.i("Image Download","Successful");
            Bitmap bmp=BitmapFactory.decodeByteArray(fileData,0,fileData.length);
            myImage.setImageBitmap(bmp);

            myImage.setVisibility(View.VISIBLE);
         }
    });

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

}