【问题标题】:listview not showing items androidlistview不显示项目android
【发布时间】:2017-05-09 15:15:04
【问题描述】:

我有 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);
    }

}

【问题讨论】:

    标签: android json listview arraylist


    【解决方案1】:

    在onPostExecute 内致电itemsAdapter.notifyDataSetChanged()。

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        检查此代码 sn-p 中的 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);
        
                    }
        
                }
            }
        }
        `

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-06-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-13
          相关资源
          最近更新 更多