Android 向ListView添加项和子项

Android 向ListView添加项和子项,android,android-listview,Android,Android Listview,下面的XML文件只包含一个ListView <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <List

下面的XML文件只包含一个
ListView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView android:id="@+id/lview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        ></ListView>


</RelativeLayout>

如果要用每行中的许多数据项填充数组列表,则必须实现自定义适配器和
getView
方法,如下所示:

    public class tasksRepositoryAdapter extends ArrayAdapter<Task>
{   
    private ArrayList<Task> list;

    public tasksRepositoryAdapter(Context context, int textViewResourceId, List<Task> tasksRepository) 
    {
        super(context, textViewResourceId, tasksRepository);
         this.list = new ArrayList<Task>();
         for (Task task : tasksRepository)
            {
                this.list.add(task);
            }
    }

    public View getView(final int position, View convertView, ViewGroup parent)
    {
        View row;
        final ViewHolder holder = new ViewHolder();
        tfRobotoRegular = Typeface.createFromAsset(getAssets(),"Roboto-Regular.ttf");

        LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflator.inflate(R.layout.new_row, null);
        holder.tvTitle = (TextView) row.findViewById(R.id.text_title);
        String title = tasksRepository.get(position).getTitle();
        if (title.length()>25)
        {
            title = title.substring(0, 24);
            title = title + "...";
        }
        holder.tvTitle.setText(title);
        holder.tvTitle.setTypeface(tfRobotoRegular);
        holder.tvDate = (TextView) row.findViewById(R.id.text_date);
        holder.tvDate.setText(tasksRepository.get(position).getDate());
        holder.tvDate.setTypeface(tfRobotoRegular);
        holder.tvTime = (TextView) row.findViewById(R.id.text_time);
        holder.tvTime.setText(tasksRepository.get(position).getTime());
        holder.tvTime.setTypeface(tfRobotoRegular);
        holder.tvDescription = (TextView) row.findViewById(R.id.text_description);
        String description = tasksRepository.get(position).getDescription();
        if (description.length()>46)
        {
            description = description.substring(0, 45);
            description = description + "...";
        }
        holder.tvDescription.setText(description);
        holder.tvDescription.setTypeface(tfRobotoRegular);
        holder.tvId = (TextView) row.findViewById(R.id.text_id);
        holder.tvId.setText(String.valueOf(tasksRepository.get(position).getId()));
        holder.tvId.setTypeface(tfRobotoRegular);
        holder.tvLocation = (TextView) row.findViewById(R.id.text_location);
        holder.tvLocation.setText(tasksRepository.get(position).getCity());
        holder.llRowLayout = (LinearLayout) row.findViewById(R.id.llRowLayout);
        holder.imCalendar = (ImageView) row.findViewById(R.id.iCalendar);
        holder.imClock = (ImageView) row.findViewById(R.id.iClock);
        holder.imLocation = (ImageView) row.findViewById(R.id.iLocation);

        holder.imTaskStatusButton = (ImageView) row.findViewById(R.id.iTaskStatusButton);
        holder.imTaskStatusButton.setTag(position);
        holder.imTaskStatusButton.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {
                 int[] location = new int[2];
                 currentRowId = position;
                 currentRow = v;    
                 // Get the x, y location and store it in the location[] array
                 // location[0] = x, location[1] = y.
                 v.getLocationOnScreen(location);

                 //Initialize the Point with x, and y positions
                 point = new Point();
                 point.x = location[0];
                 point.y = location[1];
                 showStatusPopup(TasksListActivity.this, point);
            }
        });     
        String status = tasksRepository.get(position).getStatus();
        Log.d(TAG, "The status of the current row: "+ status );
        setStatusColorImages(status, holder.imClock, holder.imCalendar, holder.imLocation, holder.llRowLayout); 

        return row;
    }
}

在这种情况下。

