2016-08-02 11 views
0

Hallo allerseits Ich benutze benutzerdefinierte listview zum Abrufen von Daten vom Server und Show in Listview .. Ich bin in der Lage, Daten zu bekommen und in listview zeigen, aber ich weiß es nicht Implementieren des click-Ereignisses von button in listitem. Es gibt zwei Schaltflächen zum Erhöhen und Verringern der Menge. Mein clicklistener funktioniert, aber es funktioniert nicht richtig. Wenn ich ein Produkt inkrementiere oder dekrementiere, sollte es zum Warenkorb hinzugefügt werden. Bitte helfen Sie mir, dieses Problem zu beheben. Ich habe zu viele Beiträge auf der Suche, konnte sie aber nicht verstehen ...Warenkorb Artikel Anzahl erhöhen/verringern & zum Warenkorb hinzufügen

Hier meinen Adapter Class Code:

public class ProductsAdapter extends BaseAdapter { 
private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener(); 

private Context context; 
private ArrayList<HashMap<String, String>> postItems; 
public SharedPreferences settings; 
public final String PREFS_NAME = "Products"; 

DisplayImageOptions options; 
ImageLoaderConfiguration imgconfig; 

private static int _counter = 0; 
private String _stringVal; 

public ProductsAdapter(Context context, ArrayList<HashMap<String, String>> arraylist){ 
    this.context = context; 

    File cacheDir = StorageUtils.getCacheDirectory(context); 
    options = new DisplayImageOptions.Builder() 
    .showImageOnLoading(R.drawable.loading) 
    .showImageForEmptyUri(R.drawable.loading) 
    .showImageOnFail(R.drawable.loading) 
    .cacheInMemory(true) 
    .cacheOnDisk(true) 
    .considerExifParams(true) 
    .displayer(new SimpleBitmapDisplayer()) 
    .imageScaleType(ImageScaleType.EXACTLY) 
    .build(); 

    imgconfig = new ImageLoaderConfiguration.Builder(context) 
    .build(); 
    ImageLoader.getInstance().init(imgconfig); 

    postItems = arraylist; 
    settings = context.getSharedPreferences(PREFS_NAME, 0);  

} 

@Override 
public int getCount() { 
    return postItems.size(); 
} 
@Override 
public Object getItem(int position) {  
    return postItems.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 

     if (convertView == null) { 
      LayoutInflater mInflater = (LayoutInflater) 
      context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
      convertView = mInflater.inflate(R.layout.row_products, null); 
     } 
     final HashMap<String, String> map = postItems.get(position); 

     ImageView imgProduct = (ImageView)convertView.findViewById(R.id.proimage); 
     ImageLoader.getInstance().displayImage(ConstValue.PRO_IMAGE_BIG_PATH+map.get("image"), imgProduct, options, animateFirstListener); 

     TextView txtTitle = (TextView)convertView.findViewById(R.id.proTitle); 
     txtTitle.setText(map.get("title")); 

     TextView txtPrice = (TextView)convertView.findViewById(R.id.txtprice); 
     txtPrice.setText(map.get("price")); 

     TextView textDiscount = (TextView)convertView.findViewById(R.id.textDiscount); 
     textDiscount.setVisibility(View.GONE); 

     TextView txtDiscountFlag = (TextView)convertView.findViewById(R.id.textDiscountFlag); 
     txtDiscountFlag.setVisibility(View.GONE); 

     TextView textCurrency = (TextView)convertView.findViewById(R.id.textCurrency); 
     textCurrency.setText(map.get("currency")); 

     final TextView value = (TextView)convertView.findViewById(R.id.textView3); 

     Button txtminus = (Button) convertView.findViewById(R.id.minus); 

     txtminus.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      Log.d("src", "Decreasing value..."); 
      _counter--; 
      _stringVal = Integer.toString(_counter); 
      value.setText(_stringVal); 

      if (_counter < 0) 
      { 
       value.setText("0"); 
      } 

     } 
     }); 

     Button txtplus = (Button) convertView.findViewById(R.id.plus); 
     txtplus.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Log.d("src", "Increasing value..."); 
      _counter++; 
      _stringVal = Integer.toString(_counter); 
      value.setText(_stringVal); 
     } 
     }); 


     if(!map.get("discount").equalsIgnoreCase("") && !map.get("discount").equalsIgnoreCase("0")){ 
      Double discount = Double.parseDouble(map.get("discount")); 
      Double price = Double.parseDouble(map.get("price")); 
      Double discount_amount = discount * price/100; 

      Double effected_price = price - discount_amount ; 
      //txtPrice.setBackgroundResource(R.drawable.strike_trough); 
      txtPrice.setPaintFlags(txtPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 
      textDiscount.setVisibility(View.VISIBLE); 
      textDiscount.setText(effected_price.toString()); 

      txtDiscountFlag.setVisibility(View.VISIBLE); 
      txtDiscountFlag.setText(discount+"% off"); 
     } 

     TextView txtUnit = (TextView)convertView.findViewById(R.id.txtunit); 
     txtUnit.setText(map.get("gmqty")); 

     TextView txtgm = (TextView)convertView.findViewById(R.id.txtgm); 
     txtgm.setText(map.get("unit")); 

     int a = Integer.parseInt(map.get("total_qty_stock").toString()); 
     int b = Integer.parseInt(map.get("consume_qty_stock").toString()); 

     int result = a - b; 

     TextView txtstock = (TextView)convertView.findViewById(R.id.stock); 
     txtstock.setText(String.valueOf(result)+" "+map.get("type")+" In Stock"); 
     //txtstock.setText(map.get("stock")+" "+map.get("type")+" In Stock"); 


    return convertView; 
} 

} 

