마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

리스트뷰가 어플리케이션 실행시 출력이 될 때가 있고 안될 때가 있습니다...

0 추천
public class MainActivity extends AppCompatActivity {
    ListView list;
    phpDown task;
    ArrayList<ListItem> listItem = new ArrayList<ListItem>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        task = new phpDown();
        ListAdapter adapter = new ListAdapter(getBaseContext(), R.layout.listitem, listItem);
        list = (ListView)findViewById(R.id.list);
        list.setAdapter(adapter);
    }

    private class ListAdapter extends ArrayAdapter<ListItem> {
        private ArrayList<ListItem> itemList;
        private Context context;
        private int rowResourceId;
        public ListAdapter(Context context, int textViewResourceId, ArrayList<ListItem> itemList){
            super(context, textViewResourceId, itemList);
            this.itemList = itemList;
            this.context = context;
            this.rowResourceId = textViewResourceId;
        }

        @NonNull
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            if(view == null){
                LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = vi.inflate(this.rowResourceId, null);
            }
            ListItem item = itemList.get(position);
            if(item != null){
                TextView text_Singer = (TextView)view.findViewById(R.id.Singer);
                text_Singer.setText("Singer: " + item.getData(1));
                TextView text_Song = (TextView)view.findViewById(R.id.Song);
                text_Song.setText("Song: " + item.getData(2));
                //TextView text_AgeGroup = (TextView)view.findViewById(R.id.AgeGroup);
                //text_AgeGroup.setText("AgeGroup: " + item.getData(3));
                TextView text_Genre = (TextView)view.findViewById(R.id.Genre);
                text_Genre.setText("Genre: " + item.getData(4));
                /*TextView text_Gender = (TextView)view.findViewById(R.id.Gender);
                text_Gender.setText("Gender: " + item.getData(5));
                TextView text_Emotion = (TextView)view.findViewById(R.id.Emotion);
                text_Emotion.setText("Emotion: " + item.getData(6));*/
                TextView text_Album = (TextView)view.findViewById(R.id.Album);
                text_Album.setText("Album: " + item.getData(7));
                TextView text_Track = (TextView)view.findViewById(R.id.Track);
                text_Track.setText("Track: " + item.getData(8));
                ImageView img = (ImageView)view.findViewById(R.id.image);
                Glide.with(img.getContext()).load(item.getData(9)).into(img);
            }
            return view;
        }
    }

    private class phpDown extends AsyncTask<String, Integer, String> {

.........

void onPostExecute(String str) {

.......
            try {
                JSONObject root = new JSONObject(str);
                JSONArray ja = root.getJSONArray("results");
                for (int i = 0; i < ja.length(); i++) {
                    JSONObject jo = ja.getJSONObject(i);
                    id = jo.getString("id");
                    Singer = jo.getString("Singer");
                    Song = jo.getString("Song");
                    AgeGroup = jo.getString("AgeGroup");
                    Genre = jo.getString("Genre");
                    Gender = jo.getString("Gender");
                    Emotion = jo.getString("Emotion");
                    Album = jo.getString("Album");
                    Track = jo.getString("Track");
                    Image = jo.getString("ImageUrl");
                    listItem.add(new ListItem(id, Singer, Song, AgeGroup, Genre, Gender, Emotion, Album, Track, Image));

 

mysql로 데이터베이스를 만들고 php Json 형식으로 파싱받아와서 데이터를 어레이리스트에 저장한 후

리스트뷰로 출력하고 있습니다

이 과정에서 어플리케이션을 실행했을 때 어쩔 때는 제데로 화면에 띄워지고 어쩔 때는 아예 출력이 안되고 공백만 나옵니다..

제대로 출력이 되는 경우가 있는걸 보면 코드문제만은 아닌거 같기도한데..혹시 메모리가 남아있다거나 초기화가 안된다거나 그런 경우일까요?? 아예 출력이 안되면 코드를 다시짜겠는데 되다안되다해서 찝찝하네요 너무....
boolks (240 포인트) 님이 2016년 10월 14일 질문
asynctask에서 데이터를 처리하고 나신다음adapter.notifyDataSetChanged() 호출해 보세요.
아 notifyDataSetChanged()라는 새로운걸 또 배워가네요 감사드립니다!

1개의 답변

+1 추천
phpDown 이 async task 인데 그러면 json 을 다운로드 하기 전에 list view 의 adapter 에서 view 에 데이터를 넣는 불상사가 생기게 끔 코딩이 되어 있네요

async task 에 대해서 좀더 파악해보시길 바랍니다.

일단 async task 의 onPostExecute 의 마지막 부분에 adapter 를 set 하시면 원하시는 문제는 해결될 것으로 보입니다만은

adapter 자체 구현도 잘못되어 있네요 view holder 를 검색하고 적용하시기 바랍니다.
aucd29 (218,390 포인트) 님이 2016년 10월 14일 답변
아 초보적인 실수를 해버렸네요...
그리고 뷰홀더에 대해선 많이 배웠습니다 책에는 나오지 않았던 내용인데 좋은 정보 주셔서 감사합니다!
...