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!我不认为它会做任何事情,但会立即崩溃......
谢谢。