result

Hier meinen ProductActivity-Code

public class ProductsActivity erweitert ActionBarActivity {

public SharedPreferences settings; 
public ConnectionDetector cd; 
static ArrayList<HashMap<String, String>> products_array; 
ProductsAdapter adapter; 
ListView products_listview; 
DisplayImageOptions options; 
ImageLoaderConfiguration imgconfig; 
ProgressDialog dialog; 

TextView txtcount; 
Button btn; 

private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener(); 
HashMap<String, String> catMap; 
@SuppressWarnings("unchecked") 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    settings = getSharedPreferences(ConstValue.MAIN_PREF, 0); 
    cd = new ConnectionDetector(getApplicationContext()); 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_products); 

/* ActionBar mActionBar = getActionBar(); 
    mActionBar.setDisplayShowHomeEnabled(true); 
    mActionBar.setDisplayShowTitleEnabled(true); 
    LayoutInflater mInflater = LayoutInflater.from(this); 

    View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null); 
    TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text); 
    mTitleTextView.setText("My Own Title"); 

    mActionBar.setCustomView(mCustomView); 
    mActionBar.setDisplayShowCustomEnabled(true); */ 

    settings = getSharedPreferences(ConstValue.MAIN_PREF, 0); 
    cd=new ConnectionDetector(this); 

    File cacheDir = StorageUtils.getCacheDirectory(this); 
    options = new DisplayImageOptions.Builder() 
    .showImageOnLoading(R.drawable.loading) 
    .showImageForEmptyUri(R.drawable.loading) 
    .showImageOnFail(R.drawable.loading) 
    .cacheInMemory(true) 
    .cacheOnDisk(true) 
    .considerExifParams(true) 
    .displayer(new SimpleBitmapDisplayer()) 
    .imageScaleType(ImageScaleType.NONE) 
    .build(); 

    imgconfig = new ImageLoaderConfiguration.Builder(this) 
    .build(); 
    ImageLoader.getInstance().init(imgconfig); 

    ArrayList<HashMap<String,String>> categoryArray = new ArrayList<HashMap<String,String>>(); 
    try { 
     categoryArray = (ArrayList<HashMap<String,String>>) ObjectSerializer.deserialize(settings.getString("categoryname", ObjectSerializer.serialize(new ArrayList<HashMap<String,String>>()))); 
    }catch (IOException e) { 
      e.printStackTrace(); 
    } 

    catMap = new HashMap<String, String>(); 
    catMap = categoryArray.get(getIntent().getExtras().getInt("position")); 

    products_array = new ArrayList<HashMap<String,String>>(); 
    try { 
     products_array = (ArrayList<HashMap<String,String>>) ObjectSerializer.deserialize(settings.getString("products_"+catMap.get("id"), ObjectSerializer.serialize(new ArrayList<HashMap<String,String>>()))); 
    }catch (IOException e) { 
      e.printStackTrace(); 
    } 

    products_listview = (ListView)findViewById(R.id.listView1); 
    adapter = new ProductsAdapter(getApplicationContext(), products_array); 
    products_listview.setAdapter(adapter); 

    TextView txtTitle = (TextView)findViewById(R.id.catname); 
    txtTitle.setText(catMap.get("name")); 

    txtcount = (TextView)findViewById(R.id.textcount); 

    btn = (Button) findViewById(R.id.button); 

    products_listview.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, final int position, 
           long id) { 
      // TODO Auto-generated method stub 

      try { 

       settings.edit().putString("selected_product", ObjectSerializer.serialize(products_array.get(position))).commit(); 



      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      Intent intent = new Intent(ProductsActivity.this, ProductdetailActivity.class); 
      intent.putExtra("position", position); 
      startActivity(intent); 

     } 
    }); 


    new loadProductsTask().execute(true); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 




