是否可以在listView项目中有两个TextView?

时间:2011-05-02 09:53:13

标签: android

我想创建一个listView,但我希望在每个listitem中都有2个不同的textView和一个按钮..这可能吗?

我使用行xml文件作为列表项..

public class main extends Activity {
    private ListView lv1;

    private String lv_arr[]={"a","b","c","d"};

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main); 





        lv1=(ListView)findViewById(R.id.list);



        lv1.setAdapter(new ArrayAdapter<String>(this,R.layout.row , lv_arr));


        lv1.setOnItemClickListener(new OnItemClickListener() {
             public void onItemClick(AdapterView<?> parent, View view,
                      int position, long id) {
                    switch( position )
                    {
                       case 0:  Intent newActivity = new Intent(chania.this, cafe.class);     

                        break;


                      //...........


                    }
                }
          });

    }
}

2 个答案:

答案 0 :(得分:0)

是的,只需将它们包装在布局中,例如LinearLayout

这是an example - 查找行布局。

答案 1 :(得分:0)

创建自己的自定义适配器,扩展Base Adapter。 尝试下面的代码(我使用了7个textview,包装在一个自定义的布局xml文件中):

public class Received_invitationAdapter extends BaseAdapter{
Context ctx_invitation;

public Received_invitationAdapter(Context ctx_invitation)
{
    super();
    this.ctx_invitation = ctx_invitation;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return PartyName.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return PartyName[position];
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View v = null;
    try
    {
        String inflater = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater li = (LayoutInflater)ctx_invitation.getSystemService(inflater);
        v = li.inflate(R.layout.receivedinvitations, null);

        TextView tv_partyname = (TextView)v.findViewById(R.id.tv_receivedinvitation_PartyTitle);
        TextView tv_partydate = (TextView)v.findViewById(R.id.tv_receivedinvitation_date);
        TextView tv_partytime = (TextView)v.findViewById(R.id.tv_receivedinvitation_time);
        TextView tv_partylocation = (TextView)v.findViewById(R.id.tv_receivedinvitation_PartyLocation);
        TextView img_chkbox = (TextView)v.findViewById(R.id.img_chkbox_receivedinvitation);
        TextView img_inv_accepted = (TextView)v.findViewById(R.id.img_accept_receivedinvitation);
        TextView img_inv_rejected = (TextView)v.findViewById(R.id.img_reject_receivedinvitation);

        tv_partyname.setText(PartyName[position]);
        tv_partydate.setText(PartyDate[position]);
        tv_partytime.setText(PartyTime[position]);
        tv_partylocation.setText(PartyLocation[position]);
        System.out.println(""+img_chkbox.getVisibility());
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return v;
}

receivedinvitations.xml文件包含getview方法中定义的7 textview。