3

Android でアプリを開発しており、UI 要素をループで生成しています。ただし、これらの要素には、文字と数字の ID が必要です。たとえば"rl1"、 または"rl2". メソッドを使用しようとしましたRelativeLayout.setId()が、そのメソッドは のみを受け入れますint。数字に限らずIDを自由に設定する方法はありますか?

ありがとう。

これが私が機能させようとしているコードです。

for (int i=1; i < 10; i++)
{
    //gets the frameview where the elements will be created.
    String LinearLayoutId = "frameview1";
    int resID = getResources().getIdentifier(LinearLayoutId, "id", "com.myapp.ERS");
    LinearLayout linearLayout = (LinearLayout)findViewById(resID);

    //creates the RelativeLayout that will hold the ImageIcon and the TextView
    RelativeLayout rl = new RelativeLayout(this);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,40 );
    rl.setLayoutParams(lp);
    rl.setId("rl"); /// >>>> I would like here to set and ID of "rl1" for example.
    rl.setBackgroundDrawable(getResources().getDrawable(R.drawable.bk36));

    //creates the image icon within the layout at the left side
    ImageView image = new ImageView(this);
    lp = new RelativeLayout.LayoutParams(
            40,RelativeLayout.LayoutParams.MATCH_PARENT );

    image.setLayoutParams(lp);
    String imageicon = "icon_"+i;
    resID = getResources().getIdentifier(imageicon, "drawable", "com.myapp.ERS");
    image.setImageDrawable(getResources().getDrawable(resID));  //sets the icon
    rl.addView(image); //adds the ImageView to the relative layout


    //creates the TextView within the layout with a 40 margin to the left
    TextView tv = new TextView(this);
    lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT );
    lp.setMargins(40, 0, 0, 0);
    tv.setLayoutParams(lp);
    String textViewID = "tv"+i;
    resID = getResources().getIdentifier(textViewID, "string", "com.myapp.ERS");
    tv.setText(getResources().getString(resID));
    tv.setTextColor(Color.BLACK);
    tv.setTextSize(25);
    rl.addView(tv);//adds the TextView to the relative layout
    rl.setOnClickListener(mAddListener);
    linearLayout.addView(rl);//adds the RelativeLayout to the LinearLayout
}

そして、私はこのようなOnClickListenerを持っています...

private OnClickListener mAddListener = new OnClickListener()
{
    public void onClick(View v){
        Intent intent;
        Bundle bundle;

        String id = getResources().getResourceEntryName(v.getId());
        id = id.replaceAll("\\D+","");
        int value = Integer.parseInt(id);

        intent = new Intent(ERS.this, ShowInfo.class);
        bundle = new Bundle();
        bundle.putInt("key", value);  
        System.out.println(v.getId());
        intent.putExtras(bundle); 
        startActivity(intent);
    }
};

数値 ID を設定しようとしましたが、次のように検索すると、次のようになります。

String id = getResources().getResourceEntryName(v.getId());

それらを見つけることができません。

最初からこれらすべてをxmlファイルに入れていましたが、リストには約40個のアイテムがあるため非常に長く、たとえばすべての文字を変更するのは複雑でした. 私はこのアイデアを思いつき、実行時にfor loop. その間、10 でテストしていますが、動作しません。

私が何か間違ったことをしているなら、私を許してください、しかし私はこれに慣れていません。

4

2 に答える 2

0

XMLレイアウトに戻って、 を使用しR classて意味のある ID を生成する方が簡単な場合があります。質問の最後に参照する元のファイルが含まれていないxmlため、問題が発生したことしか推測できません。ただし、それは法案に合っているようで、次のようなものを作成できます。

<?xml version="1.0" encoding="utf-8"?>

<TextView 
  android:id="@+id/hellotextview"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:text="Hi there"/>

android:id="@+id/hellotextview"、プロジェクトの他の場所で使用できる ID を生成します。あなたのJavaコードではTextView 、次のようなもので特定のものにアクセスできます:

TextView helloText = (TextView) findViewById(R.id.hellotextview);

R.id.hellotextview、プロジェクトのビルド時に ( で) 自動的に生成される intgen/R.javaですが、名前を選択すると、自分とプロジェクトに関連する名前を割り当てることができます。したがって、前述の"rl1"andなどの文字列値を使用しようとする代わりに、 and"rl2"を使用できます。 R.id.rl1R.id.rl2

個々の UI 要素だけでなく、文字列 ( res/values/strings.xml) や、プロジェクトのres/フォルダーに保存されているその他のリソース (アイコン、メディア ファイルなど) にも同じ手法を使用できます。文字列の場合は、それらにアクセスします。getString(R.string.some_name_given_by_you);

詳細については、Android デベロッパー サイトのAccessing Resourcesを参照してください。

于 2012-06-19T15:55:56.903 に答える
-2

他のアクティビティの他の場所でIDを指定した要素にアクセスする場合に備えて、代わりにSharedPreferencesを使用してみませんか。

于 2012-06-15T16:08:00.823 に答える