2016-12-24 5 views
0

SQL DB가 있는데 listView에 쿼리 된 값을 표시하려고합니다. 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); 
 
    } 
 

 
}

답변

1

통화 itemsAdapter.notifyDataSetChanged() 01 이내.

AsyncTask가 끝날 때까지 목록이 비어 있습니다.

당신은 당신의 코드 당신이 doInBackground의 목록을 업데이트하고 있지만 변경 사항에 대한 어댑터를 통지하지 않습니다 볼 수 마찬가지로 어댑터에게 활동 클래스

+0

감사 니펫 일했다 –

0

의 멤버 변수를 확인해야합니다. 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); 
 

 
      } 
 

 
     } 
 
    } 
 
} 
 
`

관련 문제