start android
start android

Reputation: 323

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

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;


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

}

}

Upvotes: 0

Views: 2372

Answers (3)

user370305
user370305

Reputation: 109247

Just required changes on your code,

  1. You have to add im.setOnClickListener(this); in for loop of ImageView.
  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.

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

Upvotes: 2

Ahmed Aeon Axan
Ahmed Aeon Axan

Reputation: 2139

You are setting the ID's for the ImageView with im.setId(i);. So the ID's would be the value of i. You can keep track of this somewhere.

Also, please post only the relevant pieces of the code and not the whole class

Upvotes: 0

Sean
Sean

Reputation: 5266

You can setId() when you create the view and then that's his ID. http://developer.android.com/reference/android/view/View.html#setId(int)

Upvotes: 0

Related Questions