2016-12-24 11 views
0

私はsql DBを持っており、照会された値をlistViewに表示しようとしています。私はlistViewのカスタムアダプタを作成しました。問題は 私のlistViewに表示されたデータを見ることができません。リストビューにアイテムが表示されていません

01内の主

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()のコード。

あなたのリストは、AsyncTaskがそこで終わるまで空です。

あなたはあなたのコードのように私はあなたがdoInBackgroundでリストを更新しているが、あなたは、変更のためのアダプターに通知されていません見ることができ、アダプタにActivityクラス

+0

にそれが働いたおかげで、チェック –

0

のメンバ変数を作成する必要があります。 onPostExecuteメソッドでは、itemsAdapter.notifyDataSetChanged()を呼び出して、doInBackgroundの中でそれをバックグラウンドスレッドのように呼び出さないようにする必要があります。これは、UI関連の作業には使用できません。

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

 
      } 
 

 
     } 
 
    } 
 
} 
 
`

関連する問題