我在弹出窗口的for循环中动态创建线性布局,并尝试为每个布局添加监听器。
我正在使用以下代码。但是当我点击任何布局时,它会清除所有数据..
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();
}
});
}
和fillGridData方法
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();
}
因此,当我点击“线性布局”时,数据显示的任何内容都不可见,并使其可见我需要重新启动应用程序,没有例外。如果我做错了什么,Plz会帮助我....
答案 0 :(得分:1)
尝试使用LinearLayout layout = null;在for循环中。
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();
}
});
}