2011-05-02 173 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的。

相關問題