0

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

}

}
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. onClickcListenerアクティビティにすでに実装しているので、 onClick()をオーバーライドします。

以下のコードを見てください(そして実際の問題を知るためにあなたのコードと一致させてください)

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

でのIDを設定していImageViewますim.setId(i);。したがって、IDはiの値になります。これはどこかで追跡できます。

また、クラス全体ではなく、コードの関連部分のみを投稿してください

于 2013-03-10T19:52:36.707 に答える