首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >值到TextView

值到TextView
EN

Stack Overflow用户
提问于 2017-08-09 19:58:21
回答 2查看 429关注 0票数 1

我已经按照Wintech的教程建立了到MS数据库的连接,我的结果是在调试器中获得200,所以我知道它正在成功地连接。我的问题是,我无法从查询结果显示在应用程序.

activity_main.xml:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
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.example.pmatthews.appointmentcreator.MainActivity">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg"

    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:onClick="openWebpage"
        android:src="@drawable/logo" />

    <ScrollView

        android:id="@+id/scroll"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="0dp"

                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"

                    android:layout_marginTop="5dp"
                    android:layout_weight="1"
                    android:text="Appointment Number:"
                    android:textColor="#000000" />


                <EditText
                    android:id="@+id/ApptNumView"

                    android:layout_width="0dp"
                    android:layout_height="wrap_content"

                    android:layout_marginRight="15dp"
                    android:layout_marginTop="5dp"
                    android:layout_weight="1" />

                <ImageButton
                    android:id="@+id/refreshButton"

                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:background="@drawable/refresh"
                    android:onClick="testConnection" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"

                android:orientation="horizontal">

                <TextView
                    android:id="@+id/textView"

                    android:layout_width="0dp"

                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"

                    android:layout_marginTop="5dp"
                    android:layout_weight="1"
                    android:text="Material Owner Number:"
                    android:textColor="#000000" />

                <TextView
                    android:id="@+id/MatOwnNumView"

                    android:layout_width="0dp"
                    android:layout_height="wrap_content"

                    android:layout_marginRight="15dp"
                    android:layout_marginTop="5dp"
                    android:layout_weight="1" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"

                android:orientation="horizontal">

                <TextView
                    android:layout_width="0dp"

                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"

                    android:layout_marginTop="5dp"
                    android:layout_weight="6.5"
                    android:text="Carrier Number:"
                    android:textColor="#000000" />

                <TextView
                    android:id="@+id/CarrNumView"

                    android:layout_width="0dp"
                    android:layout_height="wrap_content"

                    android:layout_marginRight="15dp"
                    android:layout_marginTop="5dp"
                    android:layout_weight="4" />

                <ImageButton
                    android:id="@+id/CarrNumButton"

                    android:layout_width="30dp"

                    android:layout_height="30dp"
                    android:background="@drawable/searchicon"


                    />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="82dp"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="0dp"

                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"


                    android:layout_marginTop="5dp"
                    android:layout_weight="1"
                    android:text="Purchase Order Number:"
                    android:textColor="#000000" />

                <EditText
                    android:id="@+id/PONInput"

                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="15dp"
                    android:layout_marginTop="5dp"
                    android:layout_weight="1" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="82dp"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="0dp"

                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"


                    android:layout_marginTop="5dp"
                    android:layout_weight="1"
                    android:text="Release Number:"
                    android:textColor="#000000" />

                <EditText
                    android:id="@+id/RelNumInput"

                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="15dp"
                    android:layout_marginTop="5dp"
                    android:layout_weight="1" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="0dp"


                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:layout_marginTop="5dp"

                    android:layout_weight="1"
                    android:text="Rail Car No:"
                    android:textColor="#000000" />

                <EditText
                    android:id="@+id/RailCarInput"

                    android:layout_width="0dp"
                    android:layout_height="wrap_content"

                    android:layout_marginRight="15dp"

                    android:layout_marginTop="5dp"
                    android:layout_weight="1" />
            </LinearLayout>


            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"

                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"

                android:text="Actuals"

                android:textColor="#000000"
                android:textSize="30sp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <CheckBox
                    android:id="@+id/truckArrivedBox"

                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"

                    android:layout_weight="1" />

                <TextView
                    android:layout_width="0dp"

                    android:layout_height="wrap_content"
                    android:layout_weight="7"

                    android:text="Truck has Arrived"

                    android:textColor="#000000" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="0dp"

                        android:layout_height="wrap_content"

                        android:layout_marginLeft="15dp"
                        android:layout_marginTop="20dp"

                        android:layout_weight="1"
                        android:text="Bill Lading No In:"

                        android:textColor="#000000" />

                    <EditText
                        android:id="@+id/billNumInput"

                        android:layout_width="0dp"
                        android:layout_height="wrap_content"

                        android:layout_marginRight="15dp"
                        android:layout_marginTop="20dp"
                        android:layout_weight="1" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"

                        android:layout_height="wrap_content"

                        android:layout_marginLeft="15dp"
                        android:layout_marginTop="40dp"

                        android:layout_weight="1"
                        android:text="Arrival Date:"
                        android:textColor="#000000" />

                    <DatePicker
                        android:id="@+id/arrivalDateInput"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:layout_marginTop="20dp"
                        android:datePickerMode="calendar"


                        />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="fill_parent"

                        android:layout_height="wrap_content"

                        android:layout_marginLeft="15dp"
                        android:layout_marginTop="20dp"

                        android:layout_weight="1"

                        android:text="Arrival Time:"
                        android:textColor="#000000" />

                    <TimePicker
                        android:id="@+id/arrivalTimeInput"

                        android:layout_width="fill_parent"

                        android:layout_height="wrap_content"
                        android:layout_marginRight="15dp"

                        android:layout_marginTop="20dp"
                        android:layout_weight="1"
                        android:timePickerMode="spinner" />


                </LinearLayout>
            </LinearLayout>

            <LinearLayout

                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <Button
                    android:id="@+id/recieveButton"

                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:layout_marginRight="15dp"
                    android:text="Recieve Inventory" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <Button
                        android:id="@+id/saveButton"

                        android:layout_width="0dp"

                        android:layout_height="wrap_content"
                        android:layout_marginLeft="15dp"

                        android:layout_weight="1"
                        android:text="Save"

                        />

                    <Button
                        android:id="@+id/cancelButton"

                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="15dp"

                        android:layout_weight="1"
                        android:text="Cancel" />
                </LinearLayout>

            </LinearLayout>

            <ListView
                android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"></ListView>

            <ListView
                android:id="@+id/listView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            </ListView>




            <TextView
                android:id="@+id/txtConNum"
                android:layout_width="fill_parent"
                android:layout_height="100dp"/>





        </LinearLayout>

    </ScrollView>

