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;
}
}
});
}
}
在运行时,我正在创建线性布局。创建的线性布局数是数据库中的记录数。线性布局有两个文本框和两个按钮。我面临的问题是只有最后一个线性布局的按钮有效。之前的线性布局的按钮不起作用。