如果片段中存在每个列表视图的多个文本视图,如何设置自定义适配器

时间:2016-07-18 15:45:31

标签: android listview android-fragments

我在片段中有一个列表视图。我想设置那些多个textview。那么我应该如何设置自定义适配器。我是android的新手,需要一些帮助。

public class Tab1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.tab_1, container, false);

        ListView listView = (ListView)v.findViewById(R.id.listOfTasks);

//need to pass 2 string arrays.
//string[] s1;
//string[] s2; 

 ListAdapter taskAdapter = new TaskCustomAdapter(  //here.....  );
        listView.setAdapter(taskAdapter);

        listView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String task = String.valueOf(parent.getItemAtPosition(position));
                        Toast.makeText(getActivity(),task,Toast.LENGTH_SHORT).show();
                    }
                }
        );

        return v;
    }

}

这是我的列表视图,包含多个文本字段。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="60dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/task_title"
            android:layout_gravity="left|center_vertical"
            android:layout_marginLeft="20dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/task_subtitle"
            android:layout_gravity="center" />


    </FrameLayout>

</LinearLayout>

我目前的适配器是:

public class TaskCustomAdapter extends ArrayAdapter<String>{

    public TaskCustomAdapter(Context context,String[] tasks) {
        super(context,R.layout.taskfragment,tasks);

    }

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

        LayoutInflater task_inflater = LayoutInflater.from(getContext());
        View customView = task_inflater.inflate(R.layout.taskfragment,parent,false);

        String task = getItem(position);
        TextView title = (TextView)customView.findViewById(R.id.task_title);
        title.setText(task);

        return customView;
    }
}

谢谢!

3 个答案:

答案 0 :(得分:0)

你走在正确的轨道上!你自己几乎完成了它。只需在此初始化所有组件(TextViews或其他内容),就像使用TextView一样:

TextView title = (TextView)customView.findViewById(R.id.task_title);
TextView subtitle = (TextView)customView.findViewById(R.id.task_subtitle);
//etc for every one
title.setText(task);
subtitle.setText("whatever");

只需像往常一样使用它们。请考虑将ViewHolder pattern用于此类事情,请在链接问题上详细了解它。

修改

你问的另一件事,如何传递2个数组,只需改变构造函数如下:

private String[] subtitles; //or whatever

public TaskCustomAdapter(Context context,String[] tasks, String[] subs) {
    super(context,R.layout.taskfragment,tasks);
    subtitles = subs;
}

现在只需使用附加参数调用构造函数,如:

ListAdapter taskAdapter = new TaskCustomAdapter(getActivity(), s1, s2);

s1s2是您要传递的字符串数组。您现在可以使用第二个textView:

subtitle.setText(subtitles[position]);

答案 1 :(得分:0)

Below code put in activity oncreate::: 

//List view reference
    ListView lv=(ListView) findViewById(R.id.listView);
//Creating the custom adapter and passing the string array data
     MainListViewCustomAdapter adapter = new MainListViewCustomAdapter(this,titleList,subtitlelist)
//Setting the adapter to listview
       lv.setAdapter(adapter);

答案 2 :(得分:-1)

package com.ultimate.gre.prep.adapters;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;



public class MainListViewCustomAdapter extends BaseAdapter {

    private String[] mTitleListToDisplay;
    private String[] mSubTitleListToDisplay;
    private Context mContext;

    //through constructor you can send the data
    //if you want any more data ,then please add more parameters to constructor
    public MainListViewCustomAdapter(Context context,String[] titleListToDisplay,String[] subTitleListToDisplay){
        mContext =  context;
        mTitleListToDisplay = titleListToDisplay;
        mSubTitleListToDisplay =subTitleListToDisplay;
    }
    @Override
    public int getCount() {
        return mTitleListToDisplay.length();
    }

    @Override
    public Object getItem(int position) {
        return mTitleListToDisplay.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder holder = null;

          LayoutInflater mInflater = (LayoutInflater) mContext
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {
            //For the first time it will execute
                convertView = mInflater.inflate(R.layout.main_activity_custom_row_leyout, null);
                holder = new ViewHolder();
                holder.txtTitle = (TextView) convertView.findViewById(R.id.task_title);
                holder.subTitleTextView = (TextView) convertView.findViewById(R.id.task_subtitle);
                convertView.setTag(holder);
            } else {
            //second time onwards this case will execute
                holder = (ViewHolder) convertView.getTag();
            }

            holder.titleTextView.setText(mTitleListToDisplay[position]);
            holder.subTitleTextView.setText(mSubTitleListToDisplay[position]);

            return convertView;
    }


    private class ViewHolder {
       TextView titleTextView;
        TextView subTitleTextView;
    }

}