I have a listview in which I am showing contacts Name. There are more values which are associated to the listview. When I press the row in the listview I am sending the Hashmap to the other activity and based on the HashMap size I want to create the TextViews dynamically and assign values to those dynamically created textviews like Name, Email, PhoneNo.
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);
}
});
Contacts Detail Class
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;
}
}}
As you see all the textviews are created succesfully.But I want to assign HASHMAP values to all the dynamically created textviews. How to achieve this.?
can I use the concept of layout inflater for that. ?
Thanks in advance
Try this
Before your for
loop add
Set<String> keys = ModuleName.keySet();
String[] values = new String[N];
int i = 0;
for (String key : keys) {
values[i] = ModuleName.get(key);
i++;
}
and then inside the for
loop, set text to the TextView
rowTextView.setText(values[i]);
Hope it helps.