如何在TableView中从数据库打印数据?

时间:2019-07-24 02:51:23

标签: android android-sqlite

我想以TableView的形式打印数据库中的数据。如何打印数据。 我使用的数据库是SQLite

2 个答案:

答案 0 :(得分:2)

只需从您的API或服务Information of JSON中获取数据,然后遵循以下参考即可。

Answer ref

答案 1 :(得分:0)

创建文件fetchData.java

import android.os.AsyncTask;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;



public class fetchData extends AsyncTask<Void,Void,Void> {
String data ="";
String dataParsed = "";
String singleParsed ="";
@Override
protected Void doInBackground(Void... voids) {
    try {
        URL url = new URL("https://api.myjson.com/bins/107msz");
        HttpURLConnection httpURLConnection = (HttpURLConnection) 
        url.openConnection();
        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new 
        InputStreamReader(inputStream));
        String line = "";
        while(line != null){
            line = bufferedReader.readLine();
            data = data + line;
        }

        JSONArray JA = new JSONArray(data);
        for(int i =0 ;i <JA.length(); i++){
            JSONObject JO = (JSONObject) JA.get(i);
            singleParsed =  "Name:" + JO.get("name") + "\n"+
                            "Password:" + JO.get("password") + "\n";

            dataParsed = dataParsed + singleParsed +"\n" ;
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return null;
  }

 @Override
 protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);

    MainActivity.data.setText(this.dataParsed);

  }
 }

另一个文件MainActivity.java

  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;

  public class MainActivity extends AppCompatActivity {
  Button click;
  public  static TextView data;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    click = (Button) findViewById(R.id.button);
    data = (TextView) findViewById(R.id.fetcheddata);


    click.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
              fetchData process = new fetchData();
              process.execute();
        }
    });

   }
  }

在您的Activity_main.xml中编写此代码

  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.abhishekpanwar.receivedatajson.MainActivity">

   <Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Click Me!"
   android:layout_marginBottom="10dp"
   android:id="@+id/button"/>

    <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/button">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp"
        android:textSize="40sp"
        android:id="@+id/fetcheddata"
        android:fontFamily="sans-serif"
        android:hint="Fetched Text Here!!!"/>
      </ScrollView>

    </RelativeLayout>