0

我想在滚动视图中创建动态图像视图和按钮,所以我想获取动态创建的单击项目的 ID 我怎样才能得到这是我的代码

公共类 TestActivity 扩展 Activity 实现 OnClickListener {

    private static final String TAG_DATA="data";
    private static final String TAG_ADVERTISE="advertisments";
    private static final String TAG_ADVERTISEID="advt_id";
    String advertiseid;

    private static final String TAG_SHOWTEXT="showtext";
    String showtext;

    private static final String TAG_PRODUCTINFO="product_info";
    String productinfo;

    private static final String TAG_THUMBIMAGE="thumbsrc";
    String thumbimage;

    private static final String TAG_DISTANCE="distance";
    String distance;

    private static final String TAG_STIPCIATED="stipciated";
    String stipciated;


    ArrayList<HashMap<String, String>> listadvertise = new ArrayList<HashMap<String,String>>();
    ArrayList<HashMap<String, String>> listadvertise1 = new ArrayList<HashMap<String,String>>();
    ArrayList<HashMap<String, String>> listadvertise2 = new ArrayList<HashMap<String,String>>();
    // Webservice parameter for home advertise
    String url;
    String fbid;
    String latitude;
    String longitude;
    String passdistance;
    String offset;

    // Webservice parameter for stipciated advertise

    String userid;
    String stipciate;

    int screenheight;
    int screenwidth;
    AlertDialog alertDialog;
    private ProgressDialog progressDialog;
    ImageView imagemenu;

    ScrollView scrollView3;


    ImageView im;
    LinearLayout homelistlayout1;
    LinearLayout homelistlayout2;
    public static final int img=50000;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.test);

        alertDialog = new AlertDialog.Builder(this).create();
        DisplayMetrics screensize= new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(screensize);

        screenheight=screensize.heightPixels;
        screenwidth=screensize.widthPixels;

        Log.e("Screen Height","---->"+screenheight);
        Log.e("Screen Width ","---->"+screenwidth);


        RelativeLayout headerlLayout = (RelativeLayout)findViewById(R.id.headerlayout);
        headerlLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,((screenwidth*8)/100)+10));

        if(CheckConnection.getInstance(this).isOnline(this))
        {

        //  new HomeAsyncTask().execute("");

        }
        else
        {
            alert();
        }

        imagemenu=(ImageView)findViewById(R.id.imagemenu);
        imagemenu.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v)
            {
                    Intent i = new Intent(TestActivity.this,HorizontalActivity.class);
                    startActivity(i);
            }
        });



      scrollView3=(ScrollView)findViewById(R.id.scrollview3);

        scrollView3.post(new Runnable() {

            public void run()
            {

                scrollView3.scrollTo(0, 200);
            }
        });


        homelistlayout1=(LinearLayout)findViewById(R.id.homelistlayout1);
        homelistlayout1.setPadding(0, 100, 0, 0);
        homelistlayout2=(LinearLayout)findViewById(R.id.homelistlayout2);


        for(int i=0;i<12;i++)
        {

            im= new ImageView(TestActivity.this);
            im.setLayoutParams(new LinearLayout.LayoutParams(200, 200));

            if(i%2==0)
            {

                    im.setImageResource(R.drawable.adv);
                    im.setId(i);
                    homelistlayout1=(LinearLayout)findViewById(R.id.homelistlayout1);
                    homelistlayout1.addView(im);


            }
            else
            {
                im.setImageResource(R.drawable.adv2);
                im.setId(i);
                homelistlayout2=(LinearLayout)findViewById(R.id.homelistlayout2);
                homelistlayout2.addView(im);

                }

            }   


            im.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {

                    ImageView iv=(ImageView) v;

                    Log.e("sfas","-->"+iv.getId());

                }
            });
            }
       }

public void onClick(View v) {

    Log.e("Clicked","----->"+v.getId());
    switch (v.getId()) 
    {
    case img:
                    Log.e("Clicked","----->"+v.getId());
            break;

    default:
        break;
    }

}

}
4

3 回答 3

2

只需要更改您的代码,

  1. 您必须添加ImageViewim.setOnClickListener(this);的 for 循环。
  2. 删除以下方法

    im.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                ImageView iv=(ImageView) v;
                Log.e("sfas","-->"+iv.getId());
            }
        });
    
  3. 覆盖 onClick()正如您已经onClickcListener在 Activity 中实现的那样。

查看下面的代码,(并与您的代码匹配以了解实际问题)

