Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将JSON从SQL填充到Android Listview_Php_Android_Sql_Json_Listview - Fatal编程技术网

Php 将JSON从SQL填充到Android Listview

Php 将JSON从SQL填充到Android Listview,php,android,sql,json,listview,Php,Android,Sql,Json,Listview,为此,我想用使用PHP从phpmyadmin提取的数据填充我的ListView。 非常相似 谁能给我指一下正确的方向吗?整个周末都有这个问题 社交搜索类 public class SocietySearch extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

为此,我想用使用PHP从phpmyadmin提取的数据填充我的ListView。 非常相似

谁能给我指一下正确的方向吗?整个周末都有这个问题

社交搜索类

public class SocietySearch extends AppCompatActivity {

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



        Society society = new Society(-1, null, null, null);
        ServerRequests serverRequest1 = new ServerRequests(SocietySearch.this);
        serverRequest1.GetSocietyDataAsyncTask(society, new GetSocietyCallback() {
            @Override
            public void done(final Society returnedSociety) {
               // ServerRequests.rowCount; // TODO this produces an error getrowcount

                ListView lv = (ListView) findViewById(R.id.ListView);
                List<ListViewItem> items = new ArrayList<>();
                for (int i=0; i <10; i++){
                items.add(new ListViewItem() {{
                    ThumbnailResource = R.drawable.test;
                    Title = returnedSociety.socName;
                    Subtitle = returnedSociety.socDes;
                }});
                CustomListViewAdapter adapter = new CustomListViewAdapter(SocietySearch.this, items);
                lv.setAdapter(adapter);}
            }
        });
    }

    class ListViewItem {
        public int ThumbnailResource;
        public String Title;
        public String Subtitle;
    }
}
item_row.xml(用于ListView的xml)


GetSocietyData PHP代码:

public class CustomListViewAdapter extends BaseAdapter {

    LayoutInflater inflater;
    List<SocietySearch.ListViewItem> items;

    public CustomListViewAdapter(Activity context, List<SocietySearch.ListViewItem> items) {
        super();

        this.items = items;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        //Auto-generated method stub
        return items.size(); // TODO Maybe this can be my sql count?
    }

    @Override
    public Object getItem(int position) {
        //Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        //Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        //Auto-generated method stub

        ListViewItem item = items.get(position);

        View vi = convertView;

        if (convertView == null)
            vi = inflater.inflate(R.layout.item_row, null);

        ImageView test = (ImageView) vi.findViewById(R.id.imgThumbnail);
        TextView txtTitle = (TextView) vi.findViewById(R.id.txtTitle);
        TextView txtSubTitle = (TextView) vi.findViewById(R.id.txtSubTitle);

        test.setImageResource(item.ThumbnailResource);
        txtTitle.setText(item.Title);
        txtSubTitle.setText(item.Subtitle);


        return vi;
    }
}
public class ServerRequests {

    ProgressDialog progressDialog;
    public static final int CONNECTION_TIMEOUT = 1000 * 15;
    public static final String SERVER_ADDRESS = "http://10.0.2.2:80/";//Connects to the emulator

    public ServerRequests(Context context) {
        progressDialog = new ProgressDialog(context);
        progressDialog.setCancelable(false);
        progressDialog.setTitle("Processing");
        progressDialog.setMessage("Please wait...");
    }

    public void GetSocietyDataAsyncTask(Society society, GetSocietyCallback societyCallBack) {
        progressDialog.show();
        new getSocietyDataAsyncTask(society, societyCallBack).execute();
    }


    public class getSocietyDataAsyncTask extends AsyncTask<Void, Void, Society> {
        Society society;
        GetSocietyCallback societyCallback;

        public getSocietyDataAsyncTask(Society society, GetSocietyCallback societyCallback) {
            this.society = society;
            this.societyCallback = societyCallback;
        }

