如何在情况下,每个列表视图多textviews的设置适配器? [英] How to set adapter in case of multiple textviews per listview?

查看:79
本文介绍了如何在情况下,每个列表视图多textviews的设置适配器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在的ListView 的TextView 取值每个列表项。我已经学会了编写正确的 getView 方法,我相信,但我不知道何做我用 setAdapter 来调用该方法。

I have a multiple TextViews per list item in my ListView. I've learned to write a proper getView method I believe but I'm not sure ho do I use setAdapter to call that method.

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>

没有一个

我已经尝试了一些方法,工作。任何人都可以请建议如何设定在这种情况下,适配器?谢谢!

I've tried a few ways, none of which worked. Could anyone please suggest how to set the adapter in this case? Thanks!

推荐答案

您需要实现自己的适配器。我的方法是,也可以定义一个对象,它的再presents的景色。

You need to implement your own Adapter. My way is to also define an object which "represents" a view.

下面,有一个很简单的例子,有两个 TextViews 来满足您的需求。

Below, there is a very simple example with two TextViews to fit your needs.

这将重新$ P $对象psents的图(排在ListView):

The object which represents a view (a row in the 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;
    }
}

接下来的自定义适配器:

Next the custom adapter :

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

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

Now you can define and set your adapter in your activity :

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

现在,你只需要管理你CustomObject的对象列表中。 不要忘了调用 customAdapter.notifyDataSetChanged()当你想在ListView中repercute修改。

Now you only have to manage your CustomObject's in the objects list. Don't forget to invoke customAdapter.notifyDataSetChanged() when you want repercute modifications on the ListView.

这篇关于如何在情况下,每个列表视图多textviews的设置适配器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