java - 如何在 Activity 中将查询结果原始显示为表格?

标签 java android database sqlite android-studio

我正在从 Android studio 的数据库中查询一些原始数据。当我将结果表作为游标时,我想在 Activity 中显示该游标,其中列写在顶部,行像普通表一样显示在另一个下面。因此,此 Activity 的列名称将根据结果查询的列而变化。 知道如何实现这个,或者有我可以使用的模板吗?我是 Android 新手,所以这对你们中的一些人来说可能是一个简单的问题,对此表示抱歉。

最佳答案

所以我最近也有同样的问题。然后我发现了一个非常好的自定义列表项教程。

首先,确保将数据库中的记录保存到对象中。

所以首先您必须创建一个行 View 。因此,创建一个简单的 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;
    }
}

现在在您的 Activity 中添加一个简单的ListView,并为其添加一个ID,例如lv。 然后在您的 Java Activity 中您可以插入以下代码:

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 - 如何在 Activity 中将查询结果原始显示为表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43820335/

相关文章:

java - 在 Windows 10 中设置环境变量以使用 java 和 javac

java - 在 Android Studio 上使用 Android 和 Google App Engine

java - 没有父 json 数组的重复 json 对象的 Json 响应

android - 如何在键盘事件上打开选择输入模式

python - Django 不会在 Postgres 数据库中创建任何表

java Spring : configure properties for sql server with integrated authentication

java - Java 中枚举类型初始化困惑

java - 将字符串编码为 UCS2

sql-server - 检索附加了多个标签的项目

sql - 如何使用 'DAY' 与 'd' 在 Access 中添加()