2011-05-02 164 views
0

我想创建一个listView,但我想在每个listitem有2个不同的textViews和一个按钮..这可能吗?是否有可能在一个listView项目中有两个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; 


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


        } 
       } 
      }); 

    } 
} 
+0

其他人请吗? – kostas 2011-05-02 10:23:12

回答

0

是的,只是包装他们的布局,例如LinearLayout

这里是an example - 寻找行布局。

+0

正如你可以在代码中看到的我使用一个字符串与每个列表项的名称...如果我想在每个列表项中有两行我必须有2个字符串或我不能使用字符串呢? – kostas 2011-05-02 10:01:57

+0

使用SimpleAdapter或创建自己的扩展ListAdapter – 2011-05-02 12:17:28

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的。

相关问题