2013-04-15 3 views
1
public class MyStock extends Activity 
    { 

     DatabaseHelper db; 
     DownloadFileviaHTTP1 d; 
     private mItems[] itemss; 
     EditText txtPriceEdit=null; 
     ProgressDialog progressDialog; 
     public ListView lst_stockListing; 
     List<HashMap<String, String>> fillMaps ; 
     ArrayList<mItems> planetList = new ArrayList<mItems>(); 
     RelativeLayout slider = null,listview=null,topHeader=null,remaniningBucks=null,r,layoutName=null; 
     Button btn_leftpanel,btn_editstock,btnbuy,btncancel,btnaccept,btnsub,btnadd,btnsell,b1,b2; 
     String[] from = new String[] {"companyName", "ButtonBuy", "currentPrice", "ButtonSell"}; 
     int[] to = new int[] { R.id.txtstockname_custom, R.id.btnbuy_custom, R.id.txtcurrent_custom, R.id.btnsell_custom }; 
     TextView txtnifty=null,txtremainingbucks=null,txt_stockqty=null,txt_quantity=null,txtPrice=null,txtPriceChange=null,t1=null,t2=null; 
     LinearLayout mainLinear=null,l=null; 

     @Override 
     public void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.mystock); 

      //Button 
      r=(RelativeLayout)findViewById(R.id.mainStock); 

      btn_leftpanel=(Button)findViewById(R.id.btn_leftpanel); 
      btncancel=(Button)findViewById(R.id.btncancel); 
      btnaccept=(Button)findViewById(R.id.btnaccept); 
      btnsub=(Button)findViewById(R.id.btn_sub); 
      btnadd=(Button)findViewById(R.id.btn_add); 
      btn_editstock=(Button)findViewById(R.id.btn_editstock); 

      //layout 
      listview=(RelativeLayout)findViewById(R.id.listview); 
      slider = (RelativeLayout)findViewById(R.id.layout_relative_slider); 
      topHeader=(RelativeLayout)findViewById(R.id.layout_top_header); 
      layoutName=(RelativeLayout)findViewById(R.id.layout_name); 
      remaniningBucks=(RelativeLayout)findViewById(R.id.layout_remainingbucks); 

      //TextView 
      txtnifty=(TextView)findViewById(R.id.txtnifty50_text); 
      txtremainingbucks=(TextView)findViewById(R.id.txtremainingbucks_text); 
      txt_stockqty=(TextView)findViewById(R.id.txtstockqty); 
      txt_quantity=(TextView)findViewById(R.id.txtquantity); 
      txtPriceEdit=(EditText)findViewById(R.id.txtprice_edit); 
      txtPrice=(TextView)findViewById(R.id.txtprice); 
      txtPriceChange=(TextView)findViewById(R.id.txtprice_change); 

      //listview 
      lst_stockListing=(ListView)findViewById(R.id.lst_mystock); 

      itemss = (mItems[]) getLastNonConfigurationInstance(); 

      d=new DownloadFileviaHTTP1(); 

      txtnifty.setText("100000.00"); 
      txtremainingbucks.setText("100000.00"); 
      txtremainingbucks.setTextColor(android.graphics.Color.rgb(105,160,38)); 

      ArrayList<Items>details = new ArrayList<Items>(); 

      mainLinear=new LinearLayout(this); 
      mainLinear.setOrientation(LinearLayout.VERTICAL); 
      mainLinear.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

      DatabaseHelper db = new DatabaseHelper(MyStock.this,"StockApp"); 
      db.getReadableDatabase(); 
      Cursor cur=db.view_mystock("TBL_MYSTOCK", db); 
      Log.d("cursor count : "," " +cur.getCount()); 

      Items item; 
      int i=0; 
      int j=52; 
      Log.d("curcount : ",""+cur.getCount()); 
      while(cur.moveToNext()) 
      { 
        l=new LinearLayout(this); 
        l.setId(i); 
        l.setBackgroundResource(R.drawable.common_grid2); 
        l.setLayoutParams(new ViewGroup.LayoutParams(android.app.ActionBar.LayoutParams.MATCH_PARENT,70)); 

        t1=new TextView(this); 
        t1.setWidth(210); 
        t1.setGravity(Gravity.CENTER); 
        t1.setHeight(55); 

        btnbuy=new Button(this); 
        btnbuy.setId(i); 
        btnbuy.setHeight(55); 
        btnbuy.setWidth(75); 
        btnbuy.setTextColor(Color.rgb(105, 160, 38)); 
        btnbuy.setBackgroundResource(R.drawable.buy_stock); 

        t2=new TextView(this); 
        t2.setWidth(112); 
        t2.setHeight(55); 

        btnsell=new Button(this); 
        btnsell.setId(j); 
        btnsell.setBackgroundResource(R.drawable.sell_stock2); 
        btnsell.setWidth(75); 
        btnsell.setHeight(55); 
        Log.d("valu of i and j","" +i +" " + j); 

        l.addView(t1); 
        l.addView(btnbuy); 
        l.addView(t2); 
        l.addView(btnsell); 
        mainLinear.addView(l);    
        i++; 
        j++; 
        t1.setText(cur.getString(1)); 
        t2.setText(cur.getString(2)); 

       if (Integer.parseInt(cur.getString(6).toString())>0) 
       { 
        btnbuy.setText(cur.getString(6)); 
        btnbuy.setBackgroundResource(R.drawable.buy_stock_number); 
       } 

       // details.add(item); 
      } 
      listview.addView(mainLinear); 

      function(); 
     } 

     void function() 
     { 
      btn_leftpanel.setOnClickListener(new View.OnClickListener() 
      { 
       public void onClick(View v) 
       { 
        Intent in =new Intent(MyStock.this,ListingItem.class); 
        startActivity(in); 
       } 
      }); 

      btn_editstock.setOnClickListener(new View.OnClickListener() 
      { 
       public void onClick(View v) 
       { 

        Log.d("top position ","" +listview.getTop()); 
       } 
      }); 
      btncancel.setOnClickListener(new View.OnClickListener() 
      { 
       public void onClick(View v) 
       { 
        Log.d("cancel","called"); 
        slider.setVisibility(View.GONE); 
       } 
      }); 
      btnaccept.setOnClickListener(new View.OnClickListener() 
      { 
       public void onClick(View v) 
       {    

       } 
      }); 
      btnadd.setOnClickListener(new View.OnClickListener() 
      { 
       public void onClick(View v) 
       { 
        int val=Integer.parseInt(txt_stockqty.getText().toString()); 
        if (val<100) 
         val=val+10; 

        txt_stockqty.setText(""+val); 
        txt_quantity.setText(""+val); 
        txtPriceEdit.setText(""+txtPrice.getText().toString()); 
        float val1=Float.parseFloat(txtPriceEdit.getText().toString());    
        txtPriceChange.setText("" + val*val1); 
       } 
      }); 
      btnsub.setOnClickListener(new View.OnClickListener() 
      { 
       public void onClick(View v) 
       { 
        int val=Integer.parseInt(txt_stockqty.getText().toString()); 
        if (val>10) 
         val=val-10; 

        txt_stockqty.setText(""+val); 
        txt_quantity.setText(""+val); 
        txtPriceEdit.setText(txtPrice.getText().toString()); 
        float val1=Float.parseFloat(txtPriceEdit.getText().toString()); 
        txtPriceChange. setText(""+val*val1); 
       } 
      }); 
      txtPriceEdit.setOnClickListener(new View.OnClickListener() 
      { 
       public void onClick(View v) 
       { 

       } 
      }); 
      btnbuy.setOnClickListener(new View.OnClickListener() 
      { 
       public void onClick(View v) 
       { 
        Log.d("button id : ",""+btnbuy.getId()); 
         slider.setVisibility(View.VISIBLE); 
         DatabaseHelper db = new DatabaseHelper(MyStock.this,"StockApp"); 
          db.getReadableDatabase(); 
          Cursor cur=db.view_mystock("TBL_MYSTOCK", db); 
          Log.d("cursor count : "," " +cur.getCount()); 
          db.close(); 
          cur.move(btnbuy.getId()); 
          while(cur.moveToNext()) 
          { 
           Toast.makeText(MyStock.this,cur.getString(2), 1000).show(); 
           txtPrice.setText(cur.getString(2)); 
           txtPriceEdit.setText(cur.getString(2)); 
           Float i=Float.parseFloat(txtPrice.getText().toString()) * Integer.parseInt(txt_quantity.getText().toString()); 
           txtPriceChange.setText(""+i); 
           break; 
          } 
       } 
      }); 
     } 
    } 

