Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在活动中将查询结果RAW显示为表?_Java_Android_Database_Sqlite_Android Studio - Fatal编程技术网

Java 如何在活动中将查询结果RAW显示为表?

Java 如何在活动中将查询结果RAW显示为表?,java,android,database,sqlite,android-studio,Java,Android,Database,Sqlite,Android Studio,我正在从Android studio的数据库中查询一些RAW。当我将结果表作为游标时,我希望在活动中显示该游标,其中列写在顶部,行在另一行下面,就像普通表一样。因此,此活动的列名将根据结果查询的列进行更改。 你知道如何实现这一点吗,或者有没有我可以使用的模板?我是Android新手,所以对你们中的一些人来说,这可能是一个简单的问题,对此表示抱歉。当您从SQL执行中获得光标时,您可以使用以下脚本: Cursor c = db.rawQuery("SELECT * FROM my_table", n

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

当您从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);

}

我希望这可能会对您有所帮助。

Android有不同的布局,可用于您的状态

您可以使用GridLayout的TableLayout。在这篇文章中,你可以找到一些关于选择哪一个的讨论:

从Android文档中,您可以查看以下示例: 及

一些补充建议:

使用AsyncTask与数据库交互,不要从UI线程执行此操作。 我选择回调方法,因此,当数据访问完成时,您会通知负责使用您选择的布局在屏幕上显示数据的调用活动。 不要创建大而长的类来做所有的事情。一个类用于活动,另一个类用于适配器,另一个类用于异步任务。
希望这对你有所帮助。

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

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

因此,首先必须创建一个行视图。因此,创建一个简单的XML文件,例如插入以下代码,并将其保存为res/layout文件夹中的myobject_list_项

现在在活动中添加一个简单的ListView,并为此添加一个类似lv的id。 然后,您可以在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;
    }
}
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.
    }
}

希望本教程能对您有所帮助。

谢谢您的回答。我可以获取并获取数据,但问题是我希望有一个活动可以根据表进行更改,并且在该活动中也可以看到RAW。@M.Kaan我添加了一个新的答案。
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.
    }
}