Android:列表适配器中http请求的正确位置在哪里
我在我的一个活动中有一个listview的列表适配器 现在,我想用从web服务获得的数据填充这个列表视图。哪里是向服务器执行异步请求以获取json数据以填充适配器的正确位置 1:在实例化适配器并将其作为构造函数传递之前? 但是我不知道怎样才能让人耳目一新 2:适配器中的某些位置 2.1:在适配器构造函数中 2.2“获取视图”中的某些位置 这是迄今为止我的适配器:Android:列表适配器中http请求的正确位置在哪里,android,listview,android-adapter,listadapter,Android,Listview,Android Adapter,Listadapter,我在我的一个活动中有一个listview的列表适配器 现在,我想用从web服务获得的数据填充这个列表视图。哪里是向服务器执行异步请求以获取json数据以填充适配器的正确位置 1:在实例化适配器并将其作为构造函数传递之前? 但是我不知道怎样才能让人耳目一新 2:适配器中的某些位置 2.1:在适配器构造函数中 2.2“获取视图”中的某些位置 这是迄今为止我的适配器: public class MyDevicesAdapter extends BaseAdapter { private Contex
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;
}
}
在别的地方;)
根据您的应用程序,您可以从任何位置触发下载。但是,您应该在单独的线程中或通过使用异步任务来完成这项工作。因此,这不会阻塞您的UI。如果数据可用,只需调用
notifyDataSetChanged()
通知侦听器即可。用户界面将在该调用后自动重新加载。初始化适配器和列表,然后调用异步任务。
在post方法中,您可以调用adapter.notifyDataSetChanged(),以便在每次更新列表时对其进行更改。在初始化过程中获取数据 通过回调让适配器知道这一点(适配器实现回调接口并将其注册到AsyncTask,当工作停止时,AsyncTask执行回调)。根据必须传递到适配器的数据类型创建ArrayList(或以您希望传递数据的任何形式)。在onCreate中,将其作为参数传递给适配器,即使它为空。此时您将无法获得任何数据 然后在另一个线程上,假设Asynctask获取数据并将其添加到ArrayList中。发布这个简单的电话
adapter.setNotifyDataSetChanged();
这将自动刷新列表,并使用ArrayList的最新值填充列表。ListView的设计方式是,每次调用notifyDataSetChanged()时它将根据初始化时使用的数据进行自我刷新。您是否在谈论ListView刷新?根据我的意见,使用
AsyncTask
向Background
发出请求,并将适配器设置为ListView
设置为onPostExecute(…)
顺便说一句,getView()的最后一行似乎是错误的;返回视图;哦,好的,我明白了。所以我初始化并显示一个空列表。然后我可以显示装货标志。然后执行请求以获取数据:一旦加载了数据。我可以隐藏加载标志,然后刷新列表?没错,你说得对:)只要记住在单独的线程中向web服务发出请求(异步任务)。它有一个post-execute方法,您可以在其中执行列表刷新部分。看这个-它只遗漏了进度部分,但这是一个艰难的过程。:)卡克沃克-你玩得太多了吗?@rekire-Hehe-nopes:)。Zapnologica-如果这对您有效,请告知您是否需要帮助。
adapter.setNotifyDataSetChanged();