I have two listviews in the same activity. The database consists of sales and purchase data with sinle table in it. Both listviews are filled using this table.
Here is the activity that displays two list:
public class ListPending extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stock_list);
DatabaseHandler db = new DatabaseHandler(this);
ArrayList<stock> stocks = (ArrayList<stock>) db.getIncompleteSales();
ArrayList<stock> stockp = (ArrayList<stock>) db.getIncompletePurchase();
for (stock cn : stocks) {
String log = "Id: "+cn.gettrans()+" ,Name: " + cn.getcommodity() + " ,Date: " + cn.getSdate() + " ,Price: " + cn.getSprice();
// Writing Contacts to log
Log.d("Sales: ", log);}
for (stock sn : stockp) {
String log = "Id: "+sn.gettrans()+" ,Name: " + sn.getcommodity() + " ,Date: " + sn.getPdate() + " ,Price: " + sn.getPprice();
// Writing Contacts to log
Log.d("Purchase: ", log);}
final ListView lv1 = (ListView) findViewById(R.id.saleslist);
MyBaseAdapter mbs = new MyBaseAdapter(this, stocks);
lv1.setAdapter(mbs);
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv1.getItemAtPosition(position);
stock fullObject = (stock)o;
Toast.makeText(ListPending.this, "You have chosen: " + " " + fullObject.getcommodity(), Toast.LENGTH_LONG).show();
Intent int1 = new Intent(ListPending.this, Counter.class );
Bundle mBundle = new Bundle();
mBundle.putInt("ID", fullObject.getID());
mBundle.putString("COMM", fullObject.getcommodity());
mBundle.putInt("LOT", fullObject.getLot());
mBundle.putString("TRANS", fullObject.gettrans());
mBundle.putDouble("PRICE", fullObject.getSprice());
mBundle.putString("DATE", fullObject.getSdate());
int1.putExtras(mBundle);
startActivity(int1);
}
});
final ListView lv2 = (ListView) findViewById(R.id.purchaselist);
MyBaseAdapter mbp = new MyBaseAdapter(this, stockp);
lv2.setAdapter(mbp);
lv2.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v,
int position, long id) {
Object o = lv2.getItemAtPosition(position);
stock fullObject = (stock)o;
Toast.makeText(ListPending.this, "You have chosen: " + " " + fullObject.getcommodity(), Toast.LENGTH_LONG).show();
Intent int1 = new Intent(ListPending.this, Counter.class );
Bundle mBundle = new Bundle();
mBundle.putInt("ID", fullObject.getID());
mBundle.putString("COMM", fullObject.getcommodity());
mBundle.putInt("LOT", fullObject.getLot());
mBundle.putString("TRANS", fullObject.gettrans());
mBundle.putDouble("PRICE", fullObject.getPprice());
mBundle.putString("DATE", fullObject.getPdate());
int1.putExtras(mBundle);
startActivity(int1);
}
});
}
'stocks' and 'stockp' data are correctly printed in log but while inflating the listview, only purchase data gets filled in both the list. Hence, I assume that the DatabaseHandler class is correctly written. Here is myBaseAdapter class:
public class MyBaseAdapter extends BaseAdapter{
private static ArrayList<stock> searchArrayList;
private LayoutInflater mInflater;
public MyBaseAdapter(Context context, ArrayList<stock> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return searchArrayList.size();
}
@Override
public Object getItem(int position) {
return searchArrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.stock_row, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.tvproduct);
holder.txtCode = (TextView) convertView.findViewById(R.id.tvcode);
holder.txtPP = (TextView) convertView.findViewById(R.id.tvpprice);
holder.txtSP = (TextView) convertView.findViewById(R.id.selling_price);
holder.img = (ImageView) convertView.findViewById(R.id.list_image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
double x; String w;
if(searchArrayList.get(position).gettrans().equals("Sales"))
{
x = searchArrayList.get(position).getSprice();
w = searchArrayList.get(position).getSdate();
}
else
{
x = searchArrayList.get(position).getPprice();
w = searchArrayList.get(position).getPdate();
}
int y = searchArrayList.get(position).getID();
int z = searchArrayList.get(position).getLot();
holder.txtName.setText(w);
holder.txtCode.setText(Integer.valueOf(y).toString());
holder.txtPP.setText(Double.valueOf(x).toString());
holder.txtSP.setText(Integer.valueOf(z).toString());
if(searchArrayList.get(position).getcommodity().equals("Gold"))
{ holder.img.setImageResource(R.drawable.gold); }
else if(searchArrayList.get(position).getcommodity().equals("Silver"))
{ holder.img.setImageResource(R.drawable.silver); }
else if(searchArrayList.get(position).getcommodity().equals("Crude Oil"))
{ holder.img.setImageResource(R.drawable.oil); }
else
{ holder.img.setImageResource(R.drawable.gas); }
return convertView;
}
static class ViewHolder {
TextView txtName;
TextView txtCode;
TextView txtPP;
TextView txtSP;
ImageView img;
}
}
And lastly, here is the stock.java file.
public class stock {
//private variables
int id, lot, brokerage;
String sDate, pDate;
Double sPrice, pPrice, PoL, Balance;
String commodity,trans;
// Empty constructor
public stock(){
}
// constructor
public stock(int lot, int brokerage, String date, double price, String commodity, String trans){
this.commodity = commodity;
this.lot = lot;
this.brokerage = brokerage;
this.PoL = 0.0;
this.Balance = 0.0;
this.trans = trans;
if(trans.equals("Sales"))
{
this.sDate = date;
this.sPrice = price;
this.pDate = null;
this.pPrice = 0.0;
}
else
{
this.pDate = date;
this.pPrice = price;
this.sDate = null;
this.sPrice = 0.0;
}
}
// getting ID
public int getID(){
return this.id;
}
// setting id
public void setID(int id){
this.id = id;
}
public int getLot(){
return this.lot;
}
public void setLot(int lot){
this.lot = lot;
}
public int getBroke(){
return this.brokerage;
}
public void setBroke(int broke){
this.brokerage = broke;
}
public String getcommodity(){
return this.commodity;
}
public void setcommodity(String commodity){
this.commodity = commodity;
}
public String gettrans(){
return this.trans;
}
public void settrans(String trans){
this.trans = trans;
}
public String getSdate() {
return this.sDate;
}
public void setSdate(String sdate){
this.sDate = sdate;
}
public String getPdate() {
return this.pDate;
}
public void setPdate(String pdate){
this.pDate = pdate;
}
public void setSprice(double sprice){
this.sPrice = sprice;
}
public double getSprice(){
return this.sPrice;
}
public void setPprice(double pprice){
this.pPrice = pprice;
}
public double getPprice(){
return this.pPrice;
}
public void setPoL(double pol){
this.PoL = pol;
}
public double getPoL(){
return this.PoL;
}
public void setBal(double bal){
this.Balance = bal;
}
public double getBal(){
return this.Balance;
}
}
Also, as the activity as two list views, I want them to work as one. That is, when scrolling the whole activity, items of second list star appearing as soon as items of first list are exhausted. Just like call log wherein the page scrolls as one with logs of different days
Aucun commentaire:
Enregistrer un commentaire