如何获取listview数据作为3 2 1在android中显示的textviews

时间:2012-04-23 11:03:37

标签: android listview

我是android新手。我今天尝试的是获取列表视图显示数据为第一行中的3个文本视图和第二个中的2个文本视图和最后一个文本视图。为此我在我的代码中尝试了这个请帮帮我。

我能够在所有列表视图中显示3行textview,但不能显示3 2 1 textview。  请建议我如何做到这一点。 请帮帮我 在我的主要活动中

ListView lview3;  
 ListView3Activity adapter;  

    private static String month[] = {"January","February","March","April","May",  
        "June","July","August","September",  
        "October","November","December"};  

    private static String desc[] = {"Month - 1","Month - 2","Month - 3",  
        "Month - 4","Month - 5","Month - 6","Month - 7",  
        "Month - 8","Month - 9","Month - 10","Month - 11","Month - 12"};  
    private static String details[] = {"Month - 1","Month - 2","Month - 3",  
        "Month - 4","Month - 5","Month - 6","Month - 7",  
        "Month - 8","Month - 9","Month - 10","Month - 11","Month - 12"};  


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    lview3 = (ListView) findViewById(R.id.listView1);  
    adapter = new ListView3Activity(this, month, desc,details);  
    lview3.setAdapter(adapter);  

    lview3.setOnItemClickListener(this);  
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "Title => "+month[position]+" n Description => "+desc[position], Toast.LENGTH_SHORT).show();  

}

}

listView3Activity如下

2 个答案:

答案 0 :(得分:0)

请参阅same kind of question询问。

在您的情况下,您需要创建一个包含所需布局的新xml文件。然后将该布局作为视图传递到每个新列表项中。将数据数组传递给示例中显示的d。

答案 1 :(得分:0)

您是否需要如下屏幕截图所示的功能?

functionality example

如果是,那么这可以通过多种方式实现。其中一个如下。

  1. 使用三个textField为ListView中的项目定义布局文件。 (在我提供的方法中,我创建了list_item_layout.xml,您可以参考此处了解详细信息)
  2. 根据需要向listview的适配器提供数据(例如我创建了自定义适配器,它采用UserRecord类型的arrayList和context作为参数)
  3. UserRecord只是一个包含三个字符串的类,将在listview项目中显示。
  4. 将适配器设置为列表视图。
  5. 请查看代码以获取详细信息,代码不言自明。
  6. main.xml中

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello" />
    
        <Button
            android:id="@+id/sendNotification"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SendNotification" />
    
        <ListView
            android:id="@+id/MyList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
    
    </LinearLayout>
    

    list_item_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_height="wrap_content" 
       android:gravity="left|center"
       android:layout_width="wrap_content" 
       android:paddingBottom="5px"
       android:paddingTop="5px" 
       android:paddingLeft="5px">
    
        <ImageView
           android:id="@+id/avatar"
           android:layout_width="wrap_content"
           android:layout_height="fill_parent"
           android:layout_marginRight="6dip"
           android:src="@drawable/ic_launcher" />
        <LinearLayout
           android:orientation="vertical"
           android:layout_width="0dip"
           android:layout_weight="1"
           android:layout_height="fill_parent">
    
            <TextView android:id="@+id/username"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:gravity="center"/>
    
            <TextView android:id="@+id/middlename" 
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content"/>
    
            <TextView android:id="@+id/lastname" 
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content"/> 
        </LinearLayout>
    </LinearLayout>
    

    listView(CustomAdapter.java)的自定义适配器

    import java.util.ArrayList;
    
    import com.main.UserRecord;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;
    
    public class CustomAdapter extends BaseAdapter {
    
        private ArrayList<UserRecord> _data;
        Context _c;
        CustomAdapter (ArrayList<UserRecord> data, Context c){
            _data = data;
            _c = c;
        }
        /* (non-Javadoc)
         * @see android.widget.Adapter#getCount()
         */
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return _data.size();
        }
    
        /* (non-Javadoc)
         * @see android.widget.Adapter#getItem(int)
         */
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return _data.get(position);
        }
    
        /* (non-Javadoc)
         * @see android.widget.Adapter#getItemId(int)
         */
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        /* (non-Javadoc)
         * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
             View v = convertView;
             if (v == null) 
             {
                LayoutInflater vi = (LayoutInflater)_c.getSystemService(_c.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.list_item_layout, null);
    
               TextView username = (TextView)v.findViewById(R.id.username);
               TextView middlename = (TextView)v.findViewById(R.id.middlename);
               TextView lastname = (TextView)v.findViewById(R.id.lastname);
    
               UserRecord user = _data.get(position);
               username.setText(user.userName);
               middlename.setText(user.middleName);
               lastname.setText(user.lastName);
             }
            return v;    
        }
    
    }
    

    UserRecord.java

    package com.main;
    
    public class UserRecord {
        public String userName;
        public String middleName;
        public String lastName;
    
        public UserRecord(String username, String middleName,String lastName) {
            this.userName = username;
            this.middleName = middleName;
            this.lastName = lastName;
        }
    }