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

AsyncTask 재사용 질문이요

0 추천

listview를 만들고있는데 10개 데이터가 먼저나오고 더보기를 클릭하면 10개가 더나오는식으로 만들려고 합니다

그래서 클릭 리스너에서 다시 실행시키니까

java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)

이렇게 한번밖에 사용이 안된다고 나오더라구요 ㅠㅠ

뭘써야 가능할까요??

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.stunote, container, false);

        stuListView = (ListView) view.findViewById(R.id.stunotepad_listview1);

        View footer = inflater.inflate(R.layout.listview_footer,null,false);

        stuListView.addFooterView(footer) ;

        adapter =  new ListViewAdapter();
        stuListView.setAdapter(adapter);

        task = new StunoteJsonReadTask(adapter);



        try {

            String resultTask = task.execute(url).get();
            Log.e("msg : ",resultTask);
        } catch (InterruptedException e) {
            e.printStackTrace();
            Log.e("msg : ","error");
        } catch (ExecutionException e) {
            e.printStackTrace();
            Log.e("msg : ","error");
        }
 Button addButton = (Button) footer.findViewById(R.id.addItem);
        addButton.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                try {
                    String resultTask = task.execute(url).get();
                    Log.e("msg : ",resultTask);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    Log.e("msg : ","error");
                } catch (ExecutionException e) {
                    e.printStackTrace();
                    Log.e("msg : ","error");
                }
            }
        });
public class StunoteJsonReadTask extends AsyncTask<String,Void,String> {
    String errorString = null;

    int index;


    ListViewAdapter adapter;

    StunoteJsonReadTask(ListViewAdapter adapter){
        index = 10;
        this.adapter = adapter;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }


    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        try{
            JSONObject jsonResponse = new JSONObject(result);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("major_document");

            for(int i = index-10;i<index;i++)
            {
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                String document_srl = jsonChildNode.optString("document_srl");
                String title = jsonChildNode.optString("title");
                String content = jsonChildNode.optString("content");
                String regDate = jsonChildNode.optString("regdate");
                String readed_count = jsonChildNode.optString("readed_count");
                String commentCount = jsonChildNode.optString("comment_count");
                String nick_name = jsonChildNode.optString("nick_name");

                regDate = regDate.substring(0,4) + '-' + regDate.substring(4,6) + '-' + regDate.substring(6,8);

                adapter.addItem(document_srl,title,content,regDate,readed_count,commentCount,nick_name);
            }

            index += 10;
            adapter.notifyDataSetChanged();
        }catch(JSONException e){
            Log.e("msg : ","JSON 오류");
        }

 

뱅이 (620 포인트) 님이 2017년 9월 25일 질문

1개의 답변

+1 추천
 
채택된 답변
AsyncTask는 재사용할 수 없으므로, 요청할 때마다 새로 만들어서 사용하면 됩니다.
익명사용자 님이 2017년 9월 25일 답변
뱅이님이 2017년 10월 31일 채택됨
...