我在Android Studio中查詢數據庫中的一些原始數據。當我將結果表作爲一個遊標時,我想在一個活動中顯示該遊標,其中列在頂部寫入,行可以像普通表一樣在另一個下面看到。因此,該活動的列名將根據結果查詢的列進行更改。 任何想法如何實現,或者有我可以使用的模板?我對Android很陌生,所以對於你們中的一些人來說這可能是一個簡單的問題,對此抱歉。如何將查詢結果原始數據顯示爲活動中的表格?
0
A
回答
0
當您從SQL執行一個光標,那麼你可以使用下面的腳本:
Cursor c = db.rawQuery("SELECT * FROM my_table", null);
while (c.moveToNext()) { //Loop through all the records
//Now on the variable 'c' there is one record.
int column_a_name = c.getColumnIndex("my_column"); //Get the index of the column from your table.
String column_a_value = c.getString(column_a_name); //Get the value from the column from the current record.
//Now you can do with the value what you want.
System.out.println(column_a_value);
}
我希望這也許對你有所幫助。
0
Android有不同的佈局,可用於您的狀態。
您可以使用GridLayout的TableLayout。在這篇文章,你可以找到一些討論有關哪一個選擇:Grid Layout Vs. Table Layout
從Android的文檔,你可以檢查一些例子,如:https://developer.android.com/guide/topics/ui/layout/grid.html 和https://developer.android.com/guide/topics/ui/layout/gridview.html
一些補充建議:
- 使用的AsyncTask的與數據庫交互,不要從UI線程那樣做。
- 我選擇回撥方式,因此,當數據訪問完成時,您需要通知負責顯示屏幕上數據的呼叫活動(使用您選擇的佈局)。
- 不要創建大型的長課堂。一個類用於活動,另一個用於適配器,另一個用於異步任務。
希望這可以幫助你。
0
所以我最近也有同樣的問題。然後我發現了一個非常好的自定義列表項的教程。
首先,確保將數據庫中的記錄保存到object
。
所以首先你必須創建一個行視圖。因此,創建一個簡單的XML文件插入例如下面的代碼,並將其保存爲myobject_list_item
在您的res/layout
文件夾中。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="@+id/column1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/column2"
android:padding="10dp"
android:paddingLeft="5dp"
android:text="Column1"
android:textAppearance="@android:style/TextAppearance.Material.Large"
android:textSize="20sp" />
<TextView
android:id="@+id/column2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/gewichtung"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:padding="10dp"
android:text="Column2"
android:textAppearance="@android:style/TextAppearance.Material.Large"
android:textSize="20sp" />
</RelativeLayout>
之後,您必須創建一個自定義列表適配器。因此,創建一個名爲MyObject_ListAdapter
一個新的Java文件,並插入下面的代碼:
package net.example.app;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import net.example.app.R;
import java.util.ArrayList;
public class MyObject_ListAdapter extends ArrayAdapter<MyObject> {
// Source:
// http://hmkcode.com/android-custom-listview-items-row/
private ArrayList<MyObject> objects;
private Context context;
public MyObject_ListAdapter(Context context, ArrayList<MyObject> objects) {
super(context, R.layout.myobject_list_item, objects);
this.objects = objects;
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
View rowView = inflater.inflate(R.layout.fach_list_item, parent, false);
// 3. Get the two text view from the rowView
TextView column1 = (TextView) rowView.findViewById(R.id.column1);
TextView column2 = (TextView) rowView.findViewById(R.id.column2);
RelativeLayout item = (RelativeLayout) rowView.findViewById(R.id.item);
// 4. Set the text for textView
column1.setText(objects.get(position).getName());
column2.setText(objects.get(position).getSecondName());
// 5. return rowView
return rowView;
}
}
添加在現在你的活動的簡單ListView
並添加此一個ID像lv
。 然後在你的Java活動,你可以插入以下代碼:
package net.example.app;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import net.example.app.MyObject;
import net.example.app.MyObject_ListAdapter;
import java.util.ArrayList;
public class MyActivity extends AppCompatActivity {
private ArrayAdapter arrayAdapter;
private ArrayList<MyObject> myObjects = new ArrayList<>();
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);
db = openOrCreateDatabase("database.db", MODE_PRIVATE, null);
ListView lv = (ListView) findViewById(R.id.lv);
arrayAdapter = new MyObject_ListAdapter(this, myObjects); //Define the custom list adapter with the activity and arraylist
lv.setAdapter(arrayAdapter); //Connect your listview with the adapter
displayData();
}
private void displayData() {
Cursor c = db.rawQuery("SELECT * FROM my_table", null);
while (c.moveToNext()) { //Loop through all the records
//Now on the variable 'c' there is one record.
int column_a_name = c.getColumnIndex("my_column1"); //Get the index of the column from your table.
String column_a_value = c.getString(column_a_name); //Get the value from the column from the current record.
int column_b_name = c.getColumnIndex("my_column2");
String column_b_value = c.getString(column_b_name);
//Now you can do with the value what you want.
myObjects.add(new MyObject(column_a_value, column_b_value));
}
arrayAdapter.notifyDataSetChanged(); //Notify, that you have changed some data in the array list.
}
}
我希望本教程可以幫助你。
相關問題
- 1. 如何顯示HTML表格原始的SQL查詢輸出
- 2. Android搜索功能;在原始活動中顯示結果?
- 3. 如何在Laravel中將原始查詢寫入原始數據?
- 4. 查詢SQL數據庫表格視圖並在HTML表格中顯示結果
- 5. 將MySQL查詢結果顯示爲XML
- 6. 查詢3個表格,在一個表格中顯示結果
- 7. Android的原始查詢結果檢查
- 8. 將SQL數據庫查詢結果顯示爲鏈接
- 9. 查詢結果和顯示數據
- 10. 顯示數據庫查詢結果
- 11. 如何從SQL數據庫表列中總結查詢結果並將結果顯示在標籤中?
- 12. 如何將查詢結果分離爲表示每個月的數據的列?
- 13. 查詢在圖表中顯示結果
- 14. 在表中顯示查詢結果
- 15. 在PHP中的HTML表格中顯示查詢結果
- 16. ServiceStack.OrmLite的原始結果查詢
- 17. 顯示從我的數據庫結果到列表活動
- 18. Android的 - 顯示查詢的結果在活動
- 19. 格式化查詢顯示結果
- 20. 如何結合兩個MySQL查詢在一個表格行中顯示結果
- 21. 顯示結果的查詢
- 22. 如何使用ZK在網格中顯示查詢結果?
- 23. 如何顯示查詢結果
- 24. 如何顯示在MySQL查詢結果
- 25. 如何在查詢中顯示帶結果集的表名?
- 26. 如何在一個表中顯示兩個查詢的結果
- 27. Laravel 4.1原始查詢訪問結果
- 28. NDB查詢返回零結果。數據存儲顯示結果
- 29. 使用數據庫查詢結果而不是顯示在數據網格中
- 30. 爲動態原生SQL查詢的結果執行結果行
謝謝你的回答。我可以獲取並獲取數據,但問題是我想根據表格進行更改,並且在該活動中也會看到原始數據。 –
@ M.Kaan我添加了一個新答案。 –