如何使用android从mysql数据库加载数据

时间:2017-02-09 23:51:19

标签: android mysql json

我想在我的Android应用程序中从mysql在线数据库加载数据 我的数据库内容是:id,其中包含int(70)类型 具有文本类型的名称和具有文本类型的图片包含图像路径的图片 3字段有3行数据 android代码是

public class ByCategoryMenu extends Activity {
    RequestQueue requestQueue;
    String url = "http://giclub.esy.es/show.php";
    TextView textView;
    ListView listView;
    ArrayList<listitem> listitmes = new ArrayList<listitem>();
    String encodimg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_single);
        textView = (TextView) findViewById(R.id.textView);
        listView = (ListView) findViewById(R.id.listView);
        imgV = (ImageView) findViewById(R.id.imgV);
        EName = (EditText) findViewById(R.id.ETXT_Name);
        requestQueue = Volley.newRequestQueue(this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject respons = jsonArray.getJSONObject(i);
                                String id = respons.getString("id");
                                String name = respons.getString("name");
                                String pic = respons.getString("pic");
                                listitmes.add(new listitem(id, name, pic));
                                listAllItme();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("VOLLEY", "ERROR");
            }
        }
        );
        requestQueue.add(jsonObjectRequest);
    }
    public void listAllItme() {
        listAdpter lA = new listAdpter(listitmes);
        listView.setAdapter(lA);
    }
    class listAdpter extends BaseAdapter {
        ArrayList<listitem> listA = new ArrayList<listitem>();
        public listAdpter(ArrayList<listitem> listA) {
            this.listA = listA;
        }
        @Override
        public int getCount() {
            return listA.size();
        }
        @Override
        public Object getItem(int position) {
            return listA.get(position).id;
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater layoutInflater = getLayoutInflater();
            View view = layoutInflater.inflate(R.layout.list, null);
            TextView id = (TextView) view.findViewById(R.id.textView_id);
            TextView name = (TextView) view.findViewById(R.id.textView_name);
            ImageView pic = (ImageView) view.findViewById(R.id.imageView2);
            id.setText(listA.get(position).id);
            name.setText(listA.get(position).name);
            Picasso.with(ByCategoryMenu.this).load("http://giclub.esy.es/image/" + listA.get(position).pic).into(pic);
            return view;
        }
    }

list.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"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/textView_id"
        android:textColor="#fa0000" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView_name"
        android:textColor="@color/accent_material_light" />

    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:id="@+id/imageView2" />
</LinearLayout>

列出班级

public class listitem {
    public String id;
    public String name;
    public String pic;
    public listitem(String id, String name, String pic) {
        this.id = id;
        this.name = name;
        this.pic = pic;
    }
}

listview xml页面(grid_single)

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

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

    </LinearLayout>

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView" />

</LinearLayout>

我得到了list.xml页面,没有改变,哪里出错了?

0 个答案:

没有答案