        @Override
        protected Society doInBackground(Void... params) {
            BufferedReader reader = null;
            Society returnedSociety = null;
            try {
                URL url = new URL(SERVER_ADDRESS + "/getsocietydata.php");

                HttpURLConnection con = (HttpURLConnection) url.openConnection();

                con.setConnectTimeout(CONNECTION_TIMEOUT);
                con.setReadTimeout(CONNECTION_TIMEOUT);

                con.setRequestMethod("POST");

                con.setDoOutput(true);

                StringBuilder sb = new StringBuilder();
                reader = new BufferedReader(new InputStreamReader(con.getInputStream()));

                String line;
                while ((line = reader.readLine()) != null) { //Read until there is something available
                    sb.append(line + "\n");     //Read and save line by line
                }
                line = sb.toString();           //Saving complete data received in string

                //Check values received in Logcat
                Log.i("custom_check", "The values received in the store part are as follows:");
                Log.i("custom_check", line);


                JSONObject jObject = new JSONObject(line);
                if (jObject.length() == 0) {
                    returnedSociety = null;
                } else {
                    //Storing each Json in a variable
                    int society_id = jObject.getInt("society_id");
                    String socName = jObject.getString("name");
                    String socEmail = jObject.getString("email");
                    String socDes = jObject.getString("description");


                    returnedSociety = new Society(society_id, socName, socEmail, socDes);
                    System.out.println(returnedSociety);
                    return returnedSociety;
                }


            } catch (Exception e) {
                e.printStackTrace();
                Log.e("Buffer Error", "Error converting result " + e.toString());

            }


            finally {

                if (reader != null) {
                    try {
                        reader.close();     //Closing the
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
            return returnedSociety;
        }

        @Override
        protected void onPostExecute(Society returnedSociety) {
            super.onPostExecute(returnedSociety);
            progressDialog.dismiss();
            societyCallback.done(returnedSociety);
        }
    }
public class Society {

    String socName, socEmail, socDes;
    int society_id;

    public Society(int society_id, String socName, String socEmail, String socDes) {

        this.society_id = society_id;
        this.socName = socName;
        this.socEmail = socEmail;
        this.socDes = socDes;

    }

    public int getSociety_id(){
        return this.society_id;
    }

    public String getSocName(){
        return this.socName;
}
    public String getSocEmail(){
        return this.socEmail;
    }
    public String getSocDes(){
        return this.socDes;
    }
}
<?php

        $user = 'root';
        $pass = '';
        $db = 'uopuser';

        $con=mysqli_connect('localhost', $user, $pass, $db) or die('Unable to connect');


        $statement = mysqli_prepare($con, 'SELECT * FROM society');
        mysqli_stmt_execute($statement);

        mysqli_stmt_store_result($statement);
        mysqli_stmt_bind_result($statement, $society_id, $name, $email, $description);

    $society = array();
    $key = 0;
    while(mysqli_stmt_fetch($statement))
    { 
        $society[$key]['society_id'] = $society_id; 
        $society[$key]['name'] = $name;
        $society[$key]['email'] = $email;
        $society[$key]['description'] = $description; 
        $key++;
    }
    echo json_encode($society); 

        mysqli_stmt_close($statement);

        mysqli_close($con);
    ?>

因此,根据与kmil的讨论,我们发现有两个主要问题。首先是这个循环所在的代码部分:

ListView lv = (ListView) findViewById(R.id.ListView);
                List<ListViewItem> items = new ArrayList<>();
                for (int i=0; i <10; i++){
                items.add(new ListViewItem() {{
                    ThumbnailResource = R.drawable.test;
                    Title = returnedSociety.socName;
                    Subtitle = returnedSociety.socDes;
                }});
                CustomListViewAdapter adapter = new CustomListViewAdapter(SocietySearch.this, items);
                lv.setAdapter(adapter);}
            }
ListView lv=(ListView)findviewbyd(R.id.ListView);
列表项=新建ArrayList();

对于(int i=0;i您在哪里遇到问题?从phpMyAdmin检索数据或填充listview?另外,您可以为自定义适配器添加代码吗?抱歉,我以为我已经添加了它!我在使用从服务器提取的社团数组填充listview时遇到问题!不用担心。因此您已经在提取数组了社会成功的例子?就人口而言。好吧,我来看看。是的,我已经成功地把它们拉出来了:我的代码有点混乱,因为我是android编程新手,这将是我的第一个应用程序。它开始让我不知所措
ListView lv = (ListView) findViewById(R.id.ListView);
                List<ListViewItem> items = new ArrayList<>();
                for (int i=0; i <10; i++){
                items.add(new ListViewItem() {{
                    ThumbnailResource = R.drawable.test;
                    Title = returnedSociety.socName;
                    Subtitle = returnedSociety.socDes;
                }});
                CustomListViewAdapter adapter = new CustomListViewAdapter(SocietySearch.this, items);
                lv.setAdapter(adapter);}
            }