public class loadProductsTask extends AsyncTask<Boolean, Void, ArrayList<HashMap<String, String>>> { 

    JSONParser jParser; 
    JSONObject json; 
    String count; 
    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 
    } 

    @Override 
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) { 
     // TODO Auto-generated method stub 
     if (result!=null) { 
      //adapter.notifyDataSetChanged(); 
     } 
     try { 
      settings.edit().putString("products_"+catMap.get("id"), ObjectSerializer.serialize(products_array)).commit(); 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     txtcount.setText(count); 
     adapter.notifyDataSetChanged(); 
     super.onPostExecute(result); 
    } 

    @Override 
    protected void onProgressUpdate(Void... values) { 
     // TODO Auto-generated method stub 
     super.onProgressUpdate(values); 
    } 

    @Override 
    protected void onCancelled(ArrayList<HashMap<String, String>> result) { 
     // TODO Auto-generated method stub 
     super.onCancelled(result); 
    } 


    @Override 
    protected ArrayList<HashMap<String, String>> doInBackground(
      Boolean... params) { 
     // TODO Auto-generated method stub 

     try { 
      jParser = new JSONParser(); 

      if(cd.isConnectingToInternet()) 
      { 
       String urlstring = ConstValue.JSON_PRODUCTS+"&id="+catMap.get("id"); 
       json = jParser.getJSONFromUrl(urlstring); 
       count = json.getString("count"); 
       if (json.has("data")) { 
        if(json.get("data") instanceof JSONArray){ 
         JSONArray jsonDrList = json.getJSONArray("data"); 
         products_array.clear(); 
         for (int i = 0; i < jsonDrList.length(); i++) { 
          JSONObject obj = jsonDrList.getJSONObject(i); 
          put_object(obj); 
         } 
        }else if(json.get("data") instanceof JSONObject){ 
         put_object(json.getJSONObject("data")); 
        } 
       } 
      }else 
      { 
       Toast.makeText(ProductsActivity.this, "Please connect mobile with working Internet", Toast.LENGTH_LONG).show(); 
      } 

      jParser = null; 
      json = null; 

      } catch (Exception e) { 
       // TODO: handle exception 

       return null; 
      } 
     return null; 
    } 

    public void put_object(JSONObject obj){ 
     HashMap<String, String> map = new HashMap<String, String>(); 


     try { 
     map.put("id", obj.getString("id")); 
     map.put("title", obj.getString("title")); 
     map.put("slug", obj.getString("slug")); 
     map.put("description", obj.getString("description"));  
     map.put("image", obj.getString("image")); 

     map.put("price", obj.getString("price")); 
     map.put("currency", obj.getString("currency")); 
     map.put("discount", obj.getString("discount")); 
     map.put("cod", obj.getString("cod"));  
     map.put("emi", obj.getString("emi"));  
     map.put("status", obj.getString("status"));  

     map.put("gmqty", obj.getString("gmqty"));  
     map.put("unit", obj.getString("unit"));  
     map.put("deliverycharge", obj.getString("deliverycharge"));  
     map.put("tax", obj.getString("tax"));  
     map.put("category_id", obj.getString("category_id")); 

     map.put("on_date", obj.getString("on_date")); 

     map.put("stock", obj.getString("stock")); 
     map.put("type", obj.getString("type")); 
     map.put("total_qty_stock", obj.getString("total_qty_stock")); 
     map.put("consume_qty_stock", obj.getString("consume_qty_stock")); 

     products_array.add(map); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

}

+1

was ist das Problem ..? – Andolasoft

+0

Wenn der Wert inkrementiert oder dekrementiert wird, sollte das Produkt zum Warenkorb hinzugefügt werden – Omkar

Antwort

0

Diese Linie ...

private static int _counter = 0; 

Also, wenn Sie in der Liste auf jedem Element klicken, halten Sie den Überblick über nur eine Nummer für alle Artikel im Adapter, nicht jeder einzelnen Artikel.

Ich schlage vor, dass Sie einige objektorientierte Design definieren, um eine Cart und Product Klassen zu erhalten. So können Sie Elemente in einer Liste mit ihren Details und ihrer Menge sauberer verwalten als eine Hashmap in einer ListView.

Sobald Sie eine Produktklasse haben, empfehle ich Ihnen prüfen, wie eine ArrayAdapter<Product>

0

implementieren Sie eine Schnittstelle in Ihrer Aktivitätsklasse mit der Schnittstelle überschreiben Methode implementieren und die Schnittstelle als Argument an den Adapter Konstruktor übergeben. Lassen Sie uns eine Schnittstelle namens increment erstellen.

public interface Increment { 
    void addCount(boolean ADD_FLAG); 
} 

Implementieren Sie dies in Ihrer Aktivität.

Übergeben Sie die Schnittstelle als Argument an den Adapterkonstruktor wie unten mit Ihrem anderen Argument.

Increment increment = null; 

public ProductsAdapter(Context context, ArrayList<HashMap<String, String>> arraylist, Increment increment) { 
    this.increment = increment; 
} 

Und schließlich in Ihrem Klick-Listener.

//Add this line to increment.. 
increment.addCount(true); 

//Add this line to decrement.. 
    increment.addCount(false); 

this helps :)

0

eine Schnittstelle erstellen, wie;

interface QuantityChangeListener { 
    void onQuantityChanged(HashMap<String, String> map); 
} 

Implementieren Sie diese Schnittstelle, wo Sie Adapter wie oben erstellen.

ProductsAdapter adapter = new ProductAdapter(this, "your list", 
    new QuantityChangeListener() { 
     @Override 
     public void onQuantityChanged(HashMap<String, String> map) { 
       // add item to cart 
     } 
    } 
); 

Übergeben Sie diese Schnittstelle zum Konstruktor der ProductsAdapter Klasse.

private QuantityChangeListener listener; 
public ProductsAdapter(Context context, ArrayList<HashMap<String, String>> arraylist, QuantityChangeListener listener){ 
    this.listener = listener; 
    // do other stuff 
} 

txtplus.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Log.d("src", "Increasing value..."); 
     _counter++; 
     _stringVal = Integer.toString(_counter); 
     value.setText(_stringVal); 
     listener.onQuantityChanged(map); 
    } 
});