首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Android应用程序的自定义适配器动态添加到列表视图中的项

使用Android应用程序的自定义适配器动态添加到列表视图中的项
EN

Stack Overflow用户
提问于 2014-05-29 17:56:04
回答 4查看 38.8K关注 0票数 14

因此,现在我有一个自定义适配器类,它接收一个位置数组并将它们添加到ListView中。这一切都很好,很好,但是我想在这个初始化之后向这个列表视图添加位置。例如,有人可以“添加一个位置”并将其添加到这个ListView中。以下是我的主要活动:

代码语言:javascript
运行
复制
package com.example.listviewtest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

public class MainActivity extends Activity {

private ListView listView1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Location location_data[] = new Location[]
    {
        new Location(R.drawable.ic_launcher, "Location 1", "Fruit!", "2 miles", "8-4 mon-fri\nclosed sun"),
        new Location(R.drawable.ic_launcher, "Location 2", "Veggies!", "2 miles", "8-5"),
        new Location(R.drawable.ic_launcher, "Location 3", "Plants!", "2 miles", "8-5"),
        new Location(R.drawable.ic_launcher, "Location 4", "Flowers!", "2 miles", "8-5"),
        new Location(R.drawable.ic_launcher, "Location 5", "Baked Goods!", "2 miles", "8-5")
    };

   LocationAdapter adapter = new LocationAdapter(this, 
            R.layout.listview_item_row, location_data);

   //adapter.add(new Location(R.drawable.ic_launcher, "Location 6", "Veggies!", "2 miles", "8-5"));


    listView1 = (ListView)findViewById(R.id.listView1);

    View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
    listView1.addHeaderView(header);

    listView1.setAdapter(adapter);
}
}

这个很管用。现在,在用数组填充adapter.add(new Location(R.drawable.ic_launcher, "Location 6", "Veggies!", "2 miles", "8-5"));之后,我想做一些类似的事情。

这是我的LocationAdapter课程:

代码语言:javascript
运行
复制
package com.example.listviewtest;

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

public class LocationAdapter extends ArrayAdapter<Location>{

Context context; 
int layoutResourceId;    
Location data[] = null;

public LocationAdapter(Context context, int layoutResourceId, Location[] data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

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

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new LocationHolder();
        holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
        holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
        holder.details = (TextView)row.findViewById(R.id.details);
        holder.distance = (TextView)row.findViewById(R.id.distance);
        holder.hours = (TextView)row.findViewById(R.id.hours);

        row.setTag(holder);
    }
    else
    {
        holder = (LocationHolder)row.getTag();
    }

    Location location = data[position];
    holder.txtTitle.setText(location.title);
    holder.imgIcon.setImageResource(location.icon);
    holder.details.setText(location.details);
    holder.distance.setText(location.distance);
    holder.hours.setText(location.hours);

    return row;
}

static class LocationHolder
{
    ImageView imgIcon;
    TextView txtTitle;
    TextView details;
    TextView distance;
    TextView hours;
}
}

对我如何实现这一点有什么想法吗?谢谢。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-05-29 18:12:35

  1. 在适配器中,将Locations data[]从数组更改为ArrayList<Location>,并覆盖适当的构造函数。
  2. 在您的活动中,将变量data设置为字段(键入ArrayList<Location>)
  3. 添加位置时,可以使用data.add(location)
  4. 然后您可以在适配器上调用notifyDatasetChanged()

示例代码

票数 8
EN

Stack Overflow用户

发布于 2014-05-29 18:12:42

将数据存储在ArrayList<Location>中而不仅仅是Location[]中,然后在列表适配器中创建一个公共类:

代码语言:javascript
运行
复制
ArrayList<Location> data = new ArrayList<Location>();

@Override
public void add(Location location) {
    data.add(location);
    notifyDataSetChanged();
}

然后,当您想要向列表中添加一个项时,只需调用location_data.add(new_location)

编辑:,看上去你是从几个基本相同的答案中挑选出来的。

票数 3
EN

Stack Overflow用户

发布于 2014-05-29 18:16:56

看起来,您需要重写LocationAdapter类中的add方法,以便将对象添加到内部列表中。

代码语言:javascript
运行
复制
@Override
public void add(Location location)
{
    super.add(location);
    data.add(location);
}

此实现要求您将数据更改为ArrayList,而不仅仅是数组,否则您必须自己重新调整数组的大小。

代码语言:javascript
运行
复制
ArrayList<Location> data = null; // Alternatively use new ArrayList<Location>();

如果不这样做,内部数据将保持不变,调用add将不会更改列表。这是不好的,因为您使用数据变量来获取视图的值。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23939800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档