2017-05-06 52 views
0

我在Android Studio中查詢數據庫中的一些原始數據。當我將結果表作爲一個遊標時,我想在一個活動中顯示該遊標,其中列在頂部寫入,行可以像普通表一樣在另一個下面看到。因此,該活動的列名將根據結果查詢的列進行更改。 任何想法如何實現,或者有我可以使用的模板?我對Android很陌生,所以對於你們中的一些人來說這可能是一個簡單的問題,對此抱歉。如何將查詢結果原始數據顯示爲活動中的表格?

回答

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

謝謝你的回答。我可以獲取並獲取數據,但問題是我想根據表格進行更改,並且在該活動中也會看到原始數據。 –

+0

@ M.Kaan我添加了一個新答案。 –

0

Android有不同的佈局,可用於您的狀態。

您可以使用GridLayout的TableLayout。在這篇文章,你可以找到一些討論有關哪一個選擇:Grid Layout Vs. Table Layout

從Android的文檔,你可以檢查一些例子,如:https://developer.android.com/guide/topics/ui/layout/grid.htmlhttps://developer.android.com/guide/topics/ui/layout/gridview.html

一些補充建議:

  1. 使用的AsyncTask的與數據庫交互,不要從UI線程那樣做。
  2. 我選擇回撥方式,因此,當數據訪問完成時,您需要通知負責顯示屏幕上數據的呼叫活動(使用您選擇的佈局)。
  3. 不要創建大型的長課堂。一個類用於活動,另一個用於適配器,另一個用於異步任務。

希望這可以幫助你。

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. 
    } 
} 

我希望本教程可以幫助你。

相關問題