2015-10-27 17 views
-1

嗨我無法從列表中刪除一行查看我不知道如何刪除它我從互聯網上搜索了很多,我嘗試了很多例子,但我無法解決問題 。當我點擊hldr.delete按鈕列表查看刪除列表解決方案請完整的行。使用基本適配器從自定義列表中刪除行視圖解決方案

PlaceOrder活動

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; 
    TextView totalprze; 
    float tprize; 
    @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); 

     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); 
     lv.setAdapter(new cartAdapter(PlaceOrder.this, pIds, pNames, pPrizes,userdata)); 

     pIds=null; 
     pNames=null; 
     pPrizes=null; 
     } 
    } 
     public void onBackPressed() { 
     pIds=null; 
     pNames=null; 
     pPrizes=null; 
     } 

} 

CartAdapter

public class cartAdapter extends BaseAdapter{ 

    String [] pIdz; 
    String [] pNamz; 
    String [] pPrizs; 
    String [] userData; 
    // List<String> imges; 
    Context context; 
    // private ShopingCartHelper obj; 

    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) { 

     pIdz=pIds; 
     pNamz=pNams; 

     context=ctx; 
     pPrizs=pprise; 
     userData=userdata; 

     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{ 
     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; 
     Bitmap bitmap = null; 



      rowView = inflater.inflate(R.layout.place_order_item_list, null); 
      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]); 

      /* convertView.setTag(hldr); 
      hldr.delete.setTag(hldr);*/ 
      // 
      // Picasso.with(context).load(imgs[position]).into(hldr.img); 
      hldr.delete.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       // rowView.remove(position); //removing from your List 
       //Toast.makeText(context, "Delete",Toast.LENGTH_LONG).show(); 
       int pid=Integer.parseInt(hldr.pid.getText().toString()); 
       //Toast.makeText(context, "pid"+pid, Toast.LENGTH_LONG).show(); 
       new ShopingCartHelper(context).delProduct(pid); 
       //PlaceOrder.lv.removeViewAt(position); 

       notifyDataSetChanged(); 

      } 
     }); 
      rowView.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
//    // TODO Auto-generated method stub 
//    cartAdapter.this.pIdz.remove[position]; 

      // Toast.makeText(context, "hi",Toast.LENGTH_LONG).show(); 
      } 
     }); 
     return rowView; 

     // TODO Auto-generated method stub 
    } 
} 

回答

0

你不能從一個數組中刪除任何東西 - 他們總是固定長度。一旦你創建了一個長度爲3的數組,那麼這個數組的長度總是3。如果你想從列表視圖中刪除,你使用的數組應該是動態的(添加&刪除)。所以使pIdz; pNamz;pPrizs;userData到一些模態對象,並準備莫代爾對象的列表,並把它傳遞給適配器,使生活更輕鬆

你會用列表,例如更好ArrayList:

hldr.delete.setTag(position); 
hldr.delete.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      int position = (int)v.getTag();     
      list.remove(position); 
      notifyDataSetChanged(); 

     } 
    }); 
0

首先,

您應該創建一個對象來存儲所有不同的字符串,而不是爲每個值使用多個數組。

EJ:

class MyClass 
{ 
    String id; 
    String name 
    String price; 

    public MyClass(String id, String name, String price) 
    { 
     this.id = id; 
     this.name = name; 
     this.price = price; 
    } 
} 

這樣,你不會必須刪除每個陣列的位置。

最後要回答你的問題,只需刪除你想要刪除的每個數組的位置的值。

因爲它們是Array而不是ArrayList,所以需要用正確的新維度重新創建它(注意ArrayList會更容易)。

然後在適配器類上調用notifyDataSetChanged()。

希望這會有所幫助。

相關問題