如何将ListView数组添加到ArrayAdapter以获取Listview

时间:2014-01-02 10:26:17

标签: android listview

我有一个自定义的行布局,其中包含List视图的Textview和TextView。 我可以向ArrayAdater添加表示每行的字符串数组,但是在填充ImageViews行时遇到问题。

我尝试创建一个ImageView数组并添加到ArrayAdaptor中,如下所示(因为我看不到一个将两个数组作为参数的ArrayAdpter构造函数):

// a string array holding the list items
    String classes[] = { "My Profile", "Splash", "Log A Dive", "Search Dives",
            "Weather: Magic Seaweed", "Irish Tides", "International Tides", "Dive PLanner" };// each class is a menu Actiitem

    ImageView images[]={(ImageView) findViewById(R.drawable.logdive),(ImageView) findViewById(R.drawable.logdive2),
            (ImageView) findViewById(R.drawable.logdive3),
            (ImageView) findViewById(R.drawable.search),(ImageView) findViewById(R.drawable.weather),
            (ImageView) findViewById(R.drawable.tides),(ImageView) findViewById(R.drawable.tides)
            ,(ImageView) findViewById(R.drawable.logdive3)};

ArrayAdapter listadaptor = new ArrayAdapter<String>(MainMenu.this,
                R.layout.single_list_row, R.id.title, classes);
        listadaptor.add(images);
setListAdapter(cutsomAdaptor);

Logcat Stack track:

  

01-02 05:10:45.480:E / AndroidRuntime(891):java.lang.RuntimeException:   无法实例化活动   ComponentInfo {com.mooney.diveapp / com.mooney.diveapp.MainMenu}:   显示java.lang.NullPointerException

2 个答案:

答案 0 :(得分:1)

我正在分享我的代码,这将有助于解决您的问题..因为我创建了三个类 第一个是命名类。

公共类命名{

int image1;
String name;

public void setImage1(int image1) {
    this.image1 = image1;
}

public int getImage1() {
    return image1;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

第二类是适配器类

public class Adapter扩展BaseAdapter {

ArrayList<Object> item;
public Activity context;
public LayoutInflater inflater;
Context c;

public Adapter(ArrayList<Object> item, Activity context) {

    this.item = item;
    this.context = context;
    this.inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return item.size();
}

@Override
public Object getItem(int position) {
    return item.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

class ViewHolder {
    ImageView image1;
    TextView name;
}

public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    holder = new ViewHolder();
    convertView = inflater.inflate(R.layout.activity_main, null);
    holder.image1 = (ImageView) convertView.findViewById(R.id.image1);
    holder.name = (TextView) convertView.findViewById(R.id.name);

    convertView.setTag(holder);

    holder = (ViewHolder) convertView.getTag();
    final Nameing n = (Nameing) item.get(position);
    holder.image1.setImageResource(n.getImage1());
    holder.name.setText(n.getName());

    holder.name.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(context, n.getName(), Toast.LENGTH_SHORT).show();

        }
    });

    return convertView;

}

}

和第三类是MainActivity。

公共类MainActivity扩展了Activity {

private ListView list;

private ArrayList<Object> item = new ArrayList<Object>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    list = (ListView) findViewById(R.id.list1);

    Arraylist();

    Adapter ad = new Adapter(item, MainActivity.this);
    list.setAdapter(ad);

}

private void Arraylist() {
    AddOjectToList1(R.drawable.image1, "name");
    AddOjectToList1(R.drawable.image2, "name");
    AddOjectToList1(R.drawable.image3, "name");
    AddOjectToList1(R.drawable.image4, "name");
    AddOjectToList1(R.drawable.image5, "name");
    AddOjectToList1(R.drawable.image6, "name");
    AddOjectToList1(R.drawable.image7, "name");

}

void AddOjectToList1(int image1, String name) {

    Nameing n = new Nameing();
    n.setImage1(image1);
    n.setName(name);
    item.add(n);
}

}

答案 1 :(得分:1)

适配器的要点是将一组数据链接到一组视图(由AdapterView创建,即ListView) - 您不会为其提供要使用的视图数组。

在这种情况下,您需要覆盖填充布局的ArrayAdapter的getView方法。您需要一个可以表示String和int(对于可绘制资源ID)的数据集,类似于Pair(不理想,但为了简单起见):

ArrayList<Pair<String, Integer>> classes = new ArrayList<Pair<String, Integer>>();
classes.add(Pair.create("My Profile", R.drawable.myProfile));
// add all items
ArrayAdapter listadaptor = new ArrayAdapter<Pair<String, Integer>>(MainMenu.this,
            R.layout.single_list_row, R.id.title, classes) {
    public View getView(int position, View convertView, ViewGroup container) {
        if(convertView == null) {
            convertView = LayoutInflator.from(getContext()).inflate(R.layout.single_list_row, container, false);
        }
        TextView title = (TextView) convertView.findViewById(R.id.title);
        Pair<String, Integer> item = getItem(position);
        title.setText(item.first);
        title.setCompoundDrawables(item.second, 0, 0, 0);
        return convertView;
    }
 };