// Mystock.java
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;
Button btn_leftpanel,btn_editstock,btnbuy,btncancel,btnaccept,btnsub,btnadd,btnsell;
String[] from = new String[] {"companyName", "ButtonBuy", "currentPrice", "ButtonSell"};
private ArrayAdapter<com.indusa.stockapp.stockListing.DownloadFileviaHTTP1.mItems> listAdapter;
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;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mystock);
//Button
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);
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>();
//displyaing the stock in the listview from the table
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;
while(cur.moveToNext())
{
item=new Items();
item.setName(cur.getString(1));
item.setPrice(cur.getString(2));
Log.d("quantity count ",cur.getString(6));
if (Integer.parseInt(cur.getString(6).toString())>0)
{
item.setButtonbackground();
// btnbuy.setBackgroundDrawable(getResources().getDrawable(R.drawable.buy_stock_number));
item.setBuy(cur.getString(6));
}
details.add(item);
}
lst_stockListing.setAdapter(new abc(details , this));
function();
}
public void display_called(Cursor cur)
{
fillMaps= new ArrayList<HashMap<String, String>>();
String s="";
int count=0;
Log.d("cursor count"," "+cur.getCount());
while(cur.moveToNext())
{
Log.d("company name",cur.getString(1));
HashMap<String, String> map = new HashMap<String, String>();
String str=cur.getString(1);
s="";
count=0;
for(int i=0;i<str.length();i++)
{
char c=str.charAt(i);
if (Character.isWhitespace(c)==true)
{
count=count+1;
if (count==2)
s=s+System.getProperty("line.separator");
else
s=s+c;
}
else
{
s=s+c;
}
}
map.put("companyName",s);
map.put("ButtonBuy"," " );
map.put("currentPrice",cur.getString(2));
map.put("ButtonSell"," ");
fillMaps.add(map);
}
}
public static class Items
{
private String companyName = "";
private Button btnbuy=null;
private String price = "";
private String buy,sell;
public Items()
{
}
public Items(String name)
{
this.companyName = name;
}
public Items(String name,String price)
{
this.companyName=name;
this.price=price;
}
public Items(String name,String price,String buy,String sell)
{
this.companyName=name;
this.price=price;
this.buy=buy;
this.sell=sell;
}
public void setButtonbackground()
{
btnbuy.setBackgroundResource(R.drawable.buy_stock_number);
}
public String getName()
{
return companyName;
}
public String getPrice()
{
return price;
}
public String getBuy()
{
return buy;
}
public String getSell()
{
return sell;
}
public void setName(String name)
{
this.companyName = name;
}
public void setPrice(String price)
{
this.price=price;
}
public void setBuy(String buy)
{
this.buy=buy;
}
public void setSell(String sell)
{
this.sell=sell;
}
}
public class abc extends BaseAdapter
{
private ArrayList<Items> _data;
Context _c;
abc (ArrayList<Items> data, Context c)
{
_data = data;
_c = c;
}
public int getCount()
{
return _data.size();
}
public Object getItem(int position)
{
return _data.get(position);
}
public long getItemId(int position)
{
return position;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
final Items item =new Items();
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)_c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.custom_mystock, null);
}
TextView companyName = (TextView) v.findViewById(R.id.txtstockname_custom);
Button buy = (Button)v.findViewById(R.id.btnbuy_custom);
TextView current= (TextView)v.findViewById(R.id.txtcurrent_custom);
Button sell = (Button)v.findViewById(R.id.btnsell_custom);
Items msg = _data.get(position);
Log.d("msg view paret ",""+msg);
companyName.setText(msg.companyName);
current.setText(msg.price);
buy.setText(msg.buy) ;
sell.setText(msg.sell);
buy.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v1)
{
slider.setVisibility(View.VISIBLE);
/* Items i =lst_stockListing.getChildCount();
holder.planetNameView.setText(p.getName());
holder.distView.setText("" + p.getDistance());*/
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(position);
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;
}
}
});
sell.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
slider.setVisibility(View.VISIBLE);
txtPrice.setText(""+position);
Toast.makeText(MyStock.this," "+position, 1000).show();
}
});
return v;
}
}
public class periodicChecker extends Thread
{
public void run()
{
while(true)
{
try
{
final Handler refresh = new Handler(Looper.getMainLooper());
refresh.post(new Runnable()
{
public void run()
{
planetList.clear();
progressDialog = ProgressDialog.show(MyStock.this, "", "Fetching Current Price...");
DatabaseHelper db = new DatabaseHelper(MyStock.this,"StockApp");
db.getWritableDatabase();
Cursor cur=db.view_mystock("TBL_MYSTOCK", db);
Log.d("cursor count : "," " +cur.getCount());
db.close();
display_called(cur);
while(cur.moveToNext())
Log.d("cursor symbol",cur.getString(0));
d.downloadFileViaHTTP1(db);
//listAdapter = new com.indusa.stockapp.stockListing.DownloadFileviaHTTP1.SelectArralAdapter(MyStock.this,planetList);
//lst_stockListing.setAdapter(listAdapter);
}
});
Thread.sleep(5000);
progressDialog.dismiss();
Thread.sleep(50000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
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)
{
}
});
}
}
//===========
//mystock.xml
//=============
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="320dp"
android:layout_height="match_parent"
android:background="@drawable/main_bg"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/layout_name"
android:layout_width="320dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_below="@id/layout_remainingbucks"
android:background="@drawable/top_header2" >
<TextView
android:id="@+id/txtstockname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:paddingLeft="10dp"
android:text="@string/lblstockname"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/txtbuy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:layout_toLeftOf="@+id/txtcurrent"
android:drawableLeft="@drawable/seperator_header2"
android:drawablePadding="10dp"
android:paddingRight="12dp"
android:text="@string/lblbuy"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/txtcurrent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:layout_toLeftOf="@+id/txtsell"
android:drawableLeft="@drawable/seperator_header2"
android:drawablePadding="2dp"
android:paddingRight="2dp"
android:text="@string/lblcurrent"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/txtsell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="3dp"
android:drawableLeft="@drawable/seperator_header2"
android:drawablePadding="10dp"
android:paddingRight="12dp"
android:text="@string/lblsell"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
//==================
//custom_mystock.xml
//===================
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/txtstockname_custom"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/btnbuy_custom"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:singleLine="false"
android:text="@string/lblstockname"
android:textColor="#000000"
android:textSize="14sp" />
<Button
android:id="@+id/btnbuy_custom"
android:layout_width="53dp"
android:layout_height="40dp"
android:layout_toLeftOf="@+id/txtcurrent_custom"
android:drawableLeft="@drawable/seperator_header2"
android:drawablePadding="10dp"
android:background="@drawable/buy_stock2"
android:paddingRight="12dp"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/txtcurrent_custom"
android:layout_width="72dp"
android:layout_height="40dp"
android:layout_toLeftOf="@+id/btnsell_custom"
android:drawableLeft="@drawable/seperator_header2"
android:drawablePadding="2dp"
android:paddingRight="2dp"
android:gravity="center"
android:text="@string/lblcurrent"
android:textColor="#000000"
android:textSize="14sp" />
<Button
android:id="@+id/btnsell_custom"
android:layout_width="52dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:drawableLeft="@drawable/seperator_header2"
android:drawablePadding="10dp"
android:paddingRight="12dp"
android:textColor="#000000"
android:background="@drawable/sell_stock2"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
//========
//logcat
//======
04-11 18:12:17.110: D/cursor(28082): android.database.sqlite.SQLiteCursor@40564dd0
04-11 18:12:17.110: D/cursor count :(28082): 5
04-11 18:12:17.110: D/quantity count(28082): 0
04-11 18:12:17.110: D/quantity count(28082): 0
04-11 18:12:17.110: D/quantity count(28082): 0
04-11 18:12:17.110: D/quantity count(28082): 10
04-11 18:12:17.110: W/dalvikvm(28082): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
04-11 18:12:17.130: E/AndroidRuntime(28082): FATAL EXCEPTION: main
04-11 18:12:17.130: E/AndroidRuntime(28082): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.indusa.stockapp/com.indusa.stockapp.stockListing.MyStock}: java.lang.NullPointerException
04-11 18:12:17.130: E/AndroidRuntime(28082): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
04-11 18:12:17.130: E/AndroidRuntime(28082): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
04-11 18:12:17.130: E/AndroidRuntime(28082): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
04-11 18:12:17.130: E/AndroidRuntime(28082): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
04-11 18:12:17.130: E/AndroidRuntime(28082): at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 18:12:17.130: E/AndroidRuntime(28082): at android.os.Looper.loop(Looper.java:130)
04-11 18:12:17.130: E/AndroidRuntime(28082): at android.app.ActivityThread.main(ActivityThread.java:3770)
04-11 18:12:17.130: E/AndroidRuntime(28082): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 18:12:17.130: E/AndroidRuntime(28082): at java.lang.reflect.Method.invoke(Method.java:507)
04-11 18:12:17.130: E/AndroidRuntime(28082): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
04-11 18:12:17.130: E/AndroidRuntime(28082): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
04-11 18:12:17.130: E/AndroidRuntime(28082): at dalvik.system.NativeStart.main(Native Method)
04-11 18:12:17.130: E/AndroidRuntime(28082): Caused by: java.lang.NullPointerException
04-11 18:12:17.130: E/AndroidRuntime(28082): at com.indusa.stockapp.stockListing.MyStock.onCreate(MyStock.java:110)
//======
//note
//=====
//在上面的代码中,我的列表视图显示了文本框,按钮,文本框和按钮,如果在数据库中的加载时间,如果有任何数量的购买数量,那么我想要//更改R. drawable .buy_stock2到R.drawable.buy_stock_number特定的//按钮我无法根据他们的id区分按钮..
答案 0 :(得分:0)
而不是使用buttons
,您可以使用android imagebutton
提供的良好设施,因为它很容易处理该控件的外观和感觉......
答案 1 :(得分:0)
您正在使用ListView并且它不会为每个项创建新视图,它会重用缓存视图,因此如果您想要更改列表项中的某些属性,您应该在适配器方法中执行此操作
getView(final int position, View convertView, ViewGroup parent)
你可以通过位置参数识别你的项目。 更改listview数据后,您应该调用方法Adapter.notifyDataSetChanged()方法通知列表视图重绘其项目,现在您的getView逻辑工作