简体   繁体   English

如何动态创建Imageview或textview在android中点击id或位置?

[英]How To Get Dynamically Created Imageview or textview clicked id or position in android?

i want to create dynamic imageview and button which in scrollview so i want to get the id for that clicked item which is created dynamically how can i get this here is my code 我想在scrollview中创建动态imageview和按钮,所以我想得到动态创建的点击项的id我怎么能在这里得到这个是我的代码

public class TestActivity extends Activity implements OnClickListener { 公共类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;
    }

}

}

Just required changes on your code, 只需要对代码进行必要的更改

  1. You have to add im.setOnClickListener(this); 你必须添加im.setOnClickListener(this); in for loop of ImageView . forView循环中。
  2. Remove below method 删除下面的方法

     im.setOnClickListener(new OnClickListener() { public void onClick(View v) { ImageView iv=(ImageView) v; Log.e("sfas","-->"+iv.getId()); } }); 
  3. Override onClick() As you have already implement onClickcListener in your Activity. 覆盖onClick()您已经在Activity中实现了onClickcListener

Look at below code, (And match with your code to know actual problem) 看下面的代码,(并与你的代码匹配,以了解实际问题)

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;
 }
}

You can setId() when you create the view and then that's his ID. 您可以在创建视图时setId() ,然后是他的ID。 http://developer.android.com/reference/android/view/View.html#setId(int) http://developer.android.com/reference/android/view/View.html#setId(int)

You are setting the ID's for the ImageView with im.setId(i); 您正在使用im.setId(i);设置ImageView的ID im.setId(i); . So the ID's would be the value of i. 因此ID将是i的值。 You can keep track of this somewhere. 你可以在某处跟踪这个。

Also, please post only the relevant pieces of the code and not the whole class 另外,请仅发布相关的代码段而不是全班

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM