AsyncTask onPostExecute TextView未更新

时间:2014-05-04 11:08:54

标签: java android android-asynctask

我的情况是这样的:

我的主要活动有3个片段。第一个片段从服务器请求数据并尝试呈现它。我使用AsyncTask内部类来获取数据并在onPostExecute中呈现。我使用SimpleCursorAdapterListView进行渲染。渲染工作正常。完成此操作后,我尝试操作UI(TextView的设置值),但它无法正常工作。 getViewById不会返回null或任何错误,但由于某种原因我仍然无法更改该值。任何建议都非常感谢。这是我使用的Fragment类:

public class FeedFragment extends Fragment {

        @Override
        public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_feed, container, false);
        Activity activity = getActivity();
        try {
             new getFeedData(activity,rootView).execute();

        }

        finally{
            return rootView;
        }


        }

     private class getFeedData extends AsyncTask<Void, Void, String> {
         private Context activity;
         private View rootView;
         public getFeedData(Context context, View main){
             this.activity=context;
             this.rootView=main;
         }
         @Override
          protected String doInBackground(Void... params) {
            try {
              HttpRequest request =  HttpRequest.get("http://upinion.us/test/feed_sample/");
              String response = "";
              if (request.ok()) {
              response = request.body();
              }
              return response;
            } catch (HttpRequestException exception) {
              return null;
            }
          }

          @Override
          protected void onPostExecute(String response) {

              String[] columns = new String[] {"_id","title","description"};
                 int[] viewIds = new int[] {R.id.postId,R.id.title,R.id.description};

                 JSONArray finalResponse;
                    try {
                        finalResponse = new JSONArray(response);
                    } catch (JSONException e) {

                        e.printStackTrace();
                        finalResponse = null;
                    }
                    MatrixCursor mc = new MatrixCursor(new String[] {"_id","title","description"}); // properties from the JSONObjects
                      for (int i = 0; i < finalResponse.length(); i++) {
                          JSONObject jsonObject;

                        try {
                            jsonObject = finalResponse.getJSONObject(i);


                            mc.addRow(new Object[] {jsonObject.get("_id"),jsonObject.get("title"),jsonObject.get("description")});
                        } catch (JSONException e) {
                            jsonObject=null;
                        }

                      }

              SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.post, mc, columns, viewIds,0); 
                 ListView list = (ListView) this.rootView.findViewById(R.id.feed_feed);
               list.setAdapter(mAdapter);
               View v;
               for (int i = 0; i < list.getCount(); i++) {


                   v = list.getAdapter().getView(i, null, null);
                  TextView text = (TextView) v.findViewById(R.id.title);

                  text.setText("HELLO MOTO");


               }
               list.setOnItemClickListener(new OnItemClickListener() {
                   public void onItemClick(AdapterView<?> parent, View view,
                       int position, long id) {

                       // selected item 
                       Integer convId = parent.getId();



                   }

                 });


          }


        }


}

正如您所看到的,我将活动和膨胀的根视图传递给asynctask,但仍然没有运气。我花了将近两天的时间试图找到一个没有成功的解决方案。

1 个答案:

答案 0 :(得分:0)

 for (int i = 0; i < list.getCount(); i++) {

           v = list.getAdapter().getView(i, null, null);
          TextView text = (TextView) v.findViewById(R.id.title);

          text.setText("HELLO MOTO");


       }

这是你的问题:

你不应该自己调用getView

要更新您的列表视图,您需要致电adapter.notifyDataSetChanged

,系统会为您调用更新

所以请完全删除这段代码,然后将其替换为adapter.notifyDataSetChanged