Adding views programmatically. displays nothing

67 views Asked by At

I am adding a view to my table row programmatically. Everything is working fine and logcat isn't showing any errors. Strangely enough nothing is being displayed in my activity.

Below is the code I am using:

            try{
                int k=0;

                for(i=0;i<=Math.ceil(jArray.length()/2);i++){
                    Log.e("I...",""+i+" "+Math.ceil(jArray.length()/2));
                    tr_head = new TableRow(getApplicationContext());
                    tr_head.setId(10+i);
                    tr_head.setPadding(30, 30, 30, 5);
                    tr_head.setGravity(Gravity.LEFT|Gravity.CENTER_HORIZONTAL);
                    tr_head.setLayoutParams(new LayoutParams(
                                                             LayoutParams.MATCH_PARENT,
                                                             LayoutParams.WRAP_CONTENT));

                    for(j=0;j<2;j++)
                    {
                        if (k<jArray.length()){
                            jobj1 = jArray.getJSONObject(k);

                            LinearLayout parent = new LinearLayout(getApplicationContext());
                            parent.setLayoutParams(new LinearLayout.LayoutParams(dpToPx(150), dpToPx(150)));
                            parent.setOrientation(LinearLayout.VERTICAL);
                            parent.setGravity(Gravity.CENTER);
                            ImageView img = new ImageView(getApplicationContext());
                            img.setLayoutParams(new LinearLayout.LayoutParams(dpToPx(60),dpToPx(60)));
                            Picasso.with(getApplicationContext()).load(jobj1.getString("image")).resize(50,50).into(img);
                            parent.addView(img);

                            TextView txt = new TextView(getApplicationContext());
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                            txt.setLayoutParams(params);
                            txt.setText(jobj1.getString("name"));
                            parent.addView(txt);

                            tr_head.addView(parent);
                            parent.setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    // TODO Auto-generated method stub
                                    try{
                                        String catid=jobj1.getString("category_id");
                                        Intent intent = new Intent(getApplicationContext(), DescribeComplain.class);
                                        intent.putExtra("category_id",catid);
                                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                        startActivity(intent);
                                    }
                                    catch(JSONException je){
                                        je.printStackTrace();
                                    }
                                }
                            });
                        }
                        else{
                            break;
                        }
                        k++;
                    }
                    t1.addView(tr_head, new TableLayout.LayoutParams(
                                                                     LayoutParams.WRAP_CONTENT,
                                                                     LayoutParams.WRAP_CONTENT));
                }
            }
            catch(JSONException je){
                je.printStackTrace();
            }
        }
    });
}
catch(JSONException e){
    Log.e("log_tag", "Error parsing data "+e.toString());
}
2

There are 2 answers

0
karan On BEST ANSWER

After trying loads of methods, I solved it by setting linearLayout's parameters like this

LinearLayout parent = new LinearLayout(SelectCaegory.this);
TableRow.LayoutParams Paramslinear = new  TableRow.LayoutParams(dpToPx(150),dpToPx(150));
4
Alexis Burgos On

Your problem is with this line:

LinearLayout parent = new LinearLayout(getApplicationContext());

You are creating a new view but she's not attach to your root view. Try to declare your LinearLayout on your layout file, and fint it with the ID

LinearLayout parent = (LinearLayout) findViewById(R.id.my_parent_layout);