android 列表子项,android-将项目和子项目添加到ListView

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

public class tasksRepositoryAdapter extends ArrayAdapter

{

private ArrayList list;

public tasksRepositoryAdapter(Context context, int textViewResourceId, List tasksRepository)

{

super(context, textViewResourceId, tasksRepository);

this.list = new ArrayList();

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;

}

}

和ViewHolder:

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;

}

在这种情况下.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值