0

我知道有这个问题,但是,我不知道它对我有用,我认为它可能有点不同,我有一个网络服务来获取数据,我的活动中有一个 listView。我想更新这个列表视图。

我一直在尝试使用处理程序,但是,它不起作用,尽管,我很欣赏其他选择。我真的不知道,我是否应该从我的 AsyncTask 类中直接访问该列表,或者如何更新。

更新:所以,我没有对我的 AsyncTask 使用嵌套类,这样可能会更容易,但我不想这样做,因为我在代码的更多地方使用了这个类。我想知道如何访问活动并用我从 WS 获得的数据加载我的 ListView。我想使用 Handler 来做这件事是个坏主意。

我的活动课程是:

public class GameOnlineListActivity extends Activity {


private static final String TAG = "GameOnlineListActivity";


// List view
private ListView lv;

// Listview Adapter
ArrayAdapter<String> adapter;

// Search EditText
EditText inputSearch;

// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;

// List with datas
String users[];

// Handler on the main (UI) thread that will receive messages from the 
// second thread and update the progress.
final Handler handler = new Handler() {
    public void handleMessage(Message msg) {
        // Get the current value of the variable total from the message data
        // and update the progress bar.         
        users = msg.getData().getStringArray("users");
        adapter.notifyDataSetChanged();
        }

};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_online_list);
    users = new String[0];
    callWebService();
    // Listview Data

    lv = (ListView) findViewById(R.id.list_view);
    inputSearch = (EditText) findViewById(R.id.inputSearch);



    // Adding items to listview

    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, users);
    lv.setAdapter(adapter);

我的webservice代码有点长,因为我用同一个类来调用几个webservice,,

 public class WebServiceTask extends AsyncTask<String, Integer, String> {


private static final String TAG = "WebServiceTask";

// connection timeout, in milliseconds (waiting to connect)
private static final int CONN_TIMEOUT = 3000;

// socket timeout, in milliseconds (waiting for data)
private static final int SOCKET_TIMEOUT = 5000;

private int taskType;
private Context context = null;
private Activity activity = null;
private Handler handler;

private String processMessage = "Processing...";

private List<NameValuePair> params = new ArrayList<NameValuePair>();
private ProgressDialog pDlg = null;


public WebServiceTask(int taskType, Activity context, String processMessage, Handler h)     {

    this.taskType = taskType;
    this.activity = context;
    this.context = context;
    this.processMessage = processMessage;
    this.handler = h;

.....

private void handleResponse(String response) {
    try {


   ......

        case ConstantsTrivial.GET_USERS:

            respJSONArray = new JSONArray(response);
            listUsers = new ArrayList<Usuario>();

            for(int i=0; i<respJSONArray.length(); i++)
            {               
                JSONObject obj = respJSONArray.getJSONObject(i);                
                id = obj.getInt(ConstantsTrivial.USER_ID);
                userName = obj.getString(ConstantsTrivial.USER_NAME);
                keyId = obj.getString(ConstantsTrivial.USER_KEY_ID);
                usu = new Usuario(id, userName, keyId);

                listUsers.add(usu);

                String[] names = new String[listUsers.size()];
                int z = 0;                  
                for (Usuario u : listUsers){
                    names[z] = u.getUserName(); 
                    z++;
                }


                Message msg = handler.obtainMessage();
                Bundle b = new Bundle();
                b.putStringArray("users", names);            
                msg.setData(b);
                handler.sendMessage(msg);


            }
            Log.d(TAG,"Lista recuperada de Usuarios por webservice:" + listUsers);
            break;
        }

   } catch (Exception e) {
       Log.e(TAG, e.getLocalizedMessage(), e);
   }

 }

@Override
protected void onPostExecute(String response) {

    handleResponse(response);

    //pDlg.dismiss();

}

我有一个解决方案,但我真的不喜欢它,我不想那样做......它真的很乱......这个代码真的依赖于那个活动,我也在寻找一些东西非常依赖,因为正如我所说,WebServicesTask 是一个可以从任何地方调用的类。:(

  case ConstantsTrivial.GET_USERS:

            respJSONArray = new JSONArray(response);
            listUsers = new ArrayList<Usuario>();
            String[] names = null;

            for(int i=0; i<respJSONArray.length(); i++)
            {               
                JSONObject obj = respJSONArray.getJSONObject(i);                
                id = obj.getInt(ConstantsTrivial.USER_ID);
                userName = obj.getString(ConstantsTrivial.USER_NAME);
                keyId = obj.getString(ConstantsTrivial.USER_KEY_ID);
                usu = new Usuario(id, userName, keyId);

                listUsers.add(usu);

                names = new String[listUsers.size()];
                int z = 0;                  
                for (Usuario u : listUsers){
                    names[z] = u.getUserName(); 
                    z++;
                }


            }
            Log.d(TAG,"Lista recuperada de Usuarios por webservice:" + listUsers);
            //NEW CODE, UPDATE!!!!
            ((GameOnlineListActivity)activity).adapter = new ArrayAdapter<String>(activity, R.layout.list_item, R.id.product_name, names);               
            ((GameOnlineListActivity)activity).lv.setAdapter(((GameOnlineListActivity)activity).adapter);
            ((GameOnlineListActivity)activity).adapter.notifyDataSetChanged();

            break;

        }
4

1 回答 1

0

只需谷歌搜索“android listadapter asynctask”。你会发现很多例子,比如这个与你的任务非常相似的例子。

于 2013-04-12T16:38:30.727 回答