public class TestActivity extends Activity implements OnClickListener 
{
    private static final String TAG_DATA="data";
    private static final String TAG_ADVERTISE="advertisments";
    private static final String TAG_ADVERTISEID="advt_id";
    String advertiseid;
    private static final String TAG_SHOWTEXT="showtext";
    String showtext;
    private static final String TAG_PRODUCTINFO="product_info";
    String productinfo;

    private static final String TAG_THUMBIMAGE="thumbsrc";
    String thumbimage;

    private static final String TAG_DISTANCE="distance";
    String distance;

    private static final String TAG_STIPCIATED="stipciated";
    String stipciated;

    ArrayList<HashMap<String, String>> listadvertise = new ArrayList<HashMap<String,String>>();
    ArrayList<HashMap<String, String>> listadvertise1 = new ArrayList<HashMap<String,String>>();
    ArrayList<HashMap<String, String>> listadvertise2 = new ArrayList<HashMap<String,String>>();
    // Webservice parameter for home advertise
    String url;
    String fbid;
    String latitude;
    String longitude;
    String passdistance;
    String offset;

    // Webservice parameter for stipciated advertise

    String userid;
    String stipciate;

    int screenheight;
    int screenwidth;
    AlertDialog alertDialog;
    private ProgressDialog progressDialog;
    ImageView imagemenu;
    ScrollView scrollView3;


    private ListView listViewLeft;
    private ListView listViewRight;

    int[] leftViewsHeights;
    int[] rightViewsHeights;

    ImageView im;
    LinearLayout homelistlayout1;
    LinearLayout homelistlayout2;
     public static final int img=50000;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.test);

        alertDialog = new AlertDialog.Builder(this).create();
        DisplayMetrics screensize= new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(screensize);

        screenheight=screensize.heightPixels;
        screenwidth=screensize.widthPixels;

        Log.e("Screen Height","---->"+screenheight);
        Log.e("Screen Width ","---->"+screenwidth);

        RelativeLayout headerlLayout = (RelativeLayout)findViewById(R.id.headerlayout);
        headerlLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,((screenwidth*8)/100)+10));

        if(CheckConnection.getInstance(this).isOnline(this))
        {

        //  new HomeAsyncTask().execute("");
        }
        else
        {
            alert();
        }

        imagemenu=(ImageView)findViewById(R.id.imagemenu);
        imagemenu.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v)
            {
                    Intent i = new Intent(TestActivity.this,HorizontalActivity.class);
                    startActivity(i);
            }
        });

      scrollView3=(ScrollView)findViewById(R.id.scrollview3);
        scrollView3.post(new Runnable() {
            public void run()
            {
                scrollView3.scrollTo(0, 200);
            }
        });

        homelistlayout1=(LinearLayout)findViewById(R.id.homelistlayout1);
        homelistlayout1.setPadding(0, 100, 0, 0);
        homelistlayout2=(LinearLayout)findViewById(R.id.homelistlayout2);

        for(int i=0;i<12;i++)
        {
            im= new ImageView(TestActivity.this);
            im.setLayoutParams(new LinearLayout.LayoutParams(200, 200));
            im.setOnClickListener(this); 
            if(i%2==0)
            {
                    im.setImageResource(R.drawable.adv);
                    im.setId(i);
                    homelistlayout1=(LinearLayout)findViewById(R.id.homelistlayout1);
                    homelistlayout1.addView(im);
            }
            else
            {
                im.setImageResource(R.drawable.adv2);
                im.setId(i);
                homelistlayout2=(LinearLayout)findViewById(R.id.homelistlayout2);
                homelistlayout2.addView(im);
            }
        }   
       }
   }

@Override
public void onClick(View v) {
  Log.e("Clicked","----->"+v.getId());
  switch (v.getId()) 
   {
    case 1:
        Log.e("Clicked","----->"+v.getId());
        break;
    case 2:
        break;
    .
    .
    .
    default:
    break;
 }
}
于 2013-03-10T19:49:02.620 回答
0

您可以setId()在创建视图时创建他的 ID。 http://developer.android.com/reference/android/view/View.html#setId(int)

于 2013-03-10T19:48:42.590 回答
0

您正在为ImageViewwith设置 ID im.setId(i);。所以 ID 将是 i 的值。你可以在某个地方跟踪它。

另外,请只发布相关的代码,而不是整个班级

于 2013-03-10T19:52:36.707 回答