0

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

我能够在所有列表视图中显示 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 如下

4

2 回答 2

0

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

功能示例

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

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

主.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;    
    }

}

用户记录.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;
    }
}
于 2012-04-24T06:50:32.907 回答
0

请参阅提出的同类问题

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

于 2012-04-23T11:30:30.953 回答