有人可以帮我解决我的问题。
我仍在尝试使用DraggingListView
和ArrayAdapter
。
现在我想通过点击来实现listview
中的删除元素,但是当我做的时候:
StableArrayAdapter.this.notifyDataSetChanged();
我得到nullPointer Exception
...
这是我的适配器:
public class StableArrayAdapter extends ArrayAdapter<Product> {
final int INVALID_ID = -1;
LayoutInflater lInflater;
Context ctx;
public static final String PREFS_NAME = "com.shvedchenko.skleroshop";
public static final String PREFS_THEME = "theme";
HashMap<Product, Integer> mIdMap = new HashMap<Product, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId, List<Product> prod) {
super(context, textViewResourceId, /*objects*/prod);
lInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ctx = context;
for (int i = 0; i < prod.size(); ++i) {
mIdMap.put(prod.get(i), i);
}
}
// пункт списка
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
// используем созданные, но не используемые view
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
Product p = getItem(position);
SharedPreferences pref = ctx.getSharedPreferences(PREFS_NAME, ctx.MODE_PRIVATE);
int theme = pref.getInt(PREFS_THEME, 0); // getting Integer
if(theme == 0)
((TextView) view.findViewById(R.id.tvDescr)).setTextColor(Color.WHITE);
else
((TextView) view.findViewById(R.id.tvDescr)).setTextColor(Color.BLACK);
((TextView) view.findViewById(R.id.tvDescr)).setText(p.getProductName());
((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.getProductImage());
ImageView iv = (ImageView)view.findViewById(R.id.ivImage);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mIdMap.remove(getItem(position));
System.out.println("FROM CLICK -- " + mIdMap.size() );
/*for( Product p : mIdMap.keySet() ) {
System.out.println( p.getProductName() );
}*/
//StableArrayAdapter.this.notifyDataSetChanged();
}
});
return view;
}
/*@Override
public Object getItem(int position) {
return mIdMap.get(position);
}*/
@Override
public long getItemId(int position) {
if (position < 0 || position >= mIdMap.size()) {
return INVALID_ID;
}
Product item = (Product) getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
这是错误:
08-26 08:50:58.902 2167-2167 / com.shvedchenko.skleroshop E / AndroidRuntime:致命异常:主要 显示java.lang.NullPointerException 在com.shvedchenko.skleroshop.StableArrayAdapter.getItemId(StableArrayAdapter.java:104) 在android.widget.AdapterView.rememberSyncState(AdapterView.java:1195) 在android.widget.AdapterView $ AdapterDataSetObserver.onChanged(AdapterView.java:810) 在android.widget.AbsListView $ AdapterDataSetObserver.onChanged(AbsListView.java:5958) 在android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37) 在android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50) 在android.widget.ArrayAdapter.notifyDataSetChanged(ArrayAdapter.java:286) 在com.shvedchenko.skleroshop.StableArrayAdapter $ 1.onClick(StableArrayAdapter.java:85) 在android.view.View.performClick(View.java:4204) 在android.view.View $ PerformClick.run(View.java:17355) 在android.os.Handler.handleCallback(Handler.java:725) 在android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(Looper.java:137) 在android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:511) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 在dalvik.system.NativeStart.main(本地方法)
/ 已更新 /
我的适配器现在:
public class StableArrayAdapter extends ArrayAdapter<Product> {
final int INVALID_ID = -1;
LayoutInflater lInflater;
Context ctx;
public static final String PREFS_NAME = "com.shvedchenko.skleroshop";
public static final String PREFS_THEME = "theme";
HashMap<Product, Integer> mIdMap = new HashMap<Product, Integer>();
ArrayList<Product> prods = new ArrayList<Product>();
public StableArrayAdapter(Context context, int textViewResourceId, List<Product> prod) {
super(context, textViewResourceId, /*objects*/prod);
lInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ctx = context;
for (int i = 0; i < prod.size(); ++i) {
mIdMap.put(prod.get(i), i);
prods.add(i,prod.get(i));
}
}
// пункт списка
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
// используем созданные, но не используемые view
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
Product p = getItem(position);
SharedPreferences pref = ctx.getSharedPreferences(PREFS_NAME, ctx.MODE_PRIVATE);
int theme = pref.getInt(PREFS_THEME, 0); // getting Integer
if(theme == 0)
((TextView) view.findViewById(R.id.tvDescr)).setTextColor(Color.WHITE);
else
((TextView) view.findViewById(R.id.tvDescr)).setTextColor(Color.BLACK);
((TextView) view.findViewById(R.id.tvDescr)).setText(p.getProductName());
((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.getProductImage());
ImageView iv = (ImageView)view.findViewById(R.id.ivImage);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
prods.remove(position);
StableArrayAdapter.this.notifyData(prods);
}
});
return view;
}
public void notifyData(List<Product> prod) {
//First of all Clear Map
mIdMap.clear();
for (int i = 0; i < prod.size(); ++i) {
mIdMap.put(prod.get(i), i);
}
notifyDataSetChanged();
}
@Override
public long getItemId(int position) {
if (position < 0 || position >= mIdMap.size()) {
return INVALID_ID;
}
Product item = (Product) getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
/ ERROR /
08-26 09:53:19.270 2530-2530 / com.shvedchenko.skleroshop E / AndroidRuntime:致命例外:主要 显示java.lang.NullPointerException 在com.shvedchenko.skleroshop.StableArrayAdapter.getItemId(StableArrayAdapter.java:122) 在android.widget.AdapterView.rememberSyncState(AdapterView.java:1195) 在android.widget.AdapterView $ AdapterDataSetObserver.onChanged(AdapterView.java:810) 在android.widget.AbsListView $ AdapterDataSetObserver.onChanged(AbsListView.java:5958) 在android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37) 在android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50) 在android.widget.ArrayAdapter.notifyDataSetChanged(ArrayAdapter.java:286) 在com.shvedchenko.skleroshop.StableArrayAdapter.notifyData(StableArrayAdapter.java:112) 在com.shvedchenko.skleroshop.StableArrayAdapter $ 1.onClick(StableArrayAdapter.java:91)
我做错了什么?
TNX提前!
答案 0 :(得分:4)
正确的适配器代码:
public class StableArrayAdapter extends ArrayAdapter<Product> {
final int INVALID_ID = -1;
LayoutInflater lInflater;
Context ctx;
public static final String PREFS_NAME = "com.shvedchenko.skleroshop";
public static final String PREFS_THEME = "theme";
HashMap<Product, Integer> mIdMap = new HashMap<Product, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId, List<Product> prod) {
super(context, textViewResourceId, /*objects*/prod);
lInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ctx = context;
for (int i = 0; i < prod.size(); i++) {
mIdMap.put(prod.get(i), i);
}
}
// пункт списка
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
// используем созданные, но не используемые view
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
Product p = getItem(position);
SharedPreferences pref = ctx.getSharedPreferences(PREFS_NAME, ctx.MODE_PRIVATE);
int theme = pref.getInt(PREFS_THEME, 0); // getting Integer
if(theme == 0)
((TextView) view.findViewById(R.id.tvDescr)).setTextColor(Color.WHITE);
else
((TextView) view.findViewById(R.id.tvDescr)).setTextColor(Color.BLACK);
((TextView) view.findViewById(R.id.tvDescr)).setText(p.getProductName());
((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.getProductImage());
ImageView iv = (ImageView)view.findViewById(R.id.ivImage);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StableArrayAdapter.this.remove(getItem(position));
StableArrayAdapter.this.notifyDataSetChanged();
}
});
return view;
}
@Override
public long getItemId(int position) {
if (position < 0 || position >= mIdMap.size()) {
return INVALID_ID;
}
Product item = (Product) getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
答案 1 :(得分:2)
从列表中删除项目
list.remove(position);
然后使用适配器对象调用以下方法。
stableAdapter.notifyData(list);
在Adapter类中编写一个方法。
public void notifyData(List<Product> prod)
{
//First of all Clear Map
mIdMap.clear();
for (int i = 0; i < prod.size(); ++i) {
mIdMap.put(prod.get(i), i);
}
notifyDataSetChanged();
}
使用adpater对象
调用此方法