嗨,我是安卓新手。我想从sql server数据库访问具有列名的完整表我已经将值绑定到列(1,2,3)到(A,B,C)。我想要n个列和行,请帮助我.. .
这是活动:
public class MainActivity1 extends Activity{
Button EXECUTAR;
EditText ValorBusca;
ListView Lista;
SimpleAdapter AD;
MainActivity ma=new MainActivity();
TableRow tr;
public void QuerySQL(String COMANDOSQL) {
ResultSet rs;
try {
//String sql="select * from Activity";
Statement statement = ma.connect.createStatement();
//DatabaseMetaData md = ma.connect.getMetaData();
///rs = statement.executeQuery(COMANDOSQL);
/// rs = statement.executeQuery("SELECT INV.ITEM_NAME,INS.QTY,INS.RATE,INV.discount,INS.[STD RATE],INS.MRP,INS.GRPCODE,INV.cname1,INV.cname2,INV.cname3,INV.cname4,INV.cname5,INV.cname6,INV.WSP,INV.TAXCODE,INS.ICODE,INV.BARCODE,INS.[EFF RATE],TAX.TaxName FROM INVITEM INV LEFT JOIN TaxMaster TAX ON TAX.TaxCode=INV.TAXCODE LEFT JOIN INVSTOCK INS ON INS.ICODE='"+ValorBusca.getText().toString()+"'");
rs = statement.executeQuery("SELECT * FROM "+ValorBusca.getText().toString()+"");
List<Map<String, String>> data = null;
data = new ArrayList<Map<String, String>>();
while(rs.next()) {
Map<String, String> datanum =new HashMap<String, String>();
datanum.put("A",rs.getString(1));
datanum.put("B",rs.getString(2));
datanum.put("c",rs.getString(3));
data.add(datanum);
}
String[] from = {"A","B","c"};
int[] views = {R.id.txttitulo,R.id.txtconteudo,R.id.textview3};
AD = new SimpleAdapter(this, data, R.layout.modelo, from, views)
{
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.modelo, null);
}
TextView text1 = (TextView) v.findViewById(R.id.txttitulo);
TextView text2 = (TextView) v.findViewById(R.id.txtconteudo);
TextView text3 = (TextView) v.findViewById(R.id.textview3);
text1.setBackgroundColor(Color.GREEN);
text2.setBackgroundColor(Color.CYAN);
text3.setBackgroundColor(Color.RED);
return super.getView(position, v, parent);
}
};
Lista.setAdapter(AD);
} catch (Exception e) {
Log.e("ERRO",e.getMessage());
Toast.makeText(getBaseContext(),"Enter Table Name",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
EXECUTAR = (Button) findViewById(R.id.btn_buscar);
ValorBusca = (EditText) findViewById(R.id.txt_buscar);
Lista = (ListView) findViewById(R.id.listView1);
ValorBusca.setText("");
EXECUTAR.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
QuerySQL(null);
}
});
}
}