2013-10-31 85 views
0

我做了一個listview應用程序,然後我創建了一個新的片段,並希望實現listview到片段。但是當我做我有一個奇怪的錯誤。Android奇怪的錯誤

public class Fragment1 extends Fragment { 
private ArrayList<FeedItem> feedList = null; 
private ProgressBar progressbar = null; 
private ListView feedListView = null; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.list_row_layout, container, false); 

     progressbar = (ProgressBar)rootView.findViewById(R.id.progressBar); 
     String url = ""; 
     new DownloadFilesTask().execute(url); 
    return rootView; 


    } 

    public void updateList() { 
     feedListView= (ListView)getActivity().findViewById(R.id.custom_list); 
     feedListView.setVisibility(View.VISIBLE); 
     progressbar.setVisibility(View.GONE); 

     **feedListView.setAdapter(new CustomListAdapter(this, feedList));** 
     feedListView.setOnItemClickListener(new OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
         Object o = feedListView.getItemAtPosition(position); 
         FeedItem newsData = (FeedItem) o; 



         **Intent intent = new Intent(FeedListActivity.this, FeedDetailsActivity.class);** 
         intent.putExtra("feed", newsData); 
         startActivity(intent); 
       } 
     }); 
      } 

      public class DownloadFilesTask extends AsyncTask<String, Integer, Void> { 

     @Override 
     protected void onProgressUpdate(Integer... values) { 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
       if (null != feedList) { 
         updateList(); 
       } 
     } 

     @Override 
     protected Void doInBackground(String... params) { 
       String url = params[0]; 

       // getting JSON string from URL 
       JSONObject json = getJSONFromUrl(url); 

       //parsing json data 
       parseJson(json); 
       return null; 
      } 
      } 


      public JSONObject getJSONFromUrl(String url) { 
      InputStream is = null; 
     JSONObject jObj = null; 
      String json = null; 

     // Making HTTP request 
     try { 
       // defaultHttpClient 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       HttpPost httpPost = new HttpPost(url); 

       HttpResponse httpResponse = httpClient.execute(httpPost); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       is = httpEntity.getContent(); 

       BufferedReader reader = new BufferedReader(new InputStreamReader(
           is, "iso-8859-1"), 8); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) { 
         sb.append(line + "\n"); 
       } 
       is.close(); 
       json = sb.toString(); 
     } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
     } catch (IOException e) { 
       e.printStackTrace(); 
     } 

     try { 
       jObj = new JSONObject(json); 
      } catch (JSONException e) { 
       Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

      // return JSON String 
      return jObj; 

     } 

      public void parseJson(JSONObject json) { 
       try { 

       // parsing json object 
       if (json.getString("status").equalsIgnoreCase("ok")) { 
         JSONArray posts = json.getJSONArray("posts"); 

         feedList = new ArrayList<FeedItem>(); 

         for (int i = 0; i < posts.length(); i++) { 
           JSONObject post = (JSONObject) posts.getJSONObject(i); 
           FeedItem item = new FeedItem(); 
           item.setTitle(post.getString("title")); 
           item.setDate(post.getString("description")); 
           item.setId(post.getString("id")); 
           item.setUrl(post.getString("url")); 
           item.setContent(post.getString("description")); 
           JSONArray attachments = post.getJSONArray("attachments"); 

           if (null != attachments && attachments.length() > 0) { 
             JSONObject attachment = attachments.getJSONObject(0); 
             if (attachment != null) 
               item.setAttachmentUrl(attachment.getString("url")); 
           } 

           feedList.add(item); 
         } 
       } 
     } catch (JSONException e) { 
       e.printStackTrace(); 
     } 
    } 

    } 

的問題是在這行

  feedListView.setAdapter(new CustomListAdapter(this, feedList)); 
      Intent intent = new Intent(FeedListActivity.this, FeedDetailsActivity.class); 

多個標記在該行 - 行斷點:片段1 [行:57] - updateList()

- The constructor CustomListAdapter(Fragment1, ArrayList<FeedItem>) is 
undefined 
     No enclosing instance of the type FeedListActivity is accessible in scope 

CustomListAdapter:

  public class CustomListAdapter extends BaseAdapter { 

     private ArrayList<FeedItem> listData; 

     private LayoutInflater layoutInflater; 

     private Context mContext; 

     public CustomListAdapter(Context context, ArrayList<FeedItem> listData) { 
      this.listData = listData; 
      layoutInflater = LayoutInflater.from(context); 
      mContext = context; 
      } 

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

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

       @Override 
       public long getItemId(int position) { 
      return position; 
      } 

      public View getView(int position, View convertView, ViewGroup parent) { 
      ViewHolder holder; 
      if (convertView == null) { 
        convertView = layoutInflater.inflate(R.layout.list_row_layout, null); 
        holder = new ViewHolder(); 
        holder.headlineView = (TextView) convertView.findViewById(R.id.title); 
        holder.reportedDateView = (TextView) convertView.findViewById(R.id.date); 
        holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage); 
        convertView.setTag(holder); 
      } else { 
        holder = (ViewHolder) convertView.getTag(); 
      } 

      FeedItem newsItem = (FeedItem) listData.get(position); 
      holder.headlineView.setText(newsItem.getTitle()); 
      holder.reportedDateView.setText(newsItem.getDate()); 

      if (holder.imageView != null) { 
        new ImageDownloaderTask(holder.imageView).execute(newsItem.getAttachmentUrl()); 
      } 

      return convertView; 
     } 

     static class ViewHolder { 
      TextView headlineView; 
      TextView reportedDateView; 
      ImageView imageView; 
      } 
      } 
+0

替換此向我們展示您的CustomListAdapter – Ahmad

+0

我加入到質疑,應用也很好地工作,但現在從不能代替 –

+2

'this'片段,使用'getActivity()'。 –

回答

0

在此行feedListView.setAdapter(new CustomListAdapter(this, feedList));只是getActivity();

+0

at com.ma.faws.Fragment1.updateList(Fragment1.java:54) –

+0

它現在有效,非常感謝你:) –

+0

如果你發現答案有用dnt忘記檢查它的權利,因爲它可以幫助其他用戶抓住正確的答案btw高興幫助:)快樂的編碼。 –