Lors de l'exécution, je crée une mise en page linéaire. Le nombre de mises en page linéaires créées est le nombre d'enregistrements dans la base de données. La disposition linéaire comporte deux zones de texte et deux boutons. Le problème auquel je suis confronté est que seul le bouton de la dernière disposition linéaire fonctionne. Les boutons de la disposition linéaire précédente ne fonctionnent pas.Le bouton de chaque disposition linéaire créée dynamiquement ne fonctionne pas seul dans la dernière mise en page linéaire

+0

Essayez d'utiliser ArrayAdapter ou BaseAdapter il vous aide .. – AndiM

Répondre

0

Essayez d'utiliser le code ci-dessous ..

public class CustAdp extends ArrayAdapter<String> { 

    Cursor cursor_subCat; 

    public CustAdp(Context context, Cursor cursor_subcategory) { 
     super(context, id); 
     this.cursor_subCat = cursor_subcategory; 
     cursor_subCat.moveToFirst(); 
    } 

    public int getCount() { 
     // TODO Auto-generated method stub 
     return cursor_subCat.getCount(); 
    } 

    public String getItem(int position) { 
     // TODO Auto-generated method stub 
     cursor_subCat.moveToPosition(position); 
     return cursor_subCat.getString(cursor_subCat 
       .getColumnIndex("Sub_CatName")); 
    } 

    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     cursor_subCat.moveToPosition(position); 
     return cursor_subCat.getInt(cursor_subCat 
       .getColumnIndex("Sub_IdAuto")); 
    } 

    @Override 
    public void notifyDataSetChanged() { 
     // TODO Auto-generated method stub 
     super.notifyDataSetChanged(); 
     cursor_subCat.requery(); 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 

     convertView = getLayoutInflater().inflate(R.layout.customsubcat, 
       null); 
     ImageView iv_subcat = (ImageView) convertView 
       .findViewById(R.id.image_region1); 
     TextView tv_subcat = (TextView) convertView 
       .findViewById(R.id.txt_regionname1); 
     Button btn_subcat = (Button) convertView 
       .findViewById(R.id.button_delete_subcat); 

     cursor_subCat.moveToPosition(position); 
     try { 
      byte[] subimage = cursor_subCat.getBlob(cursor_subCat 
        .getColumnIndex("Sub_CatImage")); 
      Bitmap bm = BitmapFactory.decodeByteArray(subimage, 0, 
        subimage.length); 
      iv_subcat.setImageBitmap(bm); 
      tv_subcat.setText(cursor_subCat.getString(cursor_subCat 
        .getColumnIndex("Sub_CatName"))); 
      subcat_id = cursor_subCat.getLong(cursor_subCat 
        .getColumnIndex("Sub_IdAuto")); 
      btn_subcat.setId((int) subcat_id); 
      id = cursor_subCat.getInt(cursor_subCat 
        .getColumnIndex("Sub_MyReceipe")); 
      convertView.setId(position); 
     } catch (Exception e) { 
      // TODO: handle exception 
     } 



     btn_subcat.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       temp = v.getId(); 
       Cursor c_subcat = db_SubCategory 
         .getReceipeFromSubCategory(temp); 
       c_subcat.moveToFirst(); 

       if (c_subcat.getCount() == 0) { 
        data = false; 
        showDialog(0); 
       } else { 
        data = true; 
        showDialog(0); 
       } 

      } 
     }); 
     return convertView; 

    } 

    class ViewHolder { 
     TextView tv_subcat; 
     Button btn_subcat; 
     ImageView iv_subcat; 
    } 
} 

