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

时间:2014-05-29 17:56:05

标签: java android android-layout listview android-listview

所以,现在我有一个自定义适配器类,它接收一个Locations数组并将它们添加到ListView。这很好,花花公子,但我希望在初始化后将Locations添加到此listview。例如,有人可以“添加位置”,它会将其添加到此ListView。这是我的主要活动:

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类:

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

关于如何实现这一点的任何想法?感谢。

4 个答案:

答案 0 :(得分:8)

  1. 在适配器中将Locations data[]从数组更改为 ArrayList<Location>并覆盖相应的构造函数
  2. 在您的活动中,将变量data设为字段(类型ArrayList<Location>
  3. 添加地点后,您可以使用data.add(location)
  4. 然后您可以在适配器上调用notifyDatasetChanged()
  5. Example code

答案 1 :(得分:3)

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

ArrayList<Location> data = new ArrayList<Location>();

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

然后,当您想要将项目添加到列表中时,只需致电location_data.add(new_location)

编辑:您可以从几个大致相同的答案中选择。

答案 2 :(得分:2)

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

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

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

ArrayList<Location> data = null; // Alternatively use new ArrayList<Location>();

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

答案 3 :(得分:1)

在您的主要活动中,我建议使用ArrayList&lt;位置&gt;而不是Location [],以便更容易添加新的Location元素。然后,而不是让LocationAdapter扩展ArrayAdapter&lt;位置&gt;,扩展ListAdapter&lt;位置&gt;所以你可以传递你的ArrayList&lt;位置&gt;它。

也就是说,你需要在MainActivity中的addLocation(Location l)方法中做的就是向Location []或ArrayList&lt;添加一个元素。位置&gt;传递给适配器的数据结构,然后调用:

adapter.notifyDataSetChanged();

请注意,您需要在MainActivity中将适配器设为成员变量,以允许在onCreate()之外进行访问。

相关问题