1

目前我正试图让我的列表视图在异步任务中更新。获取数据不是问题,问题在于更新主线程的ui。我正在使用网络服务方法来获取数据,以防万一你想知道。

我试过了 adapter.notifyDataSetChanged(); , listView.refreshDrawableState(); & listView.requestLayout(); 单独和一起都没有奏效。

我还在我的 asynctask 的doingInbackground 和 onPostExecute 中尝试了 runOnUiThread 函数,但它们都不起作用。

我已经通过论坛并尝试了几乎所有的东西,但不知何故我仍然遇到问题,而其他人已经在解决方案方面取得了进展。

最初,第一项显示在我从 xml 数据存储中检索到的数据中,但所有其他项目均未显示。但是,如果我将方向更改为横向,则最后两行不会立即显示在视图中,如果向下滚动到列表中的最后一项,则值在那里,然后不会显示最上面的两行。当您向上滚动时,现在会显示列表中的第二行。

这是应用程序最初的外观:

http://img525.imageshack.us/img525/1035/androidimage.png

这是我的名为 NewListView 的主线程的一些片段。

package com.nico.gg;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class NewListView extends Activity {
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    ListView listView;
    List<RowItem> rowItems;
    ProgressDialog PostingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        PostingDialog = new ProgressDialog(this);
        PostingDialog.setTitle("Updating Rates");
        PostingDialog.setMessage("Retrieving Rates, Please wait...");
        PostingDialog.setIndeterminate(true);
        PostingDialog.setCancelable(true);

        rowItems = new ArrayList<RowItem>();
        listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(new CustomListViewAdapter(this, R.layout.list_row,
                rowItems));
        new DownloadXmlTask().execute();

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                     Toast.makeText(getApplicationContext(), "Clicked!", Toast.LENGTH_SHORT).show();
            }
        });

    }

    public class DownloadXmlTask extends
            AsyncTask<Void, Integer, ArrayList<Rate>> {

        @Override
        protected ArrayList<Rate> doInBackground(Void... value) {
            ArrayList<Rate> val = loadXmlFromNetwork();
            return val;

        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            PostingDialog.show();
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            // TODO Auto-generated method stub
            super.onProgressUpdate(values);
        }

        protected void onPostExecute(ArrayList<Rate> val) {
            super.onPostExecute(val);
            PostingDialog.dismiss();
            CustomListViewAdapter adapter = (CustomListViewAdapter)listView.getAdapter();
            try {

                if (val.size() > 1) {

                    // for(Rate y: x){
                    for (int i = 0; i < val.size(); i++) {

                        RowItem item = new RowItem(R.drawable.rihanna,
                                String.valueOf(val.get(i).getGasRate()), val
                                        .get(i).getDateModified()
                                        .toLocaleString());
                        adapter.add(item);
                    }

                    adapter.setNotifyOnChange(true);
                    listView.setAdapter(adapter);
                    Toast.makeText(NewListView.this, "Rates Updated",
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(),
                            "Rates Updated Failed", Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), e.toString(),
                        Toast.LENGTH_LONG).show();
            }
        }
    }

    private ArrayList<Rate> loadXmlFromNetwork() {

        return new CallSoap().MostRecentPetrojamRate();
    }
}

这是我的自定义适配器类

package com.nico.gg;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomListViewAdapter extends ArrayAdapter<RowItem> {

    Context context;

    public CustomListViewAdapter(Context context, int resource,
            List<RowItem> items) {
        super(context, resource, items);
        this.context = context;
        // TODO Auto-generated constructor stub
    }

    private class ViewHolder{
        ImageView image;
        TextView txtRateName;
        TextView txtDatePosted;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder = null;
        RowItem rowItem = getItem(position);
        View row = convertView;
        LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if(row == null){
            row = mInflater.inflate(R.layout.list_row,null);
            holder = new ViewHolder();
            holder.txtRateName = (TextView)row.findViewById(R.id.gastype);
            holder.txtDatePosted = (TextView)row.findViewById(R.id.date);
            holder.image = (ImageView)row.findViewById(R.id.list_image);
            row.setTag(holder);
        }else{
            holder = (ViewHolder)row.getTag();
            holder.txtRateName.setText(rowItem.getRateName());
            holder.txtDatePosted.setText(rowItem.getDatePosted());
            holder.image.setImageResource(rowItem.getRateImgId());
        }
        return row;
    }



}

感谢您停下来并试图提供帮助...我等待您对我的问题发表评论。

4

0 回答 0