1

I am creating linear layout dynamically inside for loop for pop up window and trying to add listener to each layout.

I am using following code. But when I click on any of the layout, it clears all data..

    LinearLayout layout = null; 

    for(int i=0;i<data.size();i++)
    {
        layout=new LinearLayout(MainActivity.this);
        layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        eventFilterlayout.addView(layout);
        eventFilterlayout.setTag(new Holder(layout));

        imageView=new ImageView(MainActivity.this);
        imageView.setImageResource(data.get(i).getImgRes());
        imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,1.0f));
        layout.addView(imageView);


        eventNameText=new TextView(MainActivity.this);
        eventNameText.setText(data.get(i).getText());
        eventNameText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,4.0f));
        eventNameText.setGravity(Gravity.CENTER_VERTICAL);
        layout.addView(eventNameText);

        final Holder holder=(Holder)eventFilterlayout.getTag();

        holder.layout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(eventNameText.getText().equals("All"))
                    fillGridData(Utils.ALL);
                if(eventNameText.getText().equals("Some"))
                    fillGridData(Utils.SOME);
                if(eventNameText.getText().equals("None"))                      
                    fillGridData(Utils.NONE);
                if(eventNameText.getText().equals("Little more"))
                    fillGridData(Utils.MORE)
                if(eventNameText.getText().equals("Little less"))
                    fillGridData(Utils.LESS);

                window.dismiss();
            }
        });
    }

and in fillGridData method

public void fillGridData(int type)
{
    eventData.clear();

    switch (type) {

    case Utils.ALL:
        list.add(new EventData("All",R.raw.hall1),          
        list.add(new EventData("All", R.raw.hall2));            
    break;

    case Utils.SOME:
        list.add(new EventData("Some", R.raw.hall2));           
        break;

    case Utils.NONE:
        break;

    case Utils.MORE:
        list.add(new EventData("More",R.raw.hall1); 
        break;

    case Utils.LESS:
        list.add(new EventData("Less",R.raw.hall1),         
        list.add(new EventData("Less", R.raw.hall2));
        break;

    default:
        break;
    }

    eventAdapter=new EventAdapter(MainActivity.this,eventData);
    gridView.setAdapter(eventAdapter);
    eventAdapter.notifyDataSetChanged();
    gridView.invalidate();
    gridView.bringToFront();
}

So when I click on Linear Layout, data whatever it is displaying goes invisible and to make it visible i need to restart app, no exceptions. Plz help me if i am doing anything wrong....

Rohit
  • 2,646
  • 6
  • 27
  • 52
  • onclick lister and eventAdapter.notifyDataSetChanged(); gridView.invalidate(); are happening on same time , what extactly you wanna achive with notifydatasetadapter? – KOTIOS Feb 16 '15 at 05:32
  • Actually I am changing data so dats why I put eventAdapter.notifyDataSetChanged() but still If I remove those line same behavior m getting.. – Rohit Feb 16 '15 at 05:36
  • eventData.clear(); is this effecting? – KOTIOS Feb 16 '15 at 05:39
  • @Rohit check the below link get a more idea of how this works http://stackoverflow.com/questions/4198425/updating-the-list-view-when-the-adapter-data-changes?answertab=votes#tab-top – koherent Feb 16 '15 at 05:44
  • @diva: No initially it is loading data.. Initially in onCreate I am calling same method, at that time it is loading data, but when I click it is not loading any data – Rohit Feb 16 '15 at 06:30
  • @koherent: I tried it... I guess problem is in click Listener since I am using it inside for loop but If i go out and use it then click listener is not working – Rohit Feb 16 '15 at 07:17
  • @Rohit assign a tag to each view of the linear layout . then set the listener on the view corresponding to the tag. also call the onclick associated with that tag – koherent Feb 16 '15 at 08:06
  • @koherent:I did same thing `eventFilterlayout.setTag(new Holder(layout));` I am setting tag and calling callListener assosiater with it – Rohit Feb 16 '15 at 08:19

1 Answers1

1

Try using LinearLayout layout = null; inside for loop.

 for(int i=0;i<data.size();i++)
    {
       LinearLayout layout=new LinearLayout(MainActivity.this);
        layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        eventFilterlayout.addView(layout);


        ImageView imageView=new ImageView(MainActivity.this);
        imageView.setImageResource(data.get(i).getImgRes());
        imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,1.0f));
        layout.addView(imageView);


        TextView eventNameText=new TextView(MainActivity.this);
        eventNameText.setText(data.get(i).getText());
        eventNameText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,4.0f));
        eventNameText.setGravity(Gravity.CENTER_VERTICAL);
        layout.addView(eventNameText);



        layout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(eventNameText.getText().equals("All"))
                    fillGridData(Utils.ALL);
                if(eventNameText.getText().equals("Some"))
                    fillGridData(Utils.SOME);
                if(eventNameText.getText().equals("None"))                      
                    fillGridData(Utils.NONE);
                if(eventNameText.getText().equals("Little more"))
                    fillGridData(Utils.MORE)
                if(eventNameText.getText().equals("Little less"))
                    fillGridData(Utils.LESS);

                window.dismiss();
            }
        });
    }
Suhail Mehta
  • 5,514
  • 2
  • 23
  • 37