0
所以我有這個項目,但我目前沒有使用SQL數據庫。我有點新,但希望我能在這裏找到一些答案。SQL Server數據到列表視圖或gridview
package com.smartcartv3;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private List<Canned_Goods> myCanned = new ArrayList<Canned_Goods>();
private List<Canned_Goods> myBev = new ArrayList<Canned_Goods>();
private ArrayAdapter<String> myList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myList=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
ListView list = (ListView)findViewById(R.id.listView1);
list.setAdapter(myList);
Button btnClear=(Button)findViewById(R.id.button1);
populateCannedList();
populateBevList();
addcannedtolist();
addbevtolist();
btnClear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
clearOnclicked();
}
private void clearOnclicked() {
myList.clear();
myList.notifyDataSetChanged();
}
});
}
private void addcannedtolist() {
GridView grid =(GridView) findViewById(R.id.gridView1);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position,
long id) {
Canned_Goods clickedCanned=myCanned.get(position);
TextView nameText =(TextView) viewClicked.findViewById(R.id.item_txtName);
nameText.setText(clickedCanned.getName());
TextView priceText =(TextView) viewClicked.findViewById(R.id.item_txtPrice);
priceText.setText("" + clickedCanned.getPrice());
String canned="PHP" + " " + clickedCanned.getPrice()+ " " + clickedCanned.getName();
myList.add(canned);
myList.notifyDataSetChanged();
TextView total=(TextView)findViewById(R.id.textView3);
total.setText(""+ clickedCanned.getPrice());
}});;
}
private void addbevtolist() {
GridView grid =(GridView) findViewById(R.id.gridView2);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position,
long id) {
//Canned_Goods clickedBev=myBev.get(position);
Canned_Goods clickedBev=myBev.get(position);
TextView nameText =(TextView) viewClicked.findViewById(R.id.item_txtName);
nameText.setText(clickedBev.getName());
TextView priceText =(TextView) viewClicked.findViewById(R.id.item_txtPrice);
priceText.setText("" + clickedBev.getPrice());
String bev=clickedBev.getPrice()+ " " + clickedBev.getName();
myList.add(bev);
myList.notifyDataSetChanged();
}});;
}
private void populateCannedList() {
//This line of codes is what i use to add it on the gridview
myCanned.add(new Canned_Goods("Mang Tomas", 300));
myCanned.add(new Canned_Goods("Mang Tomas", 300));
myCanned.add(new Canned_Goods("Mang Tomas", 300));
myCanned.add(new Canned_Goods("Mang Tomas", 300));
myCanned.add(new Canned_Goods("Mang Tomas", 300));
myCanned.add(new Canned_Goods("Mang Tomas", 300));
myCanned.add(new Canned_Goods("Mang Tomas", 300));
myCanned.add(new Canned_Goods("Mang Tomas", 300));
myCanned.add(new Canned_Goods("Mang Tomas", 300));
myCanned.add(new Canned_Goods("Mang Tomas", 300));
myCanned.add(new Canned_Goods("Mang Tomas", 300));
myCanned.add(new Canned_Goods("Mang Tomas", 300));
myCanned.add(new Canned_Goods("Mang Tomas", 300));
myCanned.add(new Canned_Goods("Mang Tomas", 300));
myCanned.add(new Canned_Goods("Mang Tomas", 300));
}
public void Canned(View v) {
GridView grid1 = (GridView) findViewById(R.id.gridView2);
GridView grid = (GridView) findViewById(R.id.gridView1);
ArrayAdapter<Canned_Goods> adapter= new MyListAdapter();
grid.bringToFront();
grid1.setVisibility(View.INVISIBLE);
grid.setVisibility(View.VISIBLE);
grid.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<Canned_Goods>{
public MyListAdapter(){
super(MainActivity.this,R.layout.item_view, myCanned);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView =convertView;
if(itemView == null){
itemView = getLayoutInflater().inflate(R.layout.item_view, parent,false);
}
//Find the Canned goods to work with.
Canned_Goods currentCanned = myCanned.get(position);
//Fill the grid
TextView nameText =(TextView) itemView.findViewById(R.id.item_txtName);
nameText.setText(currentCanned.getName());
TextView priceText =(TextView) itemView.findViewById(R.id.item_txtPrice);
priceText.setText("" + currentCanned.getPrice());
return itemView;
}
}
private void populateBevList() {
myBev.add(new Canned_Goods("Coke", 300));
myBev.add(new Canned_Goods("Coke", 300));
myBev.add(new Canned_Goods("Coke", 300));
myBev.add(new Canned_Goods("Coke", 300));
myBev.add(new Canned_Goods("Coke", 300));
myBev.add(new Canned_Goods("Coke", 300));
myBev.add(new Canned_Goods("Coke", 300));
myBev.add(new Canned_Goods("Coke", 300));
myBev.add(new Canned_Goods("Coke", 300));
myBev.add(new Canned_Goods("Coke", 300));
myBev.add(new Canned_Goods("Coke", 300));
myBev.add(new Canned_Goods("Coke", 300));
myBev.add(new Canned_Goods("Coke", 300));
myBev.add(new Canned_Goods("Coke", 300));
myBev.add(new Canned_Goods("Coke", 300));
}
public void Bev(View v) {
GridView grid1 = (GridView) findViewById(R.id.gridView1);
ArrayAdapter<Canned_Goods> adapter= new MyListAdapter1();
GridView grid = (GridView) findViewById(R.id.gridView2);
grid.bringToFront();
grid1.setVisibility(View.INVISIBLE);
grid.setVisibility(View.VISIBLE);
grid.setAdapter(adapter);
}
private class MyListAdapter1 extends ArrayAdapter<Canned_Goods>{
public MyListAdapter1(){
super(MainActivity.this,R.layout.item_view, myBev);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView =convertView;
if(itemView == null){
itemView = getLayoutInflater().inflate(R.layout.item_view, parent,false);
}
//Find the Canned goods to work with.
Canned_Goods currentCanned = myBev.get(position);
//Fill the grid
TextView nameText =(TextView) itemView.findViewById(R.id.item_txtName);
nameText.setText(currentCanned.getName());
TextView priceText =(TextView) itemView.findViewById(R.id.item_txtPrice);
priceText.setText("" + currentCanned.getPrice());
return itemView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
所以基本上我想改變我的代碼來獲取數據庫中的數據。可能嗎?
順便說一下我使用MS SQL Management Studio中2012快