0

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;
    }           

}}

ScreenSHot 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

Abhishek Patel
  • 4,280
  • 1
  • 24
  • 38
Mohit Rakhra
  • 114
  • 1
  • 15

2 Answers2

1

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.

AnujMathur_07
  • 2,586
  • 2
  • 18
  • 25
  • It is working but it is setting the keySet of the Hashmap. Not their values. – Mohit Rakhra Oct 31 '13 at 07:31
  • @MohitRakhra `for(Map.Entry entry: ModuleName.entrySet()){ String key = (String) entry.getKey(); Log.i("......",""+ModuleName.get(key)); } }` use the key to get the value and set text like `textview.setText(ModuleName.get(key).toString())` – Raghunandan Oct 31 '13 at 07:32
  • @MohitRakhra `values[i]` is the key. you need to use the key to get the value from the map like `ModuleName.get(values[i])` `rowTextView.setText(ModuleName.get(values[i]))` – Raghunandan Oct 31 '13 at 07:34
  • @MohitRakhra 1 more suggestion why do you need so much textviews. it is not good and what is that you intend to do to your textview later. I suggest listview and it recycles views – Raghunandan Oct 31 '13 at 07:37
  • 1
    @Raghunandan: yeah,I forgot to get the values from the key. my bad. Editing my answer. Thanks – AnujMathur_07 Oct 31 '13 at 07:39
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);
    }
Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
  • 1
    where do you use the hash map to set text to textview – Raghunandan Oct 31 '13 at 07:40
  • dear i m not write whole code just read your hashmap according to this for loop and just write textview.settext("your value of hash"); simple dear :) – Bhanu Sharma Oct 31 '13 at 07:42
  • 1
    that is what op is looking for that is what the question is and you have not answered that dear :) – Raghunandan Oct 31 '13 at 07:42
  • Set keys = ModuleName.keySet(); String[] values = new String[N]; int i = 0; for (String key : keys) { values[i] = ModuleName.get(key); i++; } – Bhanu Sharma Oct 31 '13 at 07:44
  • i suppose that he is also looking for how to dynamically add that view so that time i m quite busy so not getting proper so i give all code :) – Bhanu Sharma Oct 31 '13 at 07:46