2017-11-19 18 views
0

私は今までに達成したことは何でもこの基本的な知識を持っています。このWebサイトまたはYouTube動画を使用して達成されています(以前は私が.get()を使用していましたUIのブロックを避けるために、AsyncTaskからデータを取得できないので、このimを削除した後、OnCreateView()関数から.get()関数を削除するアドバイスでした) 。私はそれをしなかったが、今私は、ビューを作成することができないよ、私がやった研究が、この強度ここでAsyncTaskは、BaseAdpterを使用してGridViewを作成しません

私のコードは、この

OnCreateView()からのビューを作成する方法を助けてくださいさを取得できませんでしたくさん: -

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
    View GView = inflater.inflate(R.layout.fragment_dashboard, container, false); 
    progressBarHolder = (FrameLayout) GView.findViewById(R.id.progressBarHolder); 
    GridView gridView = (GridView) GView.findViewById(R.id.gridView); 
    //Toast.makeText(getActivity(),Json_String,Toast.LENGTH_LONG).show(); 
    String finalResult = null; 
    try{ 
     finalResult = String.valueOf(new JSONTask().execute("https://www.example.in/android_api/dashboard_data",JsonData())); 
     Toast.makeText(getActivity(),Json_String,Toast.LENGTH_LONG).show(); 
     JSONObject parentObject = null; 
     parentObject = new JSONObject(finalResult); 
     if(((String) parentObject.names().get(0)).matches("error")){ 
      JSONObject jObj = parentObject.getJSONObject("error"); 
      errorThrow(jObj.getString("Description")); 
     } else if(((String) parentObject.names().get(0)).matches("success")){ 
      JSONObject jObj = parentObject.getJSONObject("success"); 
      JSONArray arrajson = jObj.getJSONArray("data"); 
      String arrayCount = Integer.toString(arrajson.length()); 
      String[] type = new String[arrajson.length()]; 
      Integer[] count = new Integer[arrajson.length()]; 
      for (int i=0; i<arrajson.length();i++){ 
       JSONObject jsonObject = arrajson.getJSONObject(i); 
       type[i] = jsonObject.getString("type"); 
       count[i] = jsonObject.getInt("count"); 
      } 
      CustomAdpter customAdpter = new CustomAdpter(DashboardFragment.this,type,count); 
      gridView.setAdapter(customAdpter); 
      return GView; 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    return GView; 
} 

ベースアダプタコード: -

class CustomAdpter extends BaseAdapter { 

    String[] type; 
    Integer[] count; 
    public CustomAdpter(DashboardFragment dashboardFragment, String[] type, Integer[] count){ 
     this.count = count; 
     this.type = type; 
    } 
    @Override 
    public int getCount() { 
     return type.length; 
    } 

    @Override 
    public Object getItem(int i) { 
     return null; 
    } 

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

    @Override 
    public View getView(int i, View view, ViewGroup viewGroup) { 
     view = getLayoutInflater().inflate(R.layout.grid_single_itme,null); 
     TextView textView = (TextView) view.findViewById(R.id.TextView1); 
     TextView textView1 = (TextView) view.findViewById(R.id.textView2); 
     textView.setText(String.valueOf(count[i])); 
     textView1.setText(type[i]); 
     view.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Toast.makeText(getActivity(),"Booking Item Clicked",Toast.LENGTH_LONG).show(); 
      } 
     }); 
     return view; 
    } 
} 

AsyncTaskコード: -

public class JSONTask extends AsyncTask<String,String,String> { 
    private ProgressDialog mProgressDialog; 

    int progress; 
    public JSONTask(){ 
     mProgressDialog = new ProgressDialog(getContext()); 
     mProgressDialog.setMax(100); 
     mProgressDialog.setProgress(0); 
    } 

    @Override 
    protected void onPreExecute(){ 
     mProgressDialog = ProgressDialog.show(getContext(),"Loading","Loading Data...",true,false); 
     super.onPreExecute(); 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     HttpURLConnection connection = null; 
     BufferedReader reader = null; 
     final String finalJson = params[1]; 
     String json = finalJson; 
     try{ 
      URL url = new URL(params[0]); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.setConnectTimeout(5000); 
      connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); 
      connection.setRequestProperty("A-APK-API", "******"); 
      connection.setRequestProperty("Authorization", "Basic **:**"); 
      connection.setDoOutput(true); 
      connection.setDoInput(true); 
      connection.setRequestMethod("POST"); 
      connection.connect(); 

      OutputStream stream = connection.getOutputStream(); 
      OutputStreamWriter streams = new OutputStreamWriter(stream, "UTF-8"); 
      stream.write(json.getBytes("UTF-8")); 
      stream.close(); 

      reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8")); 

      StringBuffer buffer = new StringBuffer(); 

      String line = ""; 
      while((line = reader.readLine()) != null){ 
       buffer.append(line); 
      } 
      return buffer.toString(); 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if(connection != null){ 
       connection.disconnect(); 
      } 
      try { 
       if(reader != null) { 
        reader.close(); 
       } 
      } catch (IOException e){ 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 
    protected void onPostExecute(String result){ 
     super.onPostExecute(result); 
     Json_String = result; 
     Toast.makeText(getContext(),result,Toast.LENGTH_LONG).show(); 
     mProgressDialog.dismiss(); 
    } 
} 

ここで私を助けてください

答えて

1

であなたのAsyncTaskクラス 表情からデータを取得するためのインタフェースを使用しています。

この文を変更してください。 asynctaskのみを開始します。

次に、その行の後にあるすべてのコードを、AsyncTaskのonPostExecute()に配置します。

すべてです。

0
あなたが最初にこの

1.Atを行い、ArrayListのなどAsyncTaskdoInBackGround方法を経由してリスト内のデータを取得する必要がありますが、アダプタを作成し、 を取り付ける方法を変更する必要があり

  • その後、onPostExecute方法でデータを取得し、アダプタを作成し、データyを得ている一方で、あなたのビュー
  • に添付し、 いくつかを表示することができます ProgressDialog
  • あなたAsyncTaskは、他の別のクラスである場合、)あなたが(に.get使用いけないときは、asynctaskから結果を得ることができません。このhttps://stackoverflow.com/a/47373959/8197737
  • 関連する問題