【问题标题】:How to retrieve data from ms sql to text view in android studio如何在 android studio 中从 ms sql 检索数据到文本视图
【发布时间】:2019-08-04 16:36:00
【问题描述】:

我正在尝试从 ms sql server 中的表中获取数据到 android 中的 textview

我已经尝试了所有方法,包括异步任务和 doinbackground,但都没有成功

这是我的代码:

 private TextView fam;
    private TextView inf;
    private boolean success = false; // boolean
    private ConnectionClass connectionClass; 

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

        getSupportActionBar().hide();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        fam = (TextView) findViewById(R.id.ttl2);
        fam.setText(getIntent().getStringExtra("Family"));
        inf = (TextView) findViewById(R.id.info);


        connectionClass = new ConnectionClass(); 



        SyncData orderData = new SyncData();
        orderData.execute("");
    }


    private class SyncData extends AsyncTask<String, String, String> {
        String msg = "No Data Found";
        ProgressDialog progress;

        @Override
        protected void onPreExecute() 
        {
            progress = ProgressDialog.show(Inside_Families.this, "Synchronising",
                    "Please Wait...", true);
        }

        @Override
        protected String doInBackground(String... strings)  
        {
            try {
                Connection conn = connectionClass.CONN(); 
                if (conn == null) {
                    msg = "Please Check Your Connection";
                    success = false;
                } else if (fam.getText().equals("القحطاني")) {


                    String query = "SELECT Info FROM faminfo";
                    Statement stmt = conn.createStatement();
                    ResultSet rs = stmt.executeQuery(query);


                    if (rs != null) 
                    {
                        while (rs.next()) {
                            try {
                                inf.setText((rs.getString("Info")));
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                        msg = "Found";
                        success = true;
                    } else {
                        msg = "No Data found!";
                        success = false;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                Writer writer = new StringWriter();
                e.printStackTrace(new PrintWriter(writer));
                msg = writer.toString();
                success = false;
            }
            return msg;
        }

        @Override
        protected void onPostExecute(String
                                             msg) 
        {
            progress.dismiss();
            Toast.makeText(Inside_Families.this, msg + "", Toast.LENGTH_LONG).show();
            if (success == false) {
            } else {
                try {

                } catch (Exception ex) {

                }

            }
        }
    }

它没有将数据检索到文本视图中的问题,这就是 logcat 显示的:

2019-08-04 19:16:26.465 32026-32495/com.gohool.firstlook.algbail W/System.err: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
2019-08-04 19:16:26.465 32026-32495/com.gohool.firstlook.algbail W/System.err:     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:8530)
2019-08-04 19:16:26.465 32026-32495/com.gohool.firstlook.algbail W/System.err:     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1432)
2019-08-04 19:16:26.465 32026-32495/com.gohool.firstlook.algbail W/System.err:     at android.view.View.requestLayout(View.java:23224)
2019-08-04 19:16:26.466 32026-32495/com.gohool.firstlook.algbail I/chatty: uid=10275(u0_a275) AsyncTask #2 identical 4 lines
2019-08-04 19:16:26.466 32026-32495/com.gohool.firstlook.algbail W/System.err:     at android.view.View.requestLayout(View.java:23224)
2019-08-04 19:16:26.466 32026-32495/com.gohool.firstlook.algbail W/System.err:     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:360)
2019-08-04 19:16:26.466 32026-32495/com.gohool.firstlook.algbail W/System.err:     at android.view.View.requestLayout(View.java:23224)
2019-08-04 19:16:26.466 32026-32495/com.gohool.firstlook.algbail W/System.err:     at android.widget.TextView.checkForRelayout(TextView.java:9789)
2019-08-04 19:16:26.466 32026-32495/com.gohool.firstlook.algbail W/System.err:     at android.widget.TextView.setText(TextView.java:6023)
2019-08-04 19:16:26.466 32026-32495/com.gohool.firstlook.algbail W/System.err:     at android.widget.TextView.setText(TextView.java:5849)
2019-08-04 19:16:26.466 32026-32495/com.gohool.firstlook.algbail W/System.err:     at android.widget.TextView.setText(TextView.java:5806)
2019-08-04 19:16:26.466 32026-32495/com.gohool.firstlook.algbail W/System.err:     at com.gohool.firstlook.algbail.Inside_Families$SyncData.doInBackground(Inside_Families.java:79)
2019-08-04 19:16:26.466 32026-32495/com.gohool.firstlook.algbail W/System.err:     at com.gohool.firstlook.algbail.Inside_Families$SyncData.doInBackground(Inside_Families.java:48)
2019-08-04 19:16:26.466 32026-32495/com.gohool.firstlook.algbail W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:333)
2019-08-04 19:16:26.467 32026-32495/com.gohool.firstlook.algbail W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2019-08-04 19:16:26.467 32026-32495/com.gohool.firstlook.algbail W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
2019-08-04 19:16:26.467 32026-32495/com.gohool.firstlook.algbail W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
2019-08-04 19:16:26.467 32026-32495/com.gohool.firstlook.algbail W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
2019-08-04 19:16:26.467 32026-32495/com.gohool.firstlook.algbail W/System.err:     at java.lang.Thread.run(Thread.java:764)

有人可以帮我完成这项任务吗?!

任何帮助将不胜感激!

【问题讨论】:

  • 您需要一个服务器端 API 来连接到服务器以从您的应用程序中检索数据,您需要与该 API 通信以从服务器获取数据并在您的应用程序中显示它们。根据情况,有很多方法,例如凌空/滑行。请参阅有关它们的一些教程

标签: java android sql sql-server


【解决方案1】:

您的错误是:只有创建视图层次结构的原始线程才能接触其视图。

正是因为这一行

inf.setText((rs.getString("Info")));

在使用 AsyncTask 时请记住,不要从后台线程更新视图。您会遇到错误,因为您尝试从后台线程设置文本。

要解决您的错误,请将所有更新 UI 的任务(如 setText)移至 onPostExecute() 方法,这通常是最佳做法。

或者,如果您想从 doInBackground 线程进行一些 UI 更改,您可以尝试这样的操作,

doInBackground() {
    runOnUiThread(new Runnable() {
                public void run() {
                    // some code #3 (Write your code here to run in UI thread)
                    inf.setText((rs.getString("Info")));
                }
            });
}

【讨论】:

    猜你喜欢
    • 2019-03-11
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    • 2019-07-03
    相关资源
    最近更新 更多