11

का उपयोग कर एक बार सूची में आइटम को जोड़ने और दिखाने के लिए मैं एक कस्टम ListView में आइटम जोड़ने और मेरे एंड्रॉइड ऐप में परिणाम दिखाने के लिए एक ArrayAdapter का उपयोग कर रहा हूं। मेरी समस्या यह है कि ArrayAdapter दृश्य को दिखाने से पहले सभी आइटम इसमें आने तक प्रतीक्षा कर रहा है। ऐसा कहने के लिए, जब मैं ArrayAdapter में आइटम जोड़ रहा हूं और मैं सूचित करता हूं DataSetChanged, यह जोड़ा गया आइटम दिखाने के लिए ListView को अद्यतन नहीं करता है। यह तब तक प्रतीक्षा करता है जब तक कि सभी आइटम जोड़े नहीं जाते हैं और आइटम दिखाने से पहले GetView को कॉल किया जाता है।एंड्रॉइड - एक ArrayAdapter

मैं इसे सूची दृश्य में जोड़ने के तुरंत बाद आइटम को दिखाने के लिए क्या करना चाहता हूं। क्या यह संभव है?

मेरा मानना ​​है कि प्रासंगिक कोड निम्नलिखित है:

r_adapter = new ReminderAdapater(Activity_ContentSearch.this, R.layout.search_listitem, myList); 
listView.setAdapter(r_adapter); 
... 
r_adapter.notifyDataSetChanged(); 
r_adapter.clear(); 
for(int i = 0; i < myList.size(); i++) 
{ 
    r_adapter.add(myList.get(i)); 
    r_adapter.notifyDataSetChanged(); 
} 

आप देख सकते हैं, भले ही मैं जोड़ने विधि के बाद notifyDataSetChanged बोल रहा हूँ, यह वास्तव में दृश्य को अपडेट नहीं करता है। उपर्युक्त लूप को समाप्त करने के बाद दृश्य अंततः अपडेट किया गया है (जो मुझे पता है, उसके आधार पर, ऐसा इसलिए है क्योंकि कोड के इस खंड के बाद तक GetView को कॉल नहीं किया जाता है)।

मैंने भाग्य के साथ अपने कस्टम ऐरेएडाप्टर की ऐड विधि को ओवरराइड करने का प्रयास किया, क्योंकि मेरे पास उस विधि में दृश्य तक पहुंच नहीं है।

किसी भी मदद का स्वागत किया जाएगा :)

बारा

उत्तर

23

एंड्रॉयड के यूआई एकल पिरोया है। प्रत्येक बार जब आप एडाप्टर में प्रवेश जोड़ते हैं तो आप मुख्य एप्लिकेशन थ्रेड से एंड्रॉइड पर नियंत्रण नहीं दे रहे हैं। इसलिए, जब तक आप नियंत्रण वापस नहीं ले लेते हैं, तब तक एंड्रॉइड को आपकी प्रविष्टियों को प्रदर्शित करने का मौका नहीं मिलता है, और आप तब तक ऐसा नहीं कर रहे हैं जब तक कि आप अपने एडाप्टर को पूरी तरह से पॉप्युलेट नहीं कर लेते।

Here is an exampleAsyncTask के उपयोग को ArrayAdapter को पृष्ठभूमि थ्रेड के माध्यम से क्रमशः भरने के लिए दिखा रहा है।

/*** 
    Copyright (c) 2008-2012 CommonsWare, LLC 
    Licensed under the Apache License, Version 2.0 (the "License"); you may not 
    use this file except in compliance with the License. You may obtain a copy 
    of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required 
    by applicable law or agreed to in writing, software distributed under the 
    License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
    OF ANY KIND, either express or implied. See the License for the specific 
    language governing permissions and limitations under the License. 

    From _The Busy Coder's Guide to Android Development_ 
    http://commonsware.com/Android 
*/ 

package com.commonsware.android.async; 

import android.app.ListActivity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.SystemClock; 
import android.widget.ArrayAdapter; 
import android.widget.Toast; 
import java.util.ArrayList; 

public class AsyncDemo extends ListActivity { 
    private static final String[] items={"lorem", "ipsum", "dolor", 
             "sit", "amet", "consectetuer", 
             "adipiscing", "elit", "morbi", 
             "vel", "ligula", "vitae", 
             "arcu", "aliquet", "mollis", 
             "etiam", "vel", "erat", 
             "placerat", "ante", 
             "porttitor", "sodales", 
             "pellentesque", "augue", 
             "purus"}; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    setListAdapter(new ArrayAdapter<String>(this, 
         android.R.layout.simple_list_item_1, 
         new ArrayList<String>())); 

    new AddStringTask().execute(); 
    } 

    class AddStringTask extends AsyncTask<Void, String, Void> { 
    @Override 
    protected Void doInBackground(Void... unused) { 
     for (String item : items) { 
     publishProgress(item); 
     SystemClock.sleep(200); 
     } 

     return(null); 
    } 

    @SuppressWarnings("unchecked") 
    @Override 
    protected void onProgressUpdate(String... item) { 
     ((ArrayAdapter<String>)getListAdapter()).add(item[0]); 
    } 

    @Override 
    protected void onPostExecute(Void unused) { 
     Toast 
     .makeText(AsyncDemo.this, "Done!", Toast.LENGTH_SHORT) 
     .show(); 
    } 
    } 
} 
संबंधित मुद्दे