마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

mysql의 데이터를 listview로 띄우고싶은데 결과창엔 아무것도 안뜹니다.

0 추천

MainActivity.class 코드

public class MainActivity extends Activity {

    //서버에서 받을 정보를 담을 변수 선언
    String myJSON;

    private static final String TAG_RESULTS = "result";
    private static final String UserName = "name";
    private static final String UserMajor = "major";
    private static final String UserContry = "contry";

    JSONArray peoples = null;
//서버 정보를 담을 배열
    ArrayList<HashMap<String, String>> personList;

    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_main);
        list = (ListView) findViewById(R.id.listview);
        personList = new ArrayList<HashMap<String, String>>();
        getDbData("제 서버 IP");
    }

    public void getDbData(String url) {
        class GetDataJSON extends AsyncTask<String, Void, String> {

            @Override
            protected String doInBackground(String... params) {

                String uri = params[0];
                BufferedReader bufferedReader = null;

                try {
                    URL url = new URL(uri);
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    StringBuilder sb = new StringBuilder();

                    if (con != null) { //연결되어있으면
                        con.setConnectTimeout(10000);
                        con.setUseCaches(false);
                        if (con.getResponseCode() == HttpURLConnection.HTTP_OK) { //연결코드가 리턴되면
                            bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                            String json;
                            while ((json = bufferedReader.readLine()) != null) {
                                sb.append(json + "\n");
                            }
                        }
                    }
                    return sb.toString().trim();
                } catch(Exception e) {
                    return new String("Exception: " + e.getMessage());
                }
            }

            @Override
            protected void onPostExecute(String result) {
                myJSON = result;
                showList();
            }
        }

        GetDataJSON g = new GetDataJSON();
        g.execute(url);
    }

    protected void showList() {
        try {
            JSONObject jsonObj = new JSONObject(myJSON);
            peoples = jsonObj.getJSONArray(TAG_RESULTS);

            for(int i = 0; i < peoples.length(); i++) {
                JSONObject c = peoples.getJSONObject(i);
                String name = c.getString(UserName);
                String major = c.getString(UserMajor);
                String contry = c.getString(UserContry);

                HashMap<String, String> persons = new HashMap<String, String>();

                persons.put(UserName, name);
                persons.put(UserMajor, major);
                persons.put(UserContry, contry);

                personList.add(persons);
            }
            //adapter 생성
            ListAdapter adapter = new SimpleAdapter(

                    MainActivity.this, personList, R.layout.listview_item,
                    new String[]{UserName, UserMajor, UserContry},
                    new int[]{R.id.name, R.id.major, R.id.contry}
            );
            list.setAdapter(adapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:id="@+id/imageview"/>

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/name"
            android:paddingBottom="2dip"
            android:paddingTop="6dip"
            android:textStyle="bold"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/major"
            android:textSize="20sp"
            android:paddingBottom="2dip"
            android:textStyle="bold" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/contry"
            android:textSize="18sp"
            android:gravity="left"
            android:textStyle="bold" />

    </LinearLayout>

</LinearLayout>

위에는 listview_item.xml

아래는 listview_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.chulhyeonkim.listviewtest1.MainActivity"
    android:orientation="vertical">

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

</LinearLayout>

 

이 코드들의 결과창은 아무런 데이터도 없는 흰색 바탕의 어플입니다.

구글링 해서 여러 예제도 찾아보고 해본건데 잘안됩니다 ㅠㅠ

안드로이드 스튜디오에서 작업하고있습니다

dciv (120 포인트) 님이 2017년 11월 28일 질문
일단 Log를 통해 MySQL에서 제대로 받아왔는지 확인을 하셨나요?

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...