0

私は曲リストアプリケーションを構築しています。データ読み込み中にスプラッシュ画面を表示したい。そのためにAsyncTask、ViewSwitcher と組み合わせて、スプラッシュ画面 (ロゴと円のプログレスバーのみ) とメイン画面の間で xml レイアウトを切り替えるように設定しました。問題は、ListView にデータを配置することになると、別のクラスにある BaseAdapter を使用していて、エラーがスローされること"The Constructor LazyAdapter(Home.LoadViewTask, ArrayList<HashMap<String,String>>) is undefined"です。

これは、AsyncTask を持つホーム クラスのソースです。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.main);

    mBtnNaslovnica = (Button) findViewById(R.id.mBtnNaslovnica);
    mBtnNaslovnica.setSelected(true);

    new LoadViewTask().execute();
}

//To use the AsyncTask, it must be subclassed
public class LoadViewTask extends AsyncTask<Void, Integer, Void>
{
    //A TextView object and a ProgressBar object
    private TextView tv_progress;
    private ProgressBar pb_progressBar;

    //Before running code in the separate thread
    @Override
    protected void onPreExecute() 
    {
        //Initialize the ViewSwitcher object
        viewSwitcher = new ViewSwitcher(Home.this);
        /* Initialize the loading screen with data from the 'loadingscreen.xml' layout xml file. 
         * Add the initialized View to the viewSwitcher.*/
        viewSwitcher.addView(ViewSwitcher.inflate(Home.this, R.layout.init, null));

        //Set ViewSwitcher instance as the current View.
        setContentView(viewSwitcher);
    }

    //The code to be executed in a background thread.
    @Override
    protected Void doInBackground(Void... params) 
    {
        ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_SONG);
        // looping through all song nodes <song>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
            map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
            map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
            map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

            // adding HashList to ArrayList
            songsList.add(map);
        }

        list=(ListView)findViewById(R.id.list);

        // Getting adapter by passing xml data ArrayList
        adapter=new LazyAdapter(this, songsList);        
        list.setAdapter(adapter);
        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {    

            }
        });

        return null;
    }

    //After executing the code in the thread
    @Override
    protected void onPostExecute(Void result) 
    {
        /* Initialize the application's main interface from the 'main.xml' layout xml file. 
         * Add the initialized View to the viewSwitcher.*/
        viewSwitcher.addView(ViewSwitcher.inflate(Home.this, R.layout.main, null));
        //Switch the Views
        viewSwitcher.showNext();
    }
}

//Override the default back key behavior
@Override
public void onBackPressed() 
{
    //Emulate the progressDialog.setCancelable(false) behavior
    //If the first view is being shown
    if(viewSwitcher.getDisplayedChild() == 0)
    {
        //Do nothing
        return;
    }
    else
    {
        //Finishes the current Activity
        super.onBackPressed();
    }
}

そして、これはLazyAdapterクラスのソースです:

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.list_row, null);

    TextView title = (TextView)vi.findViewById(R.id.title); // title
    TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
    TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image

    HashMap<String, String> song = new HashMap<String, String>();
    song = data.get(position);

    // Setting all values in listview
    title.setText(song.get(Home.KEY_TITLE));
    artist.setText(song.get(Home.KEY_ARTIST));
    duration.setText(song.get(Home.KEY_DURATION));
    imageLoader.DisplayImage(song.get(Home.KEY_THUMB_URL), thumb_image);
    return vi;
}

`

4

4 に答える 4

1

別のスレッドであるため、ラベルテキストの設定、AsyncTask.doInBackground 内のリストの変更など、UI の変更を行うことはできません...

  list=(ListView)findViewById(R.id.list);


    // Getting adapter by passing xml data ArrayList
    adapter=new LazyAdapter(this, songsList);        
    list.setAdapter(adapter);
    // Click event for single list row
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {


        }
    });

呼び出す前にコードのその部分を配置する必要があります

new LoadViewTask().execute();

そして doInBackground() の中に... 最後の行として.. この行を入れてください.. adapter.notifyDataSetChanged();

于 2012-08-21T15:31:05.167 に答える
0

Activity問題は、何らかの理由で、コンストラクターの最初のパラメーターとしてを必要とすることです。アダプターのコンストラクターに渡すことで、アクティビティではなくコンテキストを持たないthisタイプのオブジェクトを渡します。AsyncTaskしたがってActivity、オーバーライドされたコンストラクターの引数としてan を渡し、LoadViewTaskそれをコンストラクターの正しい引数として使用する必要があります。AsyncTaskそれでも、予期しない動作につながる可能性があるため、コンテキストを にバインドするときは注意してください (更新しようとするとActivityが存在しなくなる可能性があります)。多くの同期関連の欠陥があるため、使用するのは非常に危険なクラスです。AsyncTaskAsyncTask

于 2013-07-15T08:37:45.330 に答える