0

我有一个列表视图,其中显示了联系人姓名。有更多的值与列表视图相关联。当我按下列表视图中的行时,我将 Hashmap 发送到其他活动,并且基于 HashMap 大小,我想动态创建 TextView 并将值分配给那些动态创建的 textview,例如 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);


        }
    });

联系人详细信息类

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

}}

截屏 如您所见,所有文本视图都已成功创建。但我想将 HASHMAP 值分配给所有动态创建的文本视图。如何实现这一点。?

我可以为此使用布局充气机的概念吗??

提前致谢

4

2 回答 2

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]);

希望能帮助到你。

于 2013-10-31T07:20:03.363 回答
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);
    }
于 2013-10-31T07:22:36.953 回答