2011-05-02 15 views
0

私はlistViewを作成したいと思いますが、私はすべてのlistitemに2つの異なるtextViewとボタンを持たせたいと考えていますか?listViewアイテムに2つのTextViewを含めることは可能ですか?

私は

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

答えて

0

はい、単なる例LinearLayoutため、レイアウトでそれらをラップ..私のリスト項目の行のXMLファイルを使用してメートル。

ここにan example - 行レイアウトを探します。

+0

あなたがコードで見ることができるように、各リストアイテムの名前の文字列を使用しています...もし私が2つの文字列を持っている必要がありますか? – kostas

+0

SimpleAdapterを使用するか、独自の拡張ListAdapterを作成してください –

0

ベースアダプタを拡張する独自のカスタムアダプタを作成します。 (Iは、カスタマイズされたレイアウトXMLファイルに包ま7のTextViewを、使用している)以下のコードを試してください:

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を含有します。

関連する問題