0
    package com.test.ExampleListview;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;


public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ListView listview = (ListView) findViewById(R.id.listview);
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
                "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
                "Android", "iPhone", "WindowsMobile" };

        final ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < values.length; ++i) {
            list.add(values[i]);
        }

        System.out.println("created arraylist");

        final ButtonArrayAdapter adapter = new ButtonArrayAdapter(this,
                values);
        listview.setAdapter(adapter);

        System.out.println("created adapter and set it");

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
                final String item = (String) parent.getItemAtPosition(position);
                list.remove(item);
                adapter.notifyDataSetChanged();
            }
        });
    }
}

package com.test.ExampleListview;

/**
 * Created by User on 17/06/13.
 */
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 ButtonArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public ButtonArrayAdapter(Context context, String[] values) {
        super(context, R.layout.rowlayout, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.buttontext);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.buttonimage);
        textView.setText(values[position]);
        imageView.setImageResource(R.drawable.cool);
        return rowView;
    }
}


06-07 12:58:43.874 15319-15319/com.test.ExampleListview D/MyActivity: index=2 
06-07 12:58:43.924 15319-15319/com.test.ExampleListview D/AndroidRuntime: Shutting down VM   
06-07 12:58:43.924 15319-15319/com.test.ExampleListview W/dalvikvm: threadid=1: thread    
exiting with uncaught exception (group=0x40a71930) 
06-07 12:58:43.944 15319-15319/com.test.ExampleListview E/AndroidRuntime: FATAL EXCEPTION:  
main java.lang.NullPointerException at 
com.test.ExampleListview.ButtonArrayAdapter.getView(ButtonArrayAdapter.java:31)`

我已经在线学习了一个教程,它确实对 ArrayAdapters 的基础知识有所帮助。

http://www.vogella.com/articles/AndroidListView/article.html

但是,当我尝试使用自定义 ArrayAdapter 时,应用程序没有给出编译错误,但无法启动。我想知道我是否在做任何明显错误的事情,我花了几个小时测试东西,我担心我的基本 Android 知识意味​​着我错过了一些明显的东西。我什至在 logcat 中找不到我的 println!我不认为它会做任何事情,但会立即崩溃......

谢谢。

4

3 回答 3

0

看看这些行:

    View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.buttontext);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.buttonimage);
    textView.setText(values[position]);

你在 textView 得到一个 NPE。所以可能 textView 指的是一个无效的对象。这意味着rowView.findViewById(R.id.buttontext)失败。检查 xml 文件中的每个名称或字段,错误可能会清楚地显示出来。如果您仍有问题,请在此处发布您的文件内容。

于 2013-06-17T18:49:02.183 回答
0

在此代码块中:

 public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.buttontext);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.buttonimage);
        textView.setText(values[position]);
        imageView.setImageResource(R.drawable.cool);
        return rowView;
}

尝试以下操作:

  • 确保您引用的所有 id 都在您膨胀的 XML 中设置。
  • 确保这values != null是真的。
  • 用于LayoutInflater inflater = LayoutInflater.from(context);可读性。
  • 使用View rowView = inflater.inflate(R.layout.rowlayout, null, false)(空父)。该列表将自行添加视图。
于 2013-06-17T20:20:10.030 回答
0

这与我的行布局有什么关系吗?

Android Studio 说我的“命名空间未绑定”?

<?xml

 version="1.0" encoding="utf-8"?>

<RelativeLayout 
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/rowlayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@+id/buttontext"
                android:drawableLeft="@+id/buttonimage"
                android:gravity="left|center_vertical">
</RelativeLayout>
于 2013-06-18T12:29:32.833 回答