I am creating ListView with two elements in each row (one for open second for deletion of row). I have my customized ListView in main activity created like this:
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);
My own 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;
}
}
Problem is that when I'm trying to delete row by using deletion button (its On click listener is above) there is NullPointerException on notifyDataSetChanged() line.
I tried making non anonymous class (for OnClickListener), or switching local fields from OnCreate to class fields but nothing worked - I suppose that I'm making some stupid thing but for now I'm blind and can't see anything in this code.
Thanks in advance for help!!!
StackTrace:
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)