添加项目和子项到ListView [英] Adding Items and Subitems to a ListView

查看:154
本文介绍了添加项目和子项到ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>    < ListView的机器人:ID =@ + ID / LVIEW
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        >< /&的ListView GT;
< / RelativeLayout的>

我想物项和子项添加到该ListView的。

我的应用程序,使某一种根据用户的搜索请求,并得到DATAS,我想取回那些DATAS是在ListView中可见。
有没有简单的方法,如:

 保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    LV1 =(ListView控件)findViewById(R.id.lview);
    lv1.add(项目,分项,0)//其中0是位置..任何像这样的方法?
}


解决方案

如果您想要填充,你必须实现自定义适配器,并在每一行中多个数据项的数组列表中的 getView 方法,像这样:

 公共类tasksRepositoryAdapter扩展ArrayAdapter<任务>
{
    私人的ArrayList<任务>清单;    公共tasksRepositoryAdapter(上下文的背景下,INT textViewResourceId,列表与LT;任务> tasksRepository)
    {
        超(背景下,textViewResourceId,tasksRepository);
         this.list =新的ArrayList<任务>();
         对于(任务任务:tasksRepository)
            {
                this.list.add(任务);
            }
    }    公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup)
    {
        查看排;
        最后ViewHolder持有人=新ViewHolder();
        tfRobotoRegular = Typeface.createFromAsset(getAssets(),的Roboto-Regular.ttf);        LayoutInflater充气=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        行= inflator.inflate(R.layout.new_row,NULL);
        holder.tvTitle =(TextView中)row.findViewById(R.id.text_title);
        字符串title = tasksRepository.get(位置).getTitle();
        如果(title.length()→25)
        {
            标题= title.substring(0,24);
            标题=标题+...;
        }
        holder.tvTitle.setText(职称);
        holder.tvTitle.setTypeface(tfRobotoRegular);
        holder.tvDate =(TextView中)row.findViewById(R.id.text_date);
        holder.tvDate.setText(tasksRepository.get(位置).getDate());
        holder.tvDate.setTypeface(tfRobotoRegular);
        holder.tvTime =(TextView中)row.findViewById(R.id.text_time);
        holder.tvTime.setText(tasksRepository.get(位置).getTime());
        holder.tvTime.setTypeface(tfRobotoRegular);
        holder.tvDescription =(TextView中)row.findViewById(R.id.text_description);
        字符串描述= tasksRepository.get(位置).getDescription();
        如果(description.length()> 46)
        {
            描述= description.substring(0,45);
            描述=描述+...;
        }
        holder.tvDescription.setText(介绍);
        holder.tvDescription.setTypeface(tfRobotoRegular);
        holder.tvId =(TextView中)row.findViewById(R.id.text_id);
        holder.tvId.setText(将String.valueOf(tasksRepository.get(位置).getId()));
        holder.tvId.setTypeface(tfRobotoRegular);
        holder.tvLocation =(TextView中)row.findViewById(R.id.text_location);
        holder.tvLocation.setText(tasksRepository.get(位置).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(位置);
        holder.imTaskStatusButton.setOnClickListener(新OnClickListener()
        {
            公共无效的onClick(视图v)
            {
                 INT [] =位置INT新[2];
                 currentRowId =位置;
                 currentRow = V;
                 //获取x,y位置,并将其存储位置[]数组中
                 //位置[0] = X,位置[1] = Y。
                 v.getLocationOnScreen(位置);                 //初始化为x点,和y位置
                 点=新点();
                 point.x =位置[0];
                 point.y =位置[1];
                 showStatusPopup(TasksListActivity.this,点);
            }
        });
        字符串状态= tasksRepository.get(位置).getStatus();
        Log.d(TAG的当前行的状态:+状态);
        setStatusColorImages(状态,holder.imClock,holder.imCalendar,holder.imLocation,holder.llRowLayout);        返回行;
    }
}

和ViewHolder:

 静态类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;
}

在这种情况下

I have the following XML file containing only a 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>

I would like to add Items and Subitems to this ListView.

My app makes a certain kind of search upon the user's request and gets the datas, i want those datas retrieved to be visible in ListView. Is there any simple method like :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    lv1 = (ListView) findViewById(R.id.lview);
    lv1.add(item,subitem,0) // where 0 is the position.. any method like this?
}

解决方案

If you want to populate an array list with many data items in each row you have to implement a custom adapter and a getView method like so:

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

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

in this case.

这篇关于添加项目和子项到ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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