【问题标题】:How to set adapter in case of multiple textviews per listview?如果每个列表视图有多个文本视图,如何设置适配器?
【发布时间】:2012-06-21 19:32:20
【问题描述】:

我的ListView 中的每个列表项都有多个TextViews。我已经学会了编写正确的getView 方法,但我不确定我是否使用setAdapter 来调用该方法。

private static String[] project = {"proj1","proj2"};
private static String[] workRequests = {"requirement gathering", "design"};
private static String[] startDate = {"02/21/2012","07/15/2011"};
private static String[] status = {"WIP","DONE"};

ListView mListView;

public class MyDashboardActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mydashboard);

        final LayoutInflater mInflater = LayoutInflater.from(this);
        mListView = (ListView)findViewById(R.id.dashboardList);
        mListView.setAdapter(
                // How do I set the adapter?
                );
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        System.out.println("enters");
        if(convertView == null){
            convertView = LayoutInflater.from(this).inflate(R.layout.mydashboard,null);
        }

        ((TextView) convertView.findViewById(R.id.project)).setText(project[position]);
        ((TextView) convertView.findViewById(R.id.work_request)).setText(workRequests[position]);
        ((TextView) convertView.findViewById(R.id.start_date)).setText(startDate[position]);
        ((TextView) convertView.findViewById(R.id.status)).setText(status[position]);

        return convertView;
    }

这是 xml 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <!-- Include Action Bar -->
    <include layout="@layout/actionbar_layout" />

    <ListView
        android:id="@+id/dashboardList"
        style="@style/LeftHeaderText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:background="@drawable/innerdashboard_bg"
        android:textColor="@color/textColor" >

        <TextView android:id="@+id/project" />

        <TextView android:id="@+id/work_request" />

        <TextView android:id="@+id/start_date" />

        <TextView android:id="@+id/status" />

    </ListView>

</LinearLayout>

我尝试了几种方法,但都没有奏效。谁能建议在这种情况下如何设置适配器?谢谢!

【问题讨论】:

  • 查看这个完整的例子android-example-code.blogspot.in/p/…
  • 缺少一些代码...扩展 baseAdapter 的类在哪里???
  • 您的getView() 是您的Adapter 课程的一部分吗?
  • 我是 android 的新手,我不确定我在做什么。 @gtumca-MAC 就像一个魅力!谢谢。

标签: android android-layout android-listview


【解决方案1】:

您需要实现自己的适配器。我的方法是定义一个“代表”视图的对象。

下面是一个非常简单的示例,其中包含两个TextViews 以满足您的需求。

代表视图的对象(ListView 中的一行):

public class CustomObject {

    private String prop1; 
    private String prop2;

    public CustomObject(String prop1, String prop2) {
        this.prop1 = prop1;
        this.prop2 = prop2;
    }

    public String getProp1() {
        return prop1;
    }

    public String getProp2() {
       return prop2;
    }
}

接下来是自定义适配器:

public class CustomAdapter extends BaseAdapter {

   private LayoutInflater inflater;
  private ArrayList<CustomObject> objects;

   private class ViewHolder {
      TextView textView1;
      TextView textView2;
   }

   public CustomAdapter(Context context, ArrayList<CustomObject> objects) {
      inflater = LayoutInflater.from(context);
      this.objects = objects;
   }

   public int getCount() {
      return objects.size();
   }

   public CustomObject getItem(int position) {
      return objects.get(position);
   }

   public long getItemId(int position) {
      return position;
   }

   public View getView(int position, View convertView, ViewGroup parent) {
      ViewHolder holder = null;
      if(convertView == null) {
         holder = new ViewHolder();
         convertView = inflater.inflate(R.layout.your_view_layout, null);
         holder.textView1 = (TextView) convertView.findViewById(R.id.id_textView1);
        holder.textView2 = (TextView) convertView.findViewById(R.id.list_id_textView2);
         convertView.setTag(holder);
      } else {
         holder = (ViewHolder) convertView.getTag();
      }
      holder.textView1.setText(objects.get(position).getprop1());
      holder.textView2.setText(objects.get(position).getprop2());
      return convertView;
   }
}

现在您可以在活动中定义和设置适配器:

ArrayList<CustomObject> objects = new ArrayList<CustomObject>();
CustomAdapter customAdapter = new CustomAdapter(this, objects);
listView.setAdapter(customAdapter);

