Android 在片段内的PostExecute上更新/创建自定义Listview
所以我在安卓系统中有一个分区寻呼机应用程序。在我的第四个片段中,我运行了一个异步任务,该任务通过蓝牙连接到一个设备,并更新了我创建的自定义列表(据推测),但是,该列表要么更新得很晚,要么根本不更新。我不确定如何在postexecute上进行更新,所以我在asynctask之外进行了更新 代码如下:Android 在片段内的PostExecute上更新/创建自定义Listview,android,android-fragments,android-asynctask,bluetooth,Android,Android Fragments,Android Asynctask,Bluetooth,所以我在安卓系统中有一个分区寻呼机应用程序。在我的第四个片段中,我运行了一个异步任务,该任务通过蓝牙连接到一个设备,并更新了我创建的自定义列表(据推测),但是,该列表要么更新得很晚,要么根本不更新。我不确定如何在postexecute上进行更新,所以我在asynctask之外进行了更新 代码如下: public class FourthFragment extends Fragment { private WeakReference<getBeacons> getB
public class FourthFragment extends Fragment {
private WeakReference<getBeacons> getBeaconTaskWeakRef;
ArrayList<ArtInfo> ArtList = new ArrayList<>();
;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
startNewBeaconsAsyncTask();
}
ArrayList<String> titles = new ArrayList<>();
ArrayList<String> artists = new ArrayList<>();
ArrayList<String> years = new ArrayList<>();
ArrayList<Integer> images = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
for (int i = 0; i < ArtList.size(); i++) {
titles.add(ArtList.get(i).getArtTitle());
artists.add(ArtList.get(i).getArtistName());
years.add(ArtList.get(i).getYear());
int resID = getResources().getIdentifier(ArtList.get(i).getImageFilename(), "drawable", "com.acuart.acumen.acuart");
images.add(resID);
}
View v = inflater.inflate(R.layout.frag_list, container, false);
ListView byTitleList = (ListView) v.findViewById(R.id.byTitleList);
byTitleList.setAdapter(new titleList(getActivity(), R.layout.custom_list, titles));
return v;
}
private void startNewBeaconsAsyncTask() {
getBeacons newbeacons = new getBeacons(this);
this.getBeaconTaskWeakRef = new WeakReference<getBeacons>(newbeacons);
newbeacons.execute();
}
class titleList extends ArrayAdapter<String> {
public titleList(Context context, int resource, ArrayList<String> objects) {
super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.custom_list, null);
TextView title = (TextView) v.findViewById(R.id.row_title);
TextView artist = (TextView) v.findViewById(R.id.row_artist);
TextView year = (TextView) v.findViewById(R.id.row_year);
ImageView image = (ImageView) v.findViewById(R.id.row_image);
title.setText(titles.get(position));
artist.setText(artists.get(position));
year.setText(years.get(position));
image.setBackgroundResource(images.get(position));
return v;
}
}
private class getBeacons extends AsyncTask<Void, Void, Void> {
private WeakReference<FourthFragment> fragmentWeakReference;
private getBeacons(FourthFragment fragment) {
this.fragmentWeakReference = new WeakReference<FourthFragment>(fragment);
}
ProgressDialog dialog = new ProgressDialog(getActivity());
Context context = getApplicationContext();
int artCount = 0;
SQLHelper markerDBHelper = new SQLHelper(context);
@Override
protected void onPreExecute() {
dialog.setMessage("Loading, please wait...");
dialog.show();
}
@Override
protected Void doInBackground(Void... params) {
checkBluetooth();
}
@Override
protected void onPostExecute(Void v) {
dialog.dismiss();
}
} //processing bluetooth data and creating a query for database return.
}
公共类第四个片段扩展片段{
私人WeakReference getBeaconTaskWeakRef;
ArrayList ArtList=新的ArrayList();
;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setRetainInstance(真);
startNewBeaconsAsyncTask();
}
ArrayList titles=新的ArrayList();
ArrayList艺术家=新建ArrayList();
ArrayList年份=新ArrayList();
ArrayList images=新的ArrayList();
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
对于(int i=0;i
非常感谢您的帮助/评论/想法。onPostExecute()中的代码运行在UI线程上,因此您应该能够在那里更新列表适配器 我被你的问题弄糊涂了,你是说运行
onPostExecute()
需要很长时间吗?是否因为调用onPostExecute()
花费的时间太长,所以将代码放在那里以更新列表,然后将其移出
您是否正在运行一系列其他异步任务
我没有时间测试编译/测试这个,所以可能会有一些语法错误,但这只是给你一个想法
在标题列表中
添加更新支持适配器列表的数据的方法,以便:
public void updateAdapterData(ArrayList<String> newData) {
clear();
addAll(newData);
notifyDataSetChanged();
}
public void updateAdapterData(ArrayList newData){
清除();
addAll(newData);
notifyDataSetChanged();
}
异步任务可以做类似的事情
private titleList mTitleList; //Set this in your onCreateView
private class getBeacons extends AsyncTask<Void, Void, ArrayList<String>> {
@Override
protected void onPreExecute() {
dialog.setMessage("Loading, please wait...");
dialog.show();
}
@Override
protected ArrayList<Object> doInBackground(Void... params) {
//If checkbluetooth returns a list..
return checkBluetooth();
}
@Override
protected void onPostExecute(ArrayList<String> newList) {
mTitleList.updateAdapterData(newList)
dialog.dismiss();
}
}
private titleList mTitleList//在onCreateView中设置此选项
私有类getBeacons扩展异步任务{
@凌驾
受保护的void onPreExecute(){
setMessage(“正在加载,请稍候…”);
dialog.show();
}
@凌驾
受保护的ArrayList doInBackground(无效…参数){
//如果checkbluetooth返回一个列表。。
返回checkBluetooth();
}
@凌驾
受保护的void onPostExecute(ArrayList newList){
mTitleList.UpdateDapterData(新列表)
dialog.dismise();
}
}
在UI线程上运行onPostExecute()
中的代码,因此您应该能够在那里更新列表适配器
我被你的问题弄糊涂了,你是说运行onPostExecute()
需要很长时间吗?是否因为调用onPostExecute()
花费的时间太长,所以将代码放在那里以更新列表,然后将其移出
您是否正在运行一系列其他异步任务
我没有时间测试编译/测试这个,所以可能会有一些语法错误,但这只是给你一个想法
在标题列表中
添加更新支持适配器列表的数据的方法,以便:
public void updateAdapterData(ArrayList<String> newData) {
clear();
addAll(newData);
notifyDataSetChanged();
}
public void updateAdapterData(ArrayList newData){
清除();
addAll(newData);
private class getBeacons extends AsyncTask<Void, Void, ArrayList<String> > {
ArrayList<String> titles = new ArrayList<String>();
private getBeacons() {
}
@Override
protected void onPreExecute() {
}
@Override
protected ArrayList<String> doInBackground(Void... params) {
//call a method for assigning values to titles
return titles;
}
@Override
protected void onPostExecute(ArrayList<String> titles) {
//Now assign this arraylist referrence to your actual titles arraylist
adapter.notifyDataSetChanged();
}
}