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 publ
Solution 1:
Just required changes on your code,
- You have to add
im.setOnClickListener(this);
in for loop of ImageView. Remove below method
im.setOnClickListener(newOnClickListener() { publicvoidonClick(View v) { ImageView iv=(ImageView) v; Log.e("sfas","-->"+iv.getId()); } });
Override onClick() As you have already implement
onClickcListener
in your Activity.
Look at below code, (And match with your code to know actual problem)
publicclassTestActivityextendsActivityimplementsOnClickListener
{
privatestaticfinal String TAG_DATA="data";
privatestaticfinal String TAG_ADVERTISE="advertisments";
privatestaticfinal String TAG_ADVERTISEID="advt_id";
String advertiseid;
privatestaticfinal String TAG_SHOWTEXT="showtext";
String showtext;
privatestaticfinal String TAG_PRODUCTINFO="product_info";
String productinfo;
privatestaticfinal String TAG_THUMBIMAGE="thumbsrc";
String thumbimage;
privatestaticfinal String TAG_DISTANCE="distance";
String distance;
privatestaticfinal String TAG_STIPCIATED="stipciated";
String stipciated;
ArrayList<HashMap<String, String>> listadvertise = newArrayList<HashMap<String,String>>();
ArrayList<HashMap<String, String>> listadvertise1 = newArrayList<HashMap<String,String>>();
ArrayList<HashMap<String, String>> listadvertise2 = newArrayList<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;
publicstaticfinalint img=50000;
@OverridepublicvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.test);
alertDialog = newAlertDialog.Builder(this).create();
DisplayMetrics screensize= newDisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(screensize);
screenheight=screensize.heightPixels;
screenwidth=screensize.widthPixels;
Log.e("Screen Height","---->"+screenheight);
Log.e("Screen Width ","---->"+screenwidth);
RelativeLayoutheaderlLayout= (RelativeLayout)findViewById(R.id.headerlayout);
headerlLayout.setLayoutParams(newRelativeLayout.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(newOnClickListener()
{
publicvoidonClick(View v)
{
Intenti=newIntent(TestActivity.this,HorizontalActivity.class);
startActivity(i);
}
});
scrollView3=(ScrollView)findViewById(R.id.scrollview3);
scrollView3.post(newRunnable() {
publicvoidrun()
{
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= newImageView(TestActivity.this);
im.setLayoutParams(newLinearLayout.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);
}
}
}
}
@OverridepublicvoidonClick(View v) {
Log.e("Clicked","----->"+v.getId());
switch (v.getId())
{
case1:
Log.e("Clicked","----->"+v.getId());
break;
case2:
break;
.
.
.
default:
break;
}
}
Solution 2:
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)
Solution 3:
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
Post a Comment for "How To Get Dynamically Created Imageview Or Textview Clicked Id Or Position In Android?"