现在您只需管理对象列表中的自定义对象。 当您想对 ListView 进行重新修改时,不要忘记调用 customAdapter.notifyDataSetChanged()。

【讨论】:

  • 非常感谢,我正在尝试。
  • 那么我是否必须为我创建的每个新 ListView 创建自定义适配器? (当然有多个TextViews)
  • @Harsh,如果您想要在 ListView 中使用不同的视图,并且如果基本适配器还不够,是的,您必须这样做。请注意,您可以轻松地制作比一些 TextView 更复杂的视图。
  • 或者你可以让你的自定义适配器扩展 ArrayAdapter
  • 我们将如何管理CustomObject?例如,我有一个 HashMap。我将如何填充对象?
【解决方案2】:

您的 getView() 代码需要进入扩展 BaseAdapter 或其子类之一的类。

一种方法是在 MyDashboardActivity 中创建一个私有类。下面是一个简单的示例(需要一些额外的代码)。您可能还需要一个自定义对象来关联您想要在一个列表项中显示的所有内容。不要使用多个数组,而是使用一个自定义类型的数组,该数组具有您要跟踪的每个值的属性。

还有一件事:你的四个 TextView 应该进入它们自己的布局文件(参见 list_item.xml here)。该项目布局文件通过自定义适配器的构造函数连接起来(我在下面的代码中添加了注释以突出显示这一点)。

protected CustomAdapter mAdapter;

public class MyDashboardActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mydashboard);

        final LayoutInflater mInflater = LayoutInflater.from(this);
        mListView = (ListView)findViewById(R.id.dashboardList);

        mAdapter = new CustomAdapter(this, <array to be adapted>);
        mListView.setAdapter(mAdapter);
    }

    private class CustomAdapter extends ArrayAdapter<String> {

        protected Context mContext;
        protected ArrayList<String> mItems;

        public CustomAdapter(Context context, ArrayList<String> items) {
            super(context, R.layout.custom_list_item, items); // Use a custom layout file
            mContext = context;
            mItems = items;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            System.out.println("enters");
            if(convertView == null){
                convertView = LayoutInflater.from(this).inflate(R.layout.mydashboard,null);
            }

            // You'll need to use the mItems array to populate these...
            ((TextView) convertView.findViewById(R.id.project)).setText(project[position]);
            ((TextView) convertView.findViewById(R.id.work_request)).setText(workRequests[position]);
            ((TextView) convertView.findViewById(R.id.start_date)).setText(startDate[position]);
            ((TextView) convertView.findViewById(R.id.status)).setText(status[position]);

            return convertView;
        }
    }
}

【讨论】:

  • 你是个救生员。我爱你
  • 谢谢你,我正在使用它。但是有一个小建议:IntelliJ IDEA 对“inflate(R.layout.mydashboard,null)”发出警告,而这个答案stackoverflow.com/a/24832569/253938 建议将其更改为“inflate(R.layout.mydashboard,parent,false)”。我这样做了,IntelliJ 不再唠叨我,它仍然有效。
【解决方案3】:

有几种方法可以做到这一点。我将向您展示我的方式,它包含两种布局,第一种是 ListView 它的自身,另一种是每个列表项的文本应如何显示。

listset.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/shipMenu"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

listitems.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="horizontal">

    <TextView
        android:id="@+id/makerID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="25dp"/>

</LinearLayout>

上面我还没有艺术作品,但我打算为图标添加一个 ImageView(你也可以添加更多的 TextViews)。这是我的自定义适配器类。

ListAdapter.java

class ListAdapter extends ArrayAdapter <String>
{

    public ListAdapter(Context context, String[] values) {

        super(context, R.layout.listitems, values); //set the layout that contains your views (not the one with the ListView)
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view = inflater.inflate(R.layout.listitems, parent, false); //same here.

        String text = getItem(position);

        TextView makerID = (TextView) view.findViewById(R.id.makerID);
        makerID.setText(text);

        return view;
    }

}

在你的主要活动文件集中

setContentView (R.layout.listset);

并将其添加到与 setContentView() 相同的括号中

ListAdapter adapter = new ListAdapter(this, MyString[]); //place your String array in place of MyString

        ListView lv = (ListView) findViewById(R.id.ListViewID); //the ID you set your ListView to.
        lv.setAdapter(adapter);

编辑

看起来我参加聚会有点晚了,但也许这会对某人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 2015-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2013-01-22
    相关资源
    最近更新 更多