2013-11-28 39 views
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快

回答

1

一些提示:

,如果你想獲得的數據從SQL Server,然後寫一個web服務或的WebAPI(MVC)來訪問從服務器數據庫中的數據它將以Json或XML格式返回數據。

然後,您可以輕鬆地用等價的解析器分析數據。

訪問這些數據並通過適配器類放入列表或網格中,該適配器類將擴展BaseAdapter類並在其getView()方法中執行所有操作。

爲了將數據發送到服務器(通過webservice或webapi),只需創建一個JsonStringer對象,並通過鍵值對將數據放入其中,然後通過HttpPost將其發送到服務器。