我有一個列表視圖,其中顯示聯繫人姓名。有更多的值與列表視圖相關聯。當我在列表視圖中按下行時,我將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;
}
}}
正如你看到的所有textviews創建succesfully.But我想HASHMAP值分配給所有的動態創建文字瀏覽。 如何做到這一點。?
我可以使用佈局充氣器的概念。 ?
在此先感謝
爲什麼不創建一個單一的TextView和使用追加或使用一個ListView – Raghunandan
@Raghunandan我有很多工作要做,這些Textviews後,將數據添加到它。不能使用listview。 –
@BhanuSharma沒有讓你指出。 –