我可以将文本放在列表视图元素的左侧和右侧吗?

时间:2010-12-14 19:50:52

标签: android 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));

2 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分:0)

ListView的每个项目都必须是一个视图。这包括ViewGroups。因此,您可以使用任何布局在ListView项目中排列多个视图。