Android:在List Adapter中发出http请求的正确位置在哪里

时间:2014-03-10 07:17:22

标签: android listview android-adapter listadapter

我的一个活动中有一个列表视图的列表适配器,

现在我想使用从Web服务获取的数据填充此列表视图。在服务器上执行异步请求以获取json数据以填充适配器的正确位置在哪里?

1:在我实例化适配器并将其作为构造函数传递之前?      但后来我不知道多么令人耳目一新。

2:适配器中的某些位置?

2.1:在适配器构造函数中?

2.2获取视图中的某些位置?

到目前为止,这是我的适配器:

public class MyDevicesAdapter extends BaseAdapter {

private Context mContext;
private String TAG = "MyDevicesAdapter";

// Keep all Objects in array
private ArrayList<MyDevice> devices;

/**
 * Constructor for creating a my devices list adapter
 * 
 * @param context
 *            The context of the calling class
 */
public MyDevicesAdapter(Context context) {
    mContext = context;
    devices = new ArrayList<MyDevice>();
}

@Override
public int getCount() {
    return devices.size();
}

@Override
public Object getItem(int position) {
    return devices.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

/**
 * Checks if the List is empty
 */
public boolean isEmpty() {
    return devices.isEmpty();
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder holder = null;
    LayoutInflater mInflater = (LayoutInflater) mContext
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.my_device_item, null);
        holder = new ViewHolder();

        holder.imageView = (ImageView) convertView
                .findViewById(R.id.my_device_item_ImageView);
        holder.txtName = (TextView) convertView
                .findViewById(R.id.my_device_item_textView_name);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    // Temp variable to store the current list item
    final MyDevice DeviceItem = (MyDevice) getItem(position);

    //Set Image

    //Set Name


    //on click listener for the view
    convertView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        //Log.d(TAG, "Deleting order in position : " + position);
        //deleteOrder(orders.get(position), position, v);
        }
    });

    return null;
}

/**
 * Private View holder class Keeps a reference to the View for the certain
 * components. Is used to increase performance of the listView
 */
private class ViewHolder {
    ImageView imageView;
    TextView txtName;
}
}

以下是要获取我的数据的异步请求:

Network.login("mydevices",null, new AsyncHttpResponseHandler() {

        @Override
        public void onSuccess(int statusCode, Header[] headers,
                byte[] responseBody) {          
        }   

        @Override
        public void onFailure(int statusCode, Header[] headers,
                byte[] responseBody, Throwable error) {
            // Response failed :(
        }
    });

4 个答案:

答案 0 :(得分:0)

其他地方;)

根据您的应用,您可以从任何位置触发下载。但是你应该在一个单独的线程中或使用AsyncTask来完成工作。所以这不会阻止你的UI。如果数据可用,只需通过调用notifyDataSetChanged()通知听众,您就完成了。在该调用之后,UI将自动重新加载。

答案 1 :(得分:0)

初始化适配器和列表,然后调用异步任务。 在post方法中,每次更新时都可以调用adapter.notifyDataSetChanged()来更改列表。

答案 2 :(得分:0)

获取初始化进度中的数据。

让你的适配器通过CallBack知道它(适配器实现回调接口并将其注册到AsyncTask,当工作停止时,AsyncTask执行回调)。

答案 3 :(得分:0)

根据您必须传递给适配器的数据类型,创建一个ArrayList(或您希望传递数据的任何形式)。在onCreate传递中,即使它是空白的,也作为适配器的参数。此时你不会得到任何数据。

然后在一个单独的线程上,让我们说Asynctask获取数据并将其添加到您的ArrayList中。发布此简单的电话

 adapter.setNotifyDataSetChanged();

这将自动刷新List并使用ArrayList的最新值填充它。 ListView是以这种方式设计的,每次调用notifyDataSetChanged()时,它都会根据初始化的数据自行刷新。