2013-10-31 41 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

相关问题