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;
}
}
});
}
}
在运行时我正在创建linearlayout。创建的线性布局的数量是数据库中的记录数。线性布局有两个文本框和两个按钮。我面临的问题是只有最后一个线性布局的按钮才有效。先前线性布局的按钮不起作用。
答案 0 :(得分:0)
尝试使用以下代码..
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;
}
}
根据您的要求对上述代码进行更改...希望它能帮到您..
答案 1 :(得分:0)
因为我的朋友,你正在将“onclicklistener”设置为“findViewById”选择的视图(在while循环之前),因此只将监听器设置为一组视图。 你想要做什么把“function()”的内容放在循环中,并将该函数的视图名称更改为while循环内的视图名称。
离。
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());
}
});
...
}