2017-05-06 9 views
0

私は、Androidスタジオのデータベースからいくつかのローを照会しています。私はカーソルとして結果テーブルを持っているとき、私は、列が上部に書かれ、行が通常のテーブルのように見えているアクティビティーにそのカーソルを表示したいと考えています。したがって、このアクティビティの列名は、結果クエリの列に従って変更されます。 これを実装する方法や、使用できるテンプレートはありますか?私はAndroidの初心者ですので、あなたの中には簡単な質問かもしれません。申し訳ありません。私はこれがあなたのために役に立つかもしれません願っていクエリ結果の生データをアクティビティのテーブルとして表示するには?

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); 

} 

:あなたはSQLの実行からカーソルを取得すると

答えて

0

は、その後、あなたは次のスクリプトを使用することができます。

+0

ありがとうございました。私はデータを取得して到達することができますが、問題はテーブルに応じて変更するアクティビティを持たせたいということです。そのアクティビティには未加工のデータも表示されます。 –

+0

@ M.Kaan私は新しい答えを追加しました。 –

0

Androidには、あなたが宣言しているものに使用できるさまざまなレイアウトがあります。

GridLayoutのTableLayoutを使用できます。この記事で使用すると、1つの選択に関するいくつかの議論を見つけることができます:https://developer.android.com/guide/topics/ui/layout/grid.htmlhttps://developer.android.com/guide/topics/ui/layout/gridview.html

いくつかの追加の推奨事項:

  1. 使用AsyncTaskのためのAndroidのドキュメントからGrid Layout Vs. Table Layout

    を、あなたのようないくつかの例を確認することができますデータベースとやりとりしますが、UIスレッドからは行いません。

  2. 私はコールバックメソッドを選択するので、データアクセスが完了したら、呼び出し側のアクティビティに、(選択したレイアウトを使用して)データを画面に表示する担当者に通知します。
  3. すべてのことを行う大きなロングクラスを作成しないでください。アクティビティーには1つのクラスを使用し、アダプターにはもう1つのクラスを、非同期タスクには別のクラスを使用します。

希望すると、これが役に立ちます。

0

私は最近同様の質問をしています。それから、カスタムリストアイテムで本当に良いチュートリアルを見つけました。

まず、データベースのレコードをobjectに保存してください。

最初に、行ビューを作成する必要があります。したがって、例えば次のコードのような簡単なXMLファイルの挿入を作成し、res/layoutフォルダにmyobject_list_itemという名前で保存します。

<?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を追加し、このためにlvようなIDを追加します。私はこのチュートリアルでは、あなたを助けることを願ってい

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

: は、その後、あなたのJavaのアクティビティでは、次のコードを挿入することができます。

関連する問題