首先阅读本教程:仅供参考:Emil正在使用一个视图保持器,它只是自定义适配器类中的一个类。强烈建议快速传递信息。@Mark,谢谢你的帮助,我还添加了ViewHolder代码。你将ViewHolder设置为静态的是什么?如果Romain Guy说它必须是静态的:,那么它必须是静态的:)太复杂和无法解释。为什么ArrayAdapter而不是SimpleAdapter?因为我在每个条目中使用多行
    public class tasksRepositoryAdapter extends ArrayAdapter<Task>
{   
    private ArrayList<Task> list;

    public tasksRepositoryAdapter(Context context, int textViewResourceId, List<Task> tasksRepository) 
    {
        super(context, textViewResourceId, tasksRepository);
         this.list = new ArrayList<Task>();
         for (Task task : tasksRepository)
            {
                this.list.add(task);
            }
    }

    public View getView(final int position, View convertView, ViewGroup parent)
    {
        View row;
        final ViewHolder holder = new ViewHolder();
        tfRobotoRegular = Typeface.createFromAsset(getAssets(),"Roboto-Regular.ttf");

        LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflator.inflate(R.layout.new_row, null);
        holder.tvTitle = (TextView) row.findViewById(R.id.text_title);
        String title = tasksRepository.get(position).getTitle();
        if (title.length()>25)
        {
            title = title.substring(0, 24);
            title = title + "...";
        }
        holder.tvTitle.setText(title);
        holder.tvTitle.setTypeface(tfRobotoRegular);
        holder.tvDate = (TextView) row.findViewById(R.id.text_date);
        holder.tvDate.setText(tasksRepository.get(position).getDate());
        holder.tvDate.setTypeface(tfRobotoRegular);
        holder.tvTime = (TextView) row.findViewById(R.id.text_time);
        holder.tvTime.setText(tasksRepository.get(position).getTime());
        holder.tvTime.setTypeface(tfRobotoRegular);
        holder.tvDescription = (TextView) row.findViewById(R.id.text_description);
        String description = tasksRepository.get(position).getDescription();
        if (description.length()>46)
        {
            description = description.substring(0, 45);
            description = description + "...";
        }
        holder.tvDescription.setText(description);
        holder.tvDescription.setTypeface(tfRobotoRegular);
        holder.tvId = (TextView) row.findViewById(R.id.text_id);
        holder.tvId.setText(String.valueOf(tasksRepository.get(position).getId()));
        holder.tvId.setTypeface(tfRobotoRegular);
        holder.tvLocation = (TextView) row.findViewById(R.id.text_location);
        holder.tvLocation.setText(tasksRepository.get(position).getCity());
        holder.llRowLayout = (LinearLayout) row.findViewById(R.id.llRowLayout);
        holder.imCalendar = (ImageView) row.findViewById(R.id.iCalendar);
        holder.imClock = (ImageView) row.findViewById(R.id.iClock);
        holder.imLocation = (ImageView) row.findViewById(R.id.iLocation);

        holder.imTaskStatusButton = (ImageView) row.findViewById(R.id.iTaskStatusButton);
        holder.imTaskStatusButton.setTag(position);
        holder.imTaskStatusButton.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {
                 int[] location = new int[2];
                 currentRowId = position;
                 currentRow = v;    
                 // Get the x, y location and store it in the location[] array
                 // location[0] = x, location[1] = y.
                 v.getLocationOnScreen(location);

                 //Initialize the Point with x, and y positions
                 point = new Point();
                 point.x = location[0];
                 point.y = location[1];
                 showStatusPopup(TasksListActivity.this, point);
            }
        });     
        String status = tasksRepository.get(position).getStatus();
        Log.d(TAG, "The status of the current row: "+ status );
        setStatusColorImages(status, holder.imClock, holder.imCalendar, holder.imLocation, holder.llRowLayout); 

        return row;
    }
}
static class ViewHolder
{
    RelativeLayout rlTitle;
    LinearLayout llRowLayout;
    TextView tvId;
    TextView tvTitle;
    TextView tvDate;
    TextView tvTime;
    TextView tvDescription;
    TextView tvLocation;
    ImageView imClock;
    ImageView imCalendar;
    ImageView imLocation;
    ImageView imTaskStatusButton;
}