我正在创建ListView,每行包含两个元素(一个用于打开第二行以删除行)。我在主要活动中创建了我自定义的ListView,如下所示:
ArrayList<String> names = Devices.getInstance().getNameList();
final StableArrayAdapter adapter = new StableArrayAdapter(this,listViewResourceId,names);
final ListView listview = (ListView) findViewById(R.id.listView1);
listview.setAdapter(adapter);
我自己的ArrayAdapter:
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
private final Activity mContext;
public StableArrayAdapter(Activity context, int textViewResourceId, List<String> objects) {
super(context, textViewResourceId, objects);
mContext = context;
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = mContext.getLayoutInflater();
view = inflator.inflate(R.layout.row, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.text.setText(getItem(position));
viewHolder.button = (ImageButton) view.findViewById(R.id.del);
viewHolder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO dodanie zapytania o usuniecie
StableArrayAdapter.this.mIdMap.remove(getItem(position));
StableArrayAdapter.this.notifyDataSetChanged();
//((BaseAdapter) ((ListView)findViewById(R.id.listView1)).getAdapter()).notifyDataSetChanged();
}
});
view.setTag(viewHolder);
}
return view;
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
问题是,当我尝试使用删除按钮删除行时(其上面的单击侦听器在上面),在notifyDataSetChanged()行上有 NullPointerException。
我尝试制作非匿名类(对于OnClickListener),或者将本地字段从OnCreate切换到类字段但没有任何效果 - 我想我做了一些愚蠢的事情但是现在我是盲目的,看不到任何东西在这段代码中。
先谢谢你的帮助!!!
堆栈跟踪:
07-02 10:24:26.604: E/AndroidRuntime(2109): FATAL EXCEPTION: main
07-02 10:24:26.604: E/AndroidRuntime(2109): java.lang.NullPointerException
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.widget.AbsListView.obtainView(AbsListView.java:2253)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.widget.ListView.makeAndAddView(ListView.java:1769)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.widget.ListView.fillSpecific(ListView.java:1318)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.widget.ListView.layoutChildren(ListView.java:1600)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.widget.AbsListView.onLayout(AbsListView.java:2102)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.view.View.layout(View.java:13754)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.view.ViewGroup.layout(ViewGroup.java:4362)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.view.View.layout(View.java:13754)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.view.ViewGroup.layout(ViewGroup.java:4362)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.view.View.layout(View.java:13754)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.view.ViewGroup.layout(ViewGroup.java:4362)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.view.View.layout(View.java:13754)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.view.ViewGroup.layout(ViewGroup.java:4362)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.view.View.layout(View.java:13754)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.view.ViewGroup.layout(ViewGroup.java:4362)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.view.View.layout(View.java:13754)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.view.ViewGroup.layout(ViewGroup.java:4362)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1866)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1687)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.view.Choreographer.doFrame(Choreographer.java:525)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.os.Handler.handleCallback(Handler.java:615)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.os.Handler.dispatchMessage(Handler.java:92)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.os.Looper.loop(Looper.java:137)
07-02 10:24:26.604: E/AndroidRuntime(2109): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-02 10:24:26.604: E/AndroidRuntime(2109): at java.lang.reflect.Method.invokeNative(Native Method)
07-02 10:24:26.604: E/AndroidRuntime(2109): at java.lang.reflect.Method.invoke(Method.java:511)
07-02 10:24:26.604: E/AndroidRuntime(2109): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-02 10:24:26.604: E/AndroidRuntime(2109): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-02 10:24:26.604: E/AndroidRuntime(2109): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
你正在if (convertView == null) {}
区内做所有事情。仅用于向持有者充气和存储视图。设置文本并单击此块外部的侦听器,以便它们应用于每个视图,重新循环或不重新循环。
示例:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//--init if not re-cycled--
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, parent, false);
convertView.setTag(new ViewHolder(
(TextView) convertView.findViewById(R.id.label),
(ImageButton) convertView.findViewById(R.id.del)
));
}
//--data to set--
String item = getItem(position);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
StableArrayAdapter.this.mIdMap.remove(getItem(position));
StableArrayAdapter.this.notifyDataSetChanged();
}
};
//--set data--
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.text.setText(item);
holder.button.setOnClickListener(listener);
return convertView;
}
private static class ViewHolder{
public final TextView text;
public final Button button;
private ViewHolder(TextView text, Button button) {
this.text = text;
this.button = button;
}
}
答案 1 :(得分:1)
试试这个,
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// View view = null; remove this line
if (convertView == null) {
LayoutInflater inflator = mContext.getLayoutInflater();
view = inflator.inflate(R.layout.row, null);
}
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.text.setText(getItem(position));
viewHolder.button = (ImageButton) convertView.findViewById(R.id.del);
viewHolder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO dodanie zapytania o usuniecie
StableArrayAdapter.this.mIdMap.remove(getItem(position));
StableArrayAdapter.this.notifyDataSetChanged();
//((BaseAdapter) ((ListView)findViewById(R.id.listView1)).getAdapter()).notifyDataSetChanged();
}
});
view.setTag(viewHolder);
}
return view;
}
答案 2 :(得分:0)
问题当然是愚蠢的,我试图通过从容器中删除它来删除行,而没有正确通知这个整个类StableArrayAdapter,而不是这个(在OnClickListener中):
StableArrayAdapter.this.mIdMap.remove(getItem(position));
我应该用这个:
StableArrayAdapter.this.remove(getItem(position));