2012-06-15 21 views
3

我正在開發一個應用程序在Android上,我正在循環中生成UI元素。但我需要這些元素以字母和數字來標識,例如"rl1""rl2"。我試圖使用方法RelativeLayout.setId(),但該方法只接受int。有沒有一種方法可以根據需要設置ID而不受數字限制?如何爲UI元素分配字符串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文件中,但是它真的很長,因爲列表中有大約四十個項目,對於我來說,例如在所有這些項目中改變一個字母都很複雜。我想出了這個想法,在運行時在for loop中生成它們。與此同時,我正在測試10個,但我無法完成它的工作。

如果我做的不正確,請原諒我,但我對此很陌生。

回答

-2

爲什麼不嘗試使用SharedPreferences作爲替代方案,以防您想要訪問在某些其他活動的其他地方提供某些ID的元素。

+0

我是這個新手,我不知道你的意思是由SharedPreferences – jorgerodriguezhn

+0

檢查這個鏈接如何使用它:http://developer.android.com/guide/topics/data/data-storage。 html #pref –

+0

@jerisalan如果您仔細查看代碼,生成的元素是佈局,ImageView和其他UI元素。我不認爲SharedPreferences是真正的存儲這些ID的方式。 – Chilledrat

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是當項目建成(在gen/R.java)自動生成的INT,但是當你挑選名字,你可以分配他們與你和你的項目有關。因此,您可以使用R.id.rl1R.id.rl2,而不是嘗試使用字符串值,例如您提到的"rl1""rl2"

以及個人的UI元素,你也可以使用字符串相同的技術(在res/values/strings.xml),以及項目的res/文件夾下存儲的其他資源,如圖標,媒體文件等。在字符串的情況下,你將訪問它們getString(R.string.some_name_given_by_you);

請參閱Android Developers站點上的Accessing Resources以瞭解更多信息。

+0

事情是,當我設置onclicklistener時,在方法中,我獲得被點擊的textview的ID,然後我用Web視圖調用活動,並根據被點擊的textview的ID,它將加載僅與特定文本視圖相關的某個本地html文件。所以,這就是爲什麼我在運行時生成textview時試圖設置id的原因。我最終所做的只是將Id設置爲1,2,3,4,然後將該ID發送到Web視圖。如果這是有道理的,但不是這樣的。 – jorgerodriguezhn