2016-12-24 86 views
0

我有SQL DB,我试图在列表视图中显示查询的值。我为listView创建了一个自定义适配器。问题是 我无法看到我的listView上显示的任何数据。的主listview not displayed items android

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

 
}

回答

1

呼叫itemsAdapter.notifyDataSetChanged() 01内。

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

你必须使适配器Activity类

+0

感谢,它的工作 –

0

的成员变量在你的代码中,我可以看到你正在更新的列表中doInBackground但你不通知的更改适配器。 在onPostExecute方法中,您需要调用itemsAdapter.notifyDataSetChanged()并确保它不在doInBackground之内调用它,因为在后台线程中您不能与UI相关的工作。

0

入住此代码的编辑片断

` 
 

 
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); 
 

 
      } 
 

 
     } 
 
    } 
 
} 
 
`