您好,我从列表中删除行视图时遇到问题。我只是创建一个场所订单活动,然后我在那里注册列表,然后创建一个购物车适配器类。
当我删除它生成的行时,异常无法删除该行。有没有解决方案?
PlaceOrder.java
public class PlaceOrder extends Activity {
String [] pIds;
String [] pNames;
String [] pPrizes;
static ListView lv;
ImageView bck;
String [] listImages;
String food_id;
String userdata[];
Intent i;
Intent intent;
TextView totalprze;
float tprize;
ProgressDialog pDialog;
TableRow p_order;
AlertDialog.Builder builder;
List<String> cart=new ArrayList<String>();
String [] pId;
String [] PName;
String [] pPrize;
Intent intent_one;
String userid;
String status,message;
String [] delete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place_order);
lv=(ListView)findViewById(R.id.cart_list);
bck=(ImageView) findViewById(R.id.placeholder_bg_btn);
totalprze =(TextView) findViewById(R.id.place_order_price);
p_order=(TableRow)findViewById(R.id.p_order);
intent=new Intent(this,Food.class);
builder = new AlertDialog.Builder(this);
p_order.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Toast.makeText(getApplicationContext(), "", Toast.LENGTH_LONG).show();
builder.setMessage(Html.fromHtml("<b>Place Order</b>"+"<br>Do you want to continue ?"))
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Cursor cursor = new ShopingCartHelper(PlaceOrder.this).getData(userid);
// Toast.makeText(getApplicationContext(), "View Cart",Toast.LENGTH_LONG).show();
if (cursor .moveToFirst()) {
while (cursor.isAfterLast() == false) {
String pid = cursor.getString(cursor
.getColumnIndex("id"));
String pName = cursor.getString(cursor
.getColumnIndex("product_name"));
String prize = cursor.getString(cursor
.getColumnIndex("price"));
cart.add(pid);
cart.add(pName);
// System.out.print("Product Name "+pName+"Price"+prize+"id"+id+"\n");
cart.add(prize);
cursor.moveToNext();
}
int length=cart.size();
pId=new String[cart.size()/3];
PName=new String[cart.size()/3];
pPrize=new String[cart.size()/3];
//System.out.print(length+"\n\n\n\n Values of List \n\n\n\n");
int k=0;
String[] values=cart.toArray(new String[cart.size()]);
// System.out.print("Values are :\n\n"+values);
int count=0;
for(int j=0;j<values.length/3;j++){
pId[j]=values[count];
count=count+1;
PName[j]=values[count];
count=count+1;
pPrize[j]=values[count];
count=count+1;
}
for(int i=0;i<pPrize.length;i++){
System.out.println(pPrize[i]);
}
}
// Toast.makeText(getApplicationContext(), "Yes",Toast.LENGTH_SHORT).show();
new place_order().execute();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
}
);
AlertDialog d = builder.create();
// d.setTitle("Are you sure");
d.show();
TextView messageText = (TextView)d.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
}
});
i=new Intent(this,Menu.class);
bck.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Bundle bundle=new Bundle();
//bundle.putStringArray("images", ListImages);
bundle.putString("food_id", food_id);
bundle.putStringArray("images", listImages);
bundle.putStringArray("userData",userdata);
i.putExtras(bundle);
startActivity(i);
}
});
if(this.getIntent().getExtras()!=null)
{
Bundle b=this.getIntent().getExtras();
pIds=b.getStringArray("pId");
pNames=b.getStringArray("PName");
pPrizes=b.getStringArray("pPrize");
userdata=b.getStringArray("userData");
tprize=b.getFloat("totalprize");
food_id=b.getString("food_id");
listImages=b.getStringArray("images");
String prz=Float.toString(tprize);
totalprze.setText("$"+prz);
delete=new String[pIds.length];
for(int m=0;m<pIds.length;m++){
delete[m]="Delete";
}
lv.setAdapter(new cartAdapter(PlaceOrder.this, pIds, pNames, pPrizes,userdata,delete));
userid=userdata[0];
pIds=null;
pNames=null;
pPrizes=null;
}
}
public void onBackPressed() {
pIds=null;
pNames=null;
pPrizes=null;
}
}
购物车Adapter.java
public class cartAdapter extends BaseAdapter{
String [] pIdz;
String [] pNamz;
String [] pPrizs;
String [] userData;
String [] del;
// List<String> imges;
Context context;
DrawerLayout dLayout;
// private ShopingCartHelper obj;
//ListView dList;
private static LayoutInflater inflater=null;
JSONArray jCat = null;
int count=0;
ProgressDialog pDialog;
public cartAdapter(PlaceOrder ctx,
String[] pIds,String[] pNams, String[] pprise,String [] userdata, String[] delete) {
pIdz=pIds;
pNamz=pNams;
context=ctx;
pPrizs=pprise;
userData=userdata;
del=delete;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// TODO Auto-generated constructor stub
}
@Override
public int getCount() {
// TODO Auto-generated method stub
if(pIdz==null){
Toast.makeText(context, "There is issue with net connection.", Toast.LENGTH_LONG).show();
//Intent i=new Intent(context,WelcomeActivity.class);
//context.startActivity(i);
return count ;
}else{
return pIdz.length;
}
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class holder{
DrawerLayout dLayout;
TextView pid;
TextView pname;
TextView pprise;
Button delete;
ListView lv;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final holder hldr=new holder();
View rowView = null;
rowView = inflater.inflate(R.layout.place_order_item_list, null);
//hldr.dLayout= (DrawerLayout)rowView.findViewById(R.id.btn_drawer_layout);
// hldr.dLayout=(DrawerLayout)rowView.findViewById(R.id.btndrawerLayout);
hldr.pid=(TextView) rowView.findViewById(R.id.item_id);
hldr.pname=(TextView) rowView.findViewById(R.id.item_name);
hldr.pprise=(TextView) rowView.findViewById(R.id.item_price);
hldr.delete=(Button) rowView.findViewById(R.id.delete);
hldr.pid.setText(pIdz[position]);
hldr.pname.setText(pNamz[position]);
hldr.pprise.setText(pPrizs[position]);
hldr.delete.setText(del[position]);
hldr.delete.setTag(position);
hldr.delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int pid=Integer.parseInt(hldr.pid.getText().toString());
new ShopingCartHelper(context).delProduct(pid);
pIdz[position]=null;
pNamz[position]=null;
pPrizs[position]=null;
del[position]=null;
// new PlaceOrder().updateAdapter(context,pIdz,pNamz,pPrizs,del);
//
notifyDataSetChanged();
}
});
return rowView;
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
由于您要从列表中删除该行,因此您必须明确删除该行,您应该像这样声明Pidz
ArrayList<String> Pidz;
并在您的cartAdapter构造函数中
Pidz = Arrays.asList(pIds);
请修改你的onclicklistener,
public void onClick(View v) {
int pid=Integer.parseInt(hldr.pid.getText().toString());
new ShopingCartHelper(context).delProduct(pid);
pIdz[position]=null;
pNamz[position]=null;
pPrizs[position]=null;
del[position]=null;
pIdz.remove(position);
notifyDataSetChanged();
}
});
答案 1 :(得分:0)
您的问题出在CartAdapter.java中,您有:
pIdz[position]=null;
pNamz[position]=null;
pPrizs[position]=null;
del[position]=null;
您不会删除列表中的项目,只能将其设置为“空”&#39;值。所以当调用&#39; notifyDataSetChanged&#39;你发了一个NullPointerException。
要修复此错误,请将所有String []更改为ArrayList,然后轻松调用Java.util.ArrayList.remove(index)