2013-10-31 36 views
0

我有一個列表視圖,其中顯示聯繫人姓名。有更多的值與列表視圖相關聯。當我在列表視圖中按下行時,我將Hashmap發送給其他活動,並基於HashMap大小,我想動態創建TextView併爲那些動態創建的文本視圖(如Name,Email,PhoneNo)分配值。創建多個Textviews併爲其動態分配值

listview.OnItemClickListner

mListView.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View view, int position, 
       long id) { 
      // TODO Auto-generated method stub   
      @SuppressWarnings("unchecked") 
      HashMap<String, String> o = (HashMap<String, String>) mListView.getItemAtPosition(position); 
      Intent i = new Intent (Contacts.this , Contacts_Detail.class); 
      i.putExtra("HASHMAP", o); 
      startActivity(i); 


     } 
    }); 

聯繫人詳細信息類

public class Contacts_Detail extends Activity { 

LinearLayout mLinearlayout; 
TextView rowTextView; 
HashMap<String, String> ModuleName; 
ArrayList<String> KEY; 
List<TextView> allTxts; 
private LayoutInflater inflater; 
View layout; 
@SuppressWarnings("unchecked") 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_test); 
    mLinearlayout = (LinearLayout) findViewById(R.id.LinearLayout); 

    Bundle extras = getIntent().getExtras(); 

    if (extras != null) 
    { 
     //map = HashMap<String, String>>getIntent().getSerializableExtra("MODULE_LIST"); 
     ModuleName = (HashMap<String, String>) extras.getSerializable("HASHMAP"); 
     //KEY = (ArrayList<String>) extras.getSerializable("KEY"); 
    } 
    else 
    { 
     Toast.makeText(getApplicationContext(), "No Data Found", Toast.LENGTH_LONG).show(); 
     return; 

    } 

    int N = ModuleName.size(); // total number of textviews to add 
    final TextView[] myTextViews = new TextView[N]; // create an empty array; 
    allTxts = new ArrayList<TextView>(); 

    for (int i = 0; i < N; i++) { 
     // create a new textview 
     rowTextView = new TextView(this); 
     allTxts.add(rowTextView); 
     // set some properties of rowTextView or something 
     rowTextView.setText("This is TextView #" + i); 
     rowTextView.setId(i); 
     // add the textview to the linearlayout 
     mLinearlayout.addView(rowTextView); 

     // save a reference to the textview for later 
     myTextViews[i] = rowTextView; 
    }   

}} 

ScreenSHot 正如你看到的所有textviews創建succesfully.But我想HASHMAP值分配給所有的動態創建文字瀏覽。 如何做到這一點。?

我可以使用佈局充氣器的概念。 ?

在此先感謝

+0

爲什麼不創建一個單一的TextView和使用追加或使用一個ListView – Raghunandan

+0

@Raghunandan我有很多工作要做,這些Textviews後,將數據添加到它。不能使用listview。 –

+0

@BhanuSharma沒有讓你指出。 –

回答

1

試試這個

for循環之前添加

Set<String> keys = ModuleName.keySet(); 
String[] values = new String[N]; 
int i = 0; 
for (String key : keys) { 
    values[i] = ModuleName.get(key); 
    i++; 
} 

然後for循環內,設置文本的TextView

rowTextView.setText(values[i]); 

希望它幫助。

+0

它正在工作,但它設置了Hashmap的keySet。不是他們的價值。 –

+0

@MohitRakhra for(Map.Entry entry:ModuleName.entrySet()){KeyType =(String)entry.getKey(); Log.i(「......」,「」+ ModuleName.get(key)); } }'使用鍵獲取值並設置文本'textview.setText(ModuleName.get(key).toString())' – Raghunandan

+0

@MohitRakhra'values [i]'是關鍵。你需要使用這個鍵來從地圖上獲取值,例如'ModuleName.get(values [i])''rowTextView.setText(ModuleName.get(values [i]))' – Raghunandan

0
Set<String> keys = ModuleName.keySet(); 
String[] values = new String[N]; 
int i = 0; 
for (String key : keys) { 
    values[i] = ModuleName.get(key); 
    i++; 
}  




for (int i=0;i<values .length;i++) 
        { 
         final int index =i; 
         LinearLayout child_insidenew_layout = new LinearLayout(this); 
         child_insidenew_layout.setOrientation(LinearLayout.HORIZONTAL); 
         LinearLayout.LayoutParams child_inside_paramsnew = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 
         child_insidenew_layout.setLayoutParams(child_inside_paramsnew); 
         child_insidenew_layout.setGravity(Gravity.CENTER_VERTICAL); 
         child_insidenew_layout.setBackgroundResource(R.drawable.layout_selector); 

         TextView textview = new TextView(this); 


         LinearLayout.LayoutParams image_params = new LinearLayout.LayoutParams(imageWidth,imgHeight); 

         image_params.setMargins(margin,margin, margin, margin); 




         child_insidenew_layout.addView(textview, image_params); 

         TextView textrootname = new TextView(getActivity()); 
         LinearLayout.LayoutParams TextView_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
         textrootname.setSingleLine(true); 
         textrootname.setEllipsize(TruncateAt.MARQUEE); 
         textrootname.setFocusableInTouchMode(true); 
         textrootname.setFreezesText(true); 
         textrootname.setMarqueeRepeatLimit(-1); 
         textrootname.setFocusable(true); 
         textrootname.setSelected(true); 
textrootname.settext("values indeax of i") 
         textrootname.setGravity(Gravity.CENTER); 
         textrootname.setTextColor(Color.BLACK); 
         textrootname.setTextSize(15); 
         child_insidenew_layout.addView(textrootname, TextView_params); 

         child_inside_layout.addView(child_insidenew_layout, child_inside_paramsnew); 
    } 
+1

你在哪裏使用哈希映射來設置文本到textview – Raghunandan

+0

親愛的我不寫整個代碼只是根據這個for循環讀取你的哈希表,並只寫textview.settext(「你的哈希值」);簡單的親愛的:) –

+1

這是什麼op正在尋找這就是問題是什麼,你沒有回答,親愛的:) – Raghunandan

相關問題