Apporter des modifications dans le code ci-dessus vos besoins ... espérons que cela vous aidera ..

+0

Meghs je ne peux pas en mesure d'obtenir ce que u r essayer de voir. plz aidez-moi .... merci d'avance –

+0

Salut..Ajoutez votre événement de clic de bouton dans la boucle while pas dans la fonction(). Cela fonctionne bien pour moi.Vous pouvez ajouter l'événement de clic du bouton après si la condition dans votre code..Juste essayer .. @ Hag Vaibhav P – AndiM

+0

k grâce il a bien fonctionné .......... –

0

Parce que mon ami, vous définissez le « onclicklistener "s aux vues sélectionnées par le" findViewById "(avant la boucle while), et donc uniquement la définition de l'écouteur à un ensemble de vue. Ce que vous voulez faire mettre le contenu de la "fonction()" dans la boucle, et changer les noms d'affichage de cette fonction pour le nom de vue qui sont dans la boucle while.

ex.

while(cur.moveToNext()){ 
    btnbuy=new Button(this); 
    btnbuy.setId(i); 
    btnbuy.setHeight(55); 
    btnbuy.setWidth(75); 
    btnbuy.setTextColor(Color.rgb(105, 160, 38)); 
    btnbuy.setBackgroundResource(R.drawable.buy_stock); 
    btnbuy.setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View v) { 
      Log.d("button id : ",""+btnbuy.getId()); 
     } 
    }); 
    ... 
} 
+0

également notez, ne pas coder les indications de colonne par ex. 'btnbuy.setText (cur.getString (6))' utiliser quelque chose comme « btnbuy.setText (cur.getString (c.getColumnIndex (YourTableClass.Your_Column_Name_String)); » – LuckyMe

+0

DaRk.FoRcE je ne peux pas en mesure d'obtenir ce ur essayer de voir parce que je ne peux pas mettre la boucle de la création de boutons à l'intérieur du functin comme im créer le bouton au moment de la création de la mise en page linéaire –

+0

Tout ce que je dis est de créer le 'onClickListener' pour chaque' nouveau bouton (this); 'que vous créez.Le mettre dans la boucle principale.le Ce que vous avez fait appelait 'function()' APRES la création complète de TOUS les boutons, donc, SEULEMENT le dernier pointeur a été ajouté à 'setOnClickListener()' Votre problème n'est pas lié à Android, il est plus on la logique de votre code. Si ce que je dis est pas encore clair, laissez-moi savoir et je vais essayer de montrer un code rapide. Btw. "Meghs" a la solution idéaliste, mais c'est plus compliqué. Par cela, je veux dire la prochaine étape de ce que votre niveau de codage. – LuckyMe

Questions connexes