我正在尝试使用异步任务填充片段列表视图, 但是listvew并没有填充。我在日志中获取数据,并且日志中没有错误和异常。
我正在关注这个例子
dynamic listview adding "Load more items" at the end of scroll
这是代码: -
public class HindiFragment extends Fragment {
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";
static final String KEY_VIDEO_URL = "video";
static final String KEY_UPLOAD_BY = "upload_by";
Context abc=null;
static int startIndex = 0;
private WeakReference<MyAsyncTask> asyncTaskWeakRef;
ListView list;
static LazyAdapter adapter;
JSONObject json;
static int offset = 10;
static ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
public static Context hindiFragment=null;
private static int catId=0;
static View rootView ;
public HindiFragment(){}
public HindiFragment(int position) {
// TODO Auto-generated constructor stub
catId=position;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
//StrictMode.setThreadPolicy(policy);
rootView = inflater.inflate(R.layout.main, container, false);
// TextView tvLabel = (TextView)rootView.findViewById(R.id.txtLabel);
// tvLabel.setText("Hello"); try
hindiFragment=rootView.getContext();
try{
//Toast.makeText(hindiFragment, "catid is "+catId,
// Toast.LENGTH_LONG).show();
UserFunctions userFunction = new UserFunctions();
json=userFunction.getAndroidVersion();
JSONArray android_version_array = json.getJSONArray("version");
TextView tv = (TextView) rootView.findViewById(R.id.android_version);
//getting android version
for (int i = 0; i < android_version_array.length(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
JSONObject myObj = android_version_array.getJSONObject(i);
if(! myObj.getString("version").equalsIgnoreCase(String.valueOf(getString(R.string.android_version)))){
tv.setText( Html.fromHtml(myObj.getString("text")));
tv.setMovementMethod(LinkMovementMethod.getInstance());
}else
{
tv.setVisibility(View.GONE);
}
// adding each child node to HashMap key => value
}
startNewAsyncTask(this.getActivity());
return rootView;
}catch(Exception e){
e.printStackTrace();
}
return rootView;
}
public static void loadMore(int startIndex,int page,Activity myActivity){
try{
UserFunctions userFunction = new UserFunctions();
Log.e("page ",""+page);
int status=0;
JSONObject json;
json = userFunction.getChannelData(String.valueOf(catId),page);
if(json.has("video")){
JSONArray deletedtrs_array = json.getJSONArray("video");
for (int i = 0; i < deletedtrs_array.length(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
JSONObject myObj = deletedtrs_array.getJSONObject(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, myObj.getString("uid"));
map.put(KEY_TITLE, myObj.getString("uid"));
map.put(KEY_ARTIST,myObj.getString("video"));
map.put(KEY_DURATION, myObj.getString("duration"));
map.put(KEY_THUMB_URL,myObj.getString("thumb_url"));
map.put(KEY_VIDEO_URL, myObj.getString("url"));
map.put(KEY_UPLOAD_BY,"By: "+ myObj.getString("upload_by"));
// adding HashList to ArrayList
//if(!songsList.contains(map))
{
songsList.add(map);
status=1;
}
}
}
//if(status==1)
{
//]list=(ListView)rootView.findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
// adapter=new LazyAdapter(myActivity, songsList);
// list.setAdapter(adapter);
// adapter.notifyDataSetChanged();
}
}catch(Exception e){
e.printStackTrace();
}
}
private void startNewAsyncTask(Activity act) {
MyAsyncTask asyncTask = new MyAsyncTask(act);
this.asyncTaskWeakRef = new WeakReference<MyAsyncTask >(asyncTask );
asyncTask.execute();
}
private static class MyAsyncTask extends AsyncTask<Void, Void, Void> {
private WeakReference<HindiFragment> fragmentWeakRef;
HindiFragment uindiFragment;
Activity myActivity;
private MyAsyncTask (Activity activity) {
this.fragmentWeakRef = new WeakReference<HindiFragment>(uindiFragment);
myActivity=activity;
}
@Override
protected Void doInBackground(Void... params) {
//Toast.makeText(hindiFragment, "helllo",
// Toast.LENGTH_LONG).show();
//TODO: your background code
Log.e("Now in background",offset+"");
loadMore(startIndex, offset,myActivity);
return null;
}
@Override
protected void onPostExecute(Void response) {
super.onPostExecute(response);
ListView list=(ListView)rootView.findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(myActivity, songsList);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
Log.e("Ended here11 ","now endeded11");
if (this.fragmentWeakRef.get() != null) {
//TODO: treat the result
adapter.notifyDataSetChanged();
}
}
}
}
答案 0 :(得分:0)
你过度使用弱引用。我的猜测是你的异步任务或片段被垃圾收集,因为你周期性地使用它们:你可以通过查看override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let identifier = segue.identifier{
switch identifier {
case "edit view":
let editTableVC = segue.destinationViewController as! EditTableViewController
if let indexPath = self.tableView.indexPathForCell(sender as! UITableViewCell){
editTableVC.product = productAtIndexPath(indexPath)
}
case "detail view":
let detailTableVC = segue.destinationViewController as! DetailTableViewController
if let indexPath = self.tableView.indexPathForCell(sender as! UITableViewCell){
detailTableVC.product = productAtIndexPath(indexPath)
}
default: break
}
}
}
是否返回null来验证这一点。
事实上,您根本不需要weakReference.get()
:只需实施适当的取消逻辑,您可以从WeakReference
或onPause onStop
调用,具体取决于您希望如何处理重复调用。
此外,您不应将上下文传递给onDestroy
类:而是创建一个观察者,例如:
Async
如果没有取消,
public interface Callback {
public void onAsyncDone(Arraylist<DataItem> listItems);
}
其中
@Override public void onPostExecute(Void v) {
if (!isCancelled()) {
Callback c = callbackReference.get();
if (c != null) c.onAsyncDone(getListItems());
}
}
返回getListItems()
中完成的任何工作。在这里,我使用弱引用作为回调(您在活动中实现),因为您似乎想要使用它。但同样,只要您在取消时释放对上下文的引用,您实际上并不需要。活动被包装为doInBackground
的原因是异步任务应该只有一个作业:处理项目并传递它们。让您的活动或控制器使用新项目更改UI。
答案 1 :(得分:0)
我怀疑loadMore
()方法没有将对象songsList
中的任何数据加载到适配器中。只需追溯其原因。如果不使用调试器,我就无法调试它,代码流非常复杂,引用异常严密,更常见的是静态访问。
可疑的审查代码:
protected void onPostExecute(Void response) {
super.onPostExecute(response);
ListView list=(ListView)rootView.findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(myActivity, songsList);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
...
}
注意:您可以在LazyAdapter
代码中添加一些良好的日志记录,并查看是否填充了数据。