java - Java Android中的Sql查询
问题描述
我有很多问题没有找到答案。
首先,我在MS Sql server中有两张表,一张叫country,包含country id和country name,一张叫family,包含country id,family id,family name和family info。我面临的问题是如何在 Java Android 中编写一个查询来获取国家/地区的 id 并将其与家庭表中国家/地区的 id 进行比较,然后每个国家/地区都有自己的家庭。
我尝试过内部连接,但不知道该怎么做。
到目前为止,这是我的代码:
public class InsideCountries extends AppCompatActivity {
private ArrayList<ClassListItems> itemArrayList; //List items Array
private MyAppAdapter myAppAdapter; //Array Adapter
private ListView listView;
private TextView ttl;// Listview
private boolean success = false; // boolean
private ConnectionClass connectionClass; //Connection Class Variable
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inside_countries);
getSupportActionBar().hide();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
ttl = (TextView) findViewById(R.id.title) ;
ttl.setText(getIntent().getStringExtra("CountriesName"));
listView = (ListView) findViewById(R.id.listvieww); //Listview Declaration
connectionClass = new ConnectionClass(); // Connection Class Initialization
itemArrayList = new ArrayList<ClassListItems>(); // Arraylist Initialization
// Calling Async Task
SyncData orderData = new SyncData();
orderData.execute("");
}
// Async Task has three overrided methods,
private class SyncData extends AsyncTask<String, String, String> {
String msg = "No Data Found";
ProgressDialog progress;
@Override
protected void onPreExecute() //Starts the progress dailog
{
progress = ProgressDialog.show(InsideCountries.this, "Synchronising",
"Please Wait...", true);
}
@Override
protected String doInBackground(String... strings) // Connect to the database, write query and add items to array list
{
try {
Connection conn = connectionClass.CONN(); //Connection Object
if (conn == null) {
msg = "Please Check Your Connection";
success = false;
} else {
// Change below query according to your own database.
String query = "query here";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
if (rs != null) // if resultset not null, I add items to itemArraylist using class created
{
while (rs.next()) {
try {
itemArrayList.add(new ClassListItems(rs.getString("name")));
} catch (Exception ex) {
ex.printStackTrace();
}
}
msg = "Found";
success = true;
} else {
msg = "No Data found!";
success = false;
}
}
} catch(Exception e)
{
e.printStackTrace();
Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
msg = writer.toString();
success = false;
}
return msg;
}
@Override
protected void onPostExecute (String
msg) // disimissing progress dialoge, showing error and setting up my listview
{
progress.dismiss();
Toast.makeText(InsideCountries.this, msg + "", Toast.LENGTH_LONG).show();
if (success == false) {
} else {
try {
myAppAdapter = new MyAppAdapter(itemArrayList, InsideCountries.this);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(myAppAdapter);
} catch (Exception ex) {
}
}
}
}
public class MyAppAdapter extends BaseAdapter //has a class viewholder which holds
{
public class ViewHolder
{
TextView textName;
}
public List<ClassListItems> parkingList;
public Context context;
ArrayList<ClassListItems> arraylist;
private MyAppAdapter(List<ClassListItems> apps, Context context)
{
this.parkingList = apps;
this.context = context;
arraylist = new ArrayList<ClassListItems>();
arraylist.addAll(parkingList);
}
@Override
public int getCount() {
return parkingList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) // inflating the layout and initializing widgets
{
View rowView = convertView;
ViewHolder viewHolder= null;
if (rowView == null)
{
LayoutInflater inflater = getLayoutInflater();
rowView = inflater.inflate(R.layout.list_content, parent, false);
viewHolder = new ViewHolder();
viewHolder.textName = (TextView) rowView.findViewById(R.id.textName);
rowView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
// here setting up names and images
viewHolder.textName.setText(parkingList.get(position).getName()+"");
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(InsideCountries.this,Inside_Families.class);
intent.putExtra("Family",parkingList.get(position).getName());
startActivity(intent);
}
});
return rowView;
}
}
}
任何帮助将不胜感激!?
解决方案
推荐阅读
- python - 如何从数组中显示康威的生命游戏
- matlab - 为什么 Matlab 会更改作为主对象属性的所有对象的值?
- c# - 如何访问列表中每个对象的项目并对其执行一些操作?
- javascript - 角中性能.navigation.type 的替代品是什么?
- mysql - 带有子选择的慢速运行查询
- c# - 收藏已修改;当我不修改循环内的集合时,枚举操作可能不会执行
- sapui5 - ui5-tooling build 创建的 dist 文件夹在部署到 SCP 时不起作用
- cors - 使用 CDN 中的 Ionicons 时如何解决 CORS 问题?
- r - 从 data.table 到 matrix 并用 0 填充缺失的转换
- javascript - 内容安全策略“拒绝执行内联事件处理程序”错误