</LinearLayout>


</android.support.constraint.ConstraintLayout>

MainActivity.java:

代码语言:javascript
复制
package com.example.pandasai.project;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.view.View;
import android.content.Intent;
import android.net.Uri;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;


import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import java.util.ArrayList;
import java.util.HashMap;
import android.os.AsyncTask;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import static com.example.pandasai.project.R.id.txtConNum;


public class MainActivity extends AppCompatActivity {

    ArrayList<HashMap<String, String>> conNumList;
    ListAdapter adapter;




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


    conNumList = new ArrayList<HashMap<String, String>>();
    ListView list = (ListView) findViewById(R.id.list);
    new GetConNum(MainActivity.this, 
list).execute("http://xxx.xxx.xxx.xxx/LocalAppTest/Service1.svc/GetConNum");
}
class GetConNum extends AsyncTask<String, Void, String>{

String status = null;
Activity context;
ListView listView;
public GetConNum(Activity context, ListView listView){
    this.context = context;
    this.listView = listView;
}
protected void onPreExecute(){

}
protected String doInBackground(String... connUrl){
    HttpURLConnection conn = null;
    BufferedReader reader;

    try{
        final URL url = new URL(connUrl[0]);
        conn = (HttpURLConnection) url.openConnection();
        conn.addRequestProperty("Content-Type", "application/json; 
charset=utf-8");
        conn.setRequestMethod("GET");
        int result = conn.getResponseCode();
        if(result == 200){

            InputStream in = new BufferedInputStream(conn.getInputStream());
            reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder sb = new StringBuilder();
            String line = null;

            while ((line = reader.readLine()) != null){
                status = line;
            }

        }

    }catch(Exception ex){

    }
    return status;
    }
    protected void onPostExecute(String result){
    super.onPostExecute(result);

    if(result != null){
        try{

            ArrayList<String> stringArrayList = new ArrayList<String>();
            JSONArray jsonArray = new JSONArray(result);
            for(int i=0; i<jsonArray.length(); i++){
                JSONObject object = jsonArray.getJSONObject(i);
                String ControlNumber = object.getString("ControlNumber");

                HashMap<String, String> itemList = new HashMap<String, 
String>();
                itemList.put("ControlNumber", ControlNumber);

                conNumList.add(itemList);


            }
            adapter = new SimpleAdapter(MainActivity.this, conNumList, 
R.layout.activity_main, new String[]{"ControlNumber"}, new int[]
{txtConNum});
             ((AdapterView<ListAdapter>) listView).setAdapter(adapter);


        }catch(Exception ex){
            ex.printStackTrace();
        }
    }else{
        Toast.makeText(MainActivity.this, "Failed to connect.", 
Toast.LENGTH_LONG).show();
    }
}
}





public void openWebpage(View view){
    Uri webLink = Uri.parse("http://examplesite.com");
    Intent webLinkIntent = new Intent(Intent.ACTION_VIEW, webLink);

    startActivity(webLinkIntent);
}


}

据我所知,我没有收到任何错误,只是它没有按照Wintech教程中的方式执行。我不知道我错过了什么。我故意忽略了POST位,因为我不打算在以后才写到数据库。现在,我只是尝试获取应用程序中的TextView中显示的查询结果。

Wintech的教程:

Android数据库连接(第1部分,创建WCF web服务)

https://youtu.be/xnO9MbferQo

Android数据库连接(第2部分,Get & Post数据)

https://youtu.be/RLDIjrDRrU8

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-08-09 20:04:58

您正在替换最后的读行输出,所以可能最后一行是\n字符。你应该换掉

代码语言:javascript
复制
while ((line = reader.readLine()) != null){
                status = line;
            }

使用

代码语言:javascript
复制
while ((line = reader.readLine()) != null){
                sb.append(line);
            }

..
return sb.toString()

编辑

代码语言:javascript
复制
protected String doInBackground(String... connUrl){
    HttpURLConnection conn = null;
    BufferedReader reader;
    StringBuilder sb;

    try{
        final URL url = new URL(connUrl[0]);
        conn = (HttpURLConnection) url.openConnection();
        conn.addRequestProperty("Content-Type", "application/json; 
charset=utf-8");
        conn.setRequestMethod("GET");
        int result = conn.getResponseCode();
        if(result == 200){

            InputStream in = new BufferedInputStream(conn.getInputStream());
            reader = new BufferedReader(new InputStreamReader(in));
            sb = new StringBuilder();
            String line = null;

            while ((line = reader.readLine()) != null){
                sb.append(line);
            }

        }

    }catch(Exception ex){

    }
    return sb.toString();
    }
    protected void onPostExecute(String result){
       super.onPostExecute(result);
       System.out.println(result);
    }
票数 2
EN

Stack Overflow用户

发布于 2017-08-10 12:24:03

我仔细地运行调试器,并看到它正在将查询中的数据存储到result中。我在TextView中添加了ListView,在AsyncTask中声明为txtV,在PostExecute中添加了txtV.setText(result)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45599602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档