2010-12-14 27 views
2

我必須做一個列表視圖,有一個名單的列表,也是,對齊到左側,但在同一領域,性別的人,男性或女性我可以把文本放在listview元素的左邊和右邊嗎?

有可能做到這一點嗎?怎麼樣?

代碼示例歡迎

編輯

我嘗試與第一用戶答案,我得到這個異常:12-14 22:39:56.191: ERROR/AndroidRuntime(917): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

這是XML項目我做的代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 
     <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Left side" 
     android:layout_alignParentLeft="true"/> 
     <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Right side" 
     android:layout_alignParentRight="true"/> 
</RelativeLayout> 

這是代碼,我有我的清單:

public class PendingInvitations extends ListActivity { 

...... 
..... 
....  
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    private List<String> usernames=new ArrayList<String>(); 
     for (int i=0;i<friends.size();i++) 
       { 
       usernames.add(i,friends.get(i).getFullName()); 
       } 


       setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, usernames)); 
+0

我可以讓列表,這樣用,但我不知道如何把另一個文本在相同的元素,但右對齊 – NullPointerException 2010-12-14 19:54:28

回答

2

這將是用於每個細胞

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > 
     <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Left side" android:layout_alignParentLeft="true"/> 
     <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Right side" android:layout_alignParentRight="true"/> 
    </RelativeLayout> 

這個觀點是一個例子,因爲我不知道在哪裏你的知識是在與列表,如果上面的XML被稱爲「temp.xml」你會在setlistadapter功能

import android.app.ListActivity; 
    import android.os.Bundle; 
    import android.widget.ArrayAdapter; 

    public class FooList extends ListActivity extends BaseAdapter { 
     String[] listItems = {"item 1", "item 2 ", "list", "android", "item 3", "foobar", "bar", }; 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.layout_with_listview); 

      // implement your own adapter 

     } 

    } 
public View getView(int position, View convertView, ViewGroup parent) { 
       // A ViewHolder keeps references to children views to avoid unneccessary calls 
       // to findViewById() on each row. 
       ViewHolder holder; 

       // When convertView is not null, we can reuse it directly, there is no need 
       // to reinflate it. We only inflate a new View when the convertView supplied 
       // by ListView is null. 
       if (convertView == null) { 
        convertView = mInflater.inflate(R.layout.temp, null); 

        // Creates a ViewHolder and store references to the two children views 
        // we want to bind data to. 
        holder = new ViewHolder(); 
        holder.left = (TextView) convertView.findViewById(R.id.left); 
        holder.right = (TextView) convertView.findViewById(R.id.right); 

        convertView.setTag(holder); 
       } else { 
        // Get the ViewHolder back to get fast access to the TextView 
        // and the ImageView. 
        holder = (ViewHolder) convertView.getTag(); 
       } 

       // Bind the data efficiently with the holder. 

       holder.left.setText("left text"); 
       holder.right.setText("right text"); 

       return convertView; 
      } 

    class ViewHolder 
    { 
     public TextView left; 
     public TextView right; 
    } 
+0

以及如何在字段的右側寫上我想要的內容? – NullPointerException 2010-12-14 22:37:37

+0

它也崩潰了...我編輯第一篇文章的崩潰和代碼我嘗試 – NullPointerException 2010-12-14 22:40:56

+0

你需要實現你自己的列表適配器並實現「getView」函數,看看這個例子http://developer.android.com /resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html,並密切關注getView函數,例如 「convertView = mInflater.inflate(R.layout。 list_item_icon_text,null);「 這就是你想要使用temp.xml佈局的地方 – slim 2010-12-14 22:57:22

0

ListView的每個項目必須是一個視圖。這包括ViewGroups。因此,您可以使用任何佈局來排列ListView項目中的多個視圖。

+0

哇,ü代碼示例有關,但工作? – NullPointerException 2010-12-14 19:57:00

+0

看看斯利姆的回答。 – 2010-12-14 20:24:08

相關問題