listview没有显示android项目

时间:2016-12-24 01:19:15

标签: android json listview arraylist

我有sql DB,我试图在listView中显示查询的值。我为listView创建了一个自定义适配器。问题是 我无法在listView上看到任何数据。

主要代码

public class _songs_playlist extends AppCompatActivity {
    ArrayList<songsarray> listofsoongs = new ArrayList<songsarray>();
    private static int SPLASH_TIME_OUT=2000;
    AlertDialog alertDialog;
    private boolean loggedIn = false;
    String type;
    String result;

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

        new JSONParse().execute();
        MyCustomAdapterSongs itemsAdapter = new MyCustomAdapterSongs(this, listofsoongs);

        ListView listView = (ListView) findViewById(R.id.listView1);
        listView.setAdapter(itemsAdapter);
        itemsAdapter.notifyDataSetChanged();
    }

    private class JSONParse extends AsyncTask<String, String, JSONObject> {
        private ProgressDialog pDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            alertDialog=new AlertDialog.Builder(_songs_playlist.this).create();
            alertDialog.setTitle("Upadting Data");
            pDialog = new ProgressDialog(_songs_playlist.this);
            pDialog.setMessage("Getting Data ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();

        }


        @Override
        protected JSONObject doInBackground(String... args) {


                HTTPHandler sh = new HTTPHandler();
                String login_URL = "http://2f179dfb.ngrok.io/getsong.php";
                try {

                    //Fetching the boolean value form sharedpreferences


                    URL url = new URL(login_URL);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setDoInput(true);

                    InputStream inputStream = httpURLConnection.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
                    result = "";
                    String line = "";
                    while ((line = bufferedReader.readLine()) != null) {
                        result += line;
                    }
                    bufferedReader.close();
                    inputStream.close();
                    httpURLConnection.disconnect();
                    Log.e("RESULT", result);
                    JSONObject jsonObject = new JSONObject(result);

                    JSONArray result1 = jsonObject.getJSONArray("result");
                    for (int i = 0; i < result1.length(); i++) {
                        JSONObject c = result1.getJSONObject(i);
                        String id = c.getString("songID");
                        String names = c.getString("songName");
                        String ss = c.getString("singerName");
                        listofsoongs.add(new songsarray(ss,id,names));


                    }

                    return jsonObject;
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (ProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return null;




        }

        @Override
        protected void onPostExecute(JSONObject json) {
            pDialog.dismiss();
            if(true)

            {

            }
            else
            {

            }

        }
    }
}

自定义数组适配器的代码

public class MyCustomAdapterSongs extends ArrayAdapter<songsarray> {

    Context context;
    ArrayList<songsarray> items;
    public MyCustomAdapterSongs(Activity context, ArrayList<songsarray> songsarrays) {
      
        super(context, 0, songsarrays);
        this.context=context;
        this.items=songsarrays;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

      
        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.itemlist, parent, false);

        }

   
        TextView nameTextView = (TextView) listItemView.findViewById(R.id.textView1);

        
        nameTextView.setText(currentAndroidFlavor.getSingername());
        Log.e("hhhh", nameTextView.getText().toString());

      
        TextView numberTextView = (TextView) listItemView.findViewById(R.id.textView2);

        
        numberTextView.setText(currentAndroidFlavor.getSongname());
        Log.e("jjjj", numberTextView.getText().toString());



        CheckBox ch=(CheckBox)listItemView.findViewById(R.id.checkBox1);

       ch.setSelected(currentAndroidFlavor.isSelected());
       
        return listItemView;
    }

    @Override
    public int getCount() {
        if(items == null)
            return 0;
        return items.size();
    }

    @Override
    public songsarray getItem(int i) {
        return items.get(i);
    }

}

3 个答案:

答案 0 :(得分:1)

itemsAdapter.notifyDataSetChanged()内致电onPostExecute

在AsyncTask完成之前,您的列表是空的。

您必须使适配器成为Activity类的成员变量

答案 1 :(得分:0)

在代码中,我可以看到您正在更新HttpOp.setDefaultHttpClient(HttpClientBuilder.create().build()); 中的列表,但您没有通知适配器进行更改。 在onPostExecute方法中,您需要调用doInBackground并确保您没有在itemsAdapter.notifyDataSetChanged()内调用它,因为在后台线程中您无法进行与UI相关的工作。

答案 2 :(得分:0)

检查此代码段中的EDITS

`

public class _songs_playlist extends AppCompatActivity {
    ArrayList<songsarray> listofsoongs = new ArrayList<songsarray>();
    private static int SPLASH_TIME_OUT=2000;
    AlertDialog alertDialog;
    private boolean loggedIn = false;
    String type;
    String result;

//EDIT 1: Make these member variables
    MyCustomAdapterSongs itemsAdapter;
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__songs_playlist);
        
        //EDIT 2: get references your views before AsyncTask
        listView = (ListView) findViewById(R.id.listView1);
        new JSONParse().execute();
        


    }

    private class JSONParse extends AsyncTask<String, String, JSONObject> {
        private ProgressDialog pDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            alertDialog=new AlertDialog.Builder(_songs_playlist.this).create();
            alertDialog.setTitle("Upadting Data");
            pDialog = new ProgressDialog(_songs_playlist.this);
            pDialog.setMessage("Getting Data ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();

        }


        @Override
        protected JSONObject doInBackground(String... args) {


                HTTPHandler sh = new HTTPHandler();
                String login_URL = "http://2f179dfb.ngrok.io/getsong.php";
                try {

                    //Fetching the boolean value form sharedpreferences


                    URL url = new URL(login_URL);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setDoInput(true);

                    InputStream inputStream = httpURLConnection.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
                    result = "";
                    String line = "";
                    while ((line = bufferedReader.readLine()) != null) {
                        result += line;
                    }
                    bufferedReader.close();
                    inputStream.close();
                    httpURLConnection.disconnect();
                    Log.e("RESULT", result);
                    JSONObject jsonObject = new JSONObject(result);

                    JSONArray result1 = jsonObject.getJSONArray("result");
                    for (int i = 0; i < result1.length(); i++) {
                        JSONObject c = result1.getJSONObject(i);
                        String id = c.getString("songID");
                        String names = c.getString("songName");
                        String ss = c.getString("singerName");
                        listofsoongs.add(new songsarray(ss,id,names));


                    }

                    return jsonObject;
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (ProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return null;




        }

        @Override
        protected void onPostExecute(JSONObject json) {
            pDialog.dismiss();
//EDIT 3: set your adapter here as this method will be called on the UI Thread
            itemsAdapter = new MyCustomAdapterSongs(this, listofsoongs);
            listView.setAdapter(itemsAdapter);

            }

        }
    }
}
`