Android multiple Id managing



我有一个带有两个选项卡的"活动"。在第一个中有一个Button。如果按下此Button,则会在第二个选项卡中创建一个新的TableRow。这很好。

每个TableRow有3个视图(一个图像、一些文本和一个button)。每次创建Button时,我都会为其分配一个id(0,1,2…)

我想要的是设置一个特定的文本,如果id是0,1,2…无论哪个id。

我试过了,但不起作用:

public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch(arg0.getId()){
        case R.id.save:
            if (et.getText() !=null && thumbnail != null){
                 TableRow tr = new TableRow(this);
                 ImageView view = new ImageView(this);
                 TextView view2 = new TextView(this);
                 Button view3 = new Button(this);
                 view3.setOnClickListener(this);
                 titulo = new String[500];
                 mensaje = new String[500];
                 fotos = new Bitmap[500];
                 view.setImageBitmap(thumbnail);
                 view.setPadding(1, 5, 0, 0);
                 view.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                 Calendar c = Calendar.getInstance();
                 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
                 titulo[i]=""+et.getText();
                 mensaje[i]=""+et1.getText();
                 fotos[i]=thumbnail;
                 i++;
                 view2.setText("" + et.getText()  + dateFormat.format(c.getTime()) );
                 view2.setGravity(Gravity.CENTER_HORIZONTAL);
                 view2.setGravity(Gravity.CENTER_VERTICAL);
                 view3.setGravity(Gravity.RIGHT);
                 view3.setGravity(Gravity.CENTER_VERTICAL);
                 DisplayMetrics metrics = new DisplayMetrics();
                 getWindowManager().getDefaultDisplay().getMetrics(metrics);
                 tr.addView(view, metrics.widthPixels/3, 150);
                 tr.addView(view2, metrics.widthPixels/2, 100);
                 tr.addView(view3, metrics.widthPixels/6, 20);
                 tl.addView (tr, 0);

                 final Toast toastMensaje = Toast.makeText(getApplicationContext(),
                            "Tu entrada se cargó correctamente", Toast.LENGTH_LONG);
                    toastMensaje.setGravity(Gravity.CENTER, 0, 0);
                    toastMensaje.show();
                    et.setText("");
                    et1.setText("");
                    i1.setVisibility(View.GONE);

            }
            else{
                final Toast toastMensaje = Toast.makeText(getApplicationContext(),
                        "Tienes que añadir un título y una foto", Toast.LENGTH_LONG);
                toastMensaje.setGravity(Gravity.CENTER, 0, 0);
                toastMensaje.show();
            }

        break;
        case R.id.photo:
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

        break;
        case R.id.gallery:
            Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
             startActivityForResult(intent, TFRequestCodes);
        break;
    }

        for (int j=0; j<titulo.length; j++){
            if (arg0.getId()==j){
            final Toast toastMensaje = Toast.makeText(getApplicationContext(),titulo[j], Toast.LENGTH_LONG);
            toastMensaje.setGravity(Gravity.CENTER, 0, 0);
            toastMensaje.show();
        }
    }




}

不起作用的是这段代码:

        for (int j=0; j<titulo.length; j++){
            if (arg0.getId()==j){
            final Toast toastMensaje = Toast.makeText(getApplicationContext(),titulo[j], Toast.LENGTH_LONG);
            toastMensaje.setGravity(Gravity.CENTER, 0, 0);
            toastMensaje.show();
        }
    }

这是XML文件:

android:id="@+id/LinearLayout"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabHost
    android:id="@+id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TabWidget>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#001521"
                android:orientation="vertical">

                 <EditText
                    android:id="@+id/title"
                    android:layout_width="fill_parent"
                    android:layout_height="42dp"
                    android:layout_marginTop="5dp"
                    android:hint="Lugar" 
                    android:gravity="top">
                    </EditText>
                 <EditText
                    android:id="@+id/editText1"
                    android:layout_width="fill_parent"
                    android:layout_height="252dp"
                    android:layout_marginTop="3dp"
                    android:hint="Título"
                    android:gravity="top"
                    android:inputType="textMultiLine" ></EditText>
                 <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#001521"
                android:orientation="horizontal">
               <Button
                    android:id="@+id/photo"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:text="Photo" />
               <Button
                    android:id="@+id/gallery"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:text="Gallery" />
                <ImageView
                    android:id="@+id/i1"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"/>

            </LinearLayout>
                <Button
                    android:id="@+id/save"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:text="Guardar entrada"
                     />
            </LinearLayout>
            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
                <ScrollView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent" >
                    <TableLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:id="@+id/tl"
                    android:stretchColumns="0,1,2">
                    </TableLayout>
                </ScrollView>
            </LinearLayout>
            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

您不应该初始化onClick()中的那些

   titulo = new String[500];
                     mensaje = new String[500];
                     fotos = new Bitmap[500];

这样一来,每次单击"保存"时,都会创建新对象,并且无法存储字符串。将它们移动到onCreate()。我也是一个全局变量吗?

我将把这个放在这里,作为注释的开头:p

尝试添加一些日志以检查它是否进入循环

for (int j=0; j<titulo.length; j++)
{ 
 Log.d("TAG","id = " + arg0.getId() + " j = " + j); 
 if (arg0.getId()==j)
 { 
     Log.d("TAG","its equal"); 
     final Toast toastMensaje = Toast.makeText(getApplicationContext(),titulo[j], Toast.LENGTH_LONG);
     toastMensaje.setGravity(Gravity.CENTER, 0, 0); toastMensaje.show(); 
 }

我怀疑您真正的问题是使用getApplicationContext()而不是活动的上下文。也试试这个。