在动态listview android中附加多个值

时间:2012-03-27 06:07:10

标签: android listview

我对listview android有疑问。我从服务中获得3个动态值。我正在使用自己的Arraylist Adpater。我有3个textviews。我能够从服务中获取数据。我需要知道如何将3个值放在listview的单个textview中。

示例代码:

buddyNameList[i] = r.getName() + status; 
            //buddyNameLists[i] = status;
                //RowData rd;
                for(i=0;i<buddyNameList[i].length();i++){



                //buddyJidList[i] = r.getUser();
                // Intent myIntent = new Intent(this,DataHelper.class);
                // myIntent.putExtra("UJID", mydata);
                // startActivity(myIntent);
                // dh.insertBuddy(r.getUser(),r.getName());
                i++;
                ListAdapter adapter = new ArrayAdapter<String>(this,
                        R.layout.custom_row_view,R.id.name     ,buddyNameList); // here i can append one value in 1 textview. i need to display all 3            //values from service in 3 individual textview in listview

                lv.setAdapter(adapter);

我需要知道如何将所有3个数据的值放在listview中的相应单元格中 请指导我如何实现结果。

提前致谢。     }

2 个答案:

答案 0 :(得分:1)

你可以试试这个。我们可以动态添加许多文本视图。

String[] name =
    {
        "Apple",
        "Banana",
        "Orange",
        "Mango",
        "Grapes",
        "Jack Fruit",
        "Strawberry",
        "cucumber",
        "pumpkin",
        "pine Apple"
    };
String[] age =
    {
        "20",
        "21",
        "22",
        "23",
        "24",
        "25",
        "26",
        "27",
        "28",
        "29"
    };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


   TableLayout tv=(TableLayout) findViewById(R.id.table1);
    tv.removeAllViewsInLayout();

    int Fruits_length=name.length;
    int i=0;

    while(i<Fruits_length)
    {


    String str1 = "<font color=#0099ff>Name</font> <br/> <font color=#ffffff> "+name[i]+" </font>";

    String str2="<font color=#0099ff>Age</font> <br/> <font color=#ffffff> "+age[i]+" </font>";


    final TableRow tr=new TableRow(Listview2Activity.this);

        tr.setLayoutParams(new LayoutParams(
                   LayoutParams.FILL_PARENT,
                   LayoutParams.WRAP_CONTENT));


             tr.setId(i);



            final TextView b1=new TextView(Listview2Activity.this);
               b1.setTextSize(15);
               b1.setText(Html.fromHtml(str1));
               b1.setWidth(200);
            tr.addView(b1);

            final TextView b2=new TextView(Listview2Activity.this);
            b2.setTextSize(15);
            b2.setText(Html.fromHtml(str2));
            b2.setWidth(200);
         tr.addView(b2);


             tr.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape));
           tv.addView(tr);


        final View vline1 = new View(Listview2Activity.this);
      vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
      vline1.setBackgroundColor(Color.GREEN);
      tv.addView(vline1);      


                tr.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {


                        String e=String.valueOf(tr.getId());

                        Toast.makeText(getApplicationContext(), e, Toast.LENGTH_SHORT).show(); 


                    }
                });

    i=i+1;
    }


}

}

答案 1 :(得分:0)

使用自定义BaseAdapter。在适配器的getView中,膨胀您的布局并为textViews提供值。请参阅http://developer.android.com/reference/android/widget/BaseAdapter.html