2017-05-02 7 views
1

サーバーからリストを取得するときに問題があります。オブジェクトをチェックする場合は、そこにオブジェクトがあるかどうかを確認します。 オブジェクトがある場合、私のレイアウトのテキストに表示されます:数秒後にオブジェクトがなくなり、それが消えます と私のリストを表示します。AsyncTaskのチェックリスト

オブジェクトが私のレイアウトに直接表示されるようにしたいです。 RetrieveTaskはAsyncTaskを拡張 プライベートクラス> {

@Override 
    protected List<branch> doInBackground(String... params) { 

     manager = new SessionManager(getActivity().getApplicationContext()); 
     manager.checkLogin(); 
     HashMap<String, String> user = manager.getUserID(); 
     String uid = user.get(SessionManager.KEY_uid); 
     // Intent intent= getActivity().getIntent(); // gets the previously created intent 
     //String type18 = intent.getStringExtra("key28"); 
     String strUrl = 

     HttpURLConnection connection = null; 
     BufferedReader reader = null; 

     try { 
      URL url = new URL(strUrl); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.connect(); 
      InputStream stream = connection.getInputStream(); 
      reader = new BufferedReader(new InputStreamReader(stream)); 
      StringBuffer buffer = new StringBuffer(); 
      String line = ""; 
      while ((line = reader.readLine()) != null) { 
       buffer.append(line); 
      } 

      String finalJson = buffer.toString(); 

      JSONObject parentObject = new JSONObject(finalJson); 
      JSONArray parentArray = parentObject.getJSONArray("markers"); 

      //////////////////////////////////////// 
      List<branch> bList = new ArrayList<>(); 

      Gson gson = new Gson(); 
      for (int i = 0; i < parentArray.length(); i++) { 
       JSONObject finalObject = parentArray.getJSONObject(i); 

       /////////market object 
       market mr = new market(Integer.valueOf(finalObject.getString("id")), finalObject.getString("mName") 
         , finalObject.getString("pic")); 

       /////////location object 
       location lo = new location(Float.valueOf(finalObject.getString("lat")), Float.valueOf(finalObject.getString("lng")) 
         , finalObject.getString("area"), finalObject.getString("city")); 


       /////////(branch object) inside it (market object) 
       branch it = new branch(Integer.valueOf(finalObject.getString("bid")), Integer.valueOf(finalObject.getString("phone")) 
         , finalObject.getString("ser"), Integer.valueOf(finalObject.getString("dis")), Integer.valueOf(finalObject.getString("brNo")) 
         , Float.valueOf(finalObject.getString("rating")), mr, lo,true); 

       /*//////////add marker 
       LatLng latlng = new LatLng(Double.valueOf(finalObject.getString("lat")), Double.valueOf(finalObject.getString("lng"))); 
       addMarker(latlng, Integer.valueOf(finalObject.getString("bid")));*/ 

       // adding the branch object in the list 
       bList.add(it); 
      } 
      return bList; 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } finally { 
      if (connection != null) { 
       connection.disconnect(); 
      } 
      try { 
       if (reader != null) { 
        reader.close(); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     return null; 

    } 

ここpostExcute

@Override 
    protected void onPostExecute(final List<branch> result) { 
     super.onPostExecute(result); 

     if (result != null) { 

      final itemAdapter adapter = new itemAdapter(getActivity().getApplicationContext(), R.layout.row_favmarkets, result); 
      lv.setAdapter(adapter); 


     } 
    } 

} 

が、これは

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="4dp" 
android:paddingLeft="4dp" 
android:paddingRight="4dp" 
android:paddingTop="4dp" 
tools:context="com.gmplatform.gmp.favouriteitem"> 
<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/textView28" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="40dp" 
     android:layout_marginTop="250dp" 
     android:text="There is no Markets in your Favourite List.." 
     android:textSize="24sp" 
     android:visibility="gone" /> 
</LinearLayout> 

<ListView 
    android:id="@+id/l20" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:horizontalSpacing="3dp" 
    android:numColumns="3" 
    android:verticalSpacing="0dp" /> 
<Button 
    android:text="Button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/button6" 
    android:layout_marginLeft="400dp" /> 

+0

は、非同期タスクが実行される前に、目に見えるに設定されている「オブジェクトがない」示すテキストですか?それはXMLに設定されていますか? xmlレイアウトを投稿できますか? – ahasbini

+0

@ahasbini私のXMLを投稿してください、それを確認してください – Abdulelah

答えて

0
たときに最初のショーでは何も意志

提案された変更私のレイアウトxmlです要求が実行されます。完了すると、リストが空であれば「お気に入りリストには市場はありません」と表示されます。そうでない場合はリストが表示されます。

パートレイアウトXML:

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/linearLayout28"> 

    <TextView 
     android:id="@+id/textView28" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="40dp" 
     android:layout_marginTop="250dp" 
     android:text="There is no Markets in your Favourite List.." 
     android:textSize="24sp" 
     android:visibility="gone" /> 
</LinearLayout> 

初期化linearLayout28

LinearLayout linearLayout28 = findViewById(R.id.linearLayout28); 

そして、AsyncTaskの変更:あなたは解決策に基本的ですが、あなたが更新してはならない

private class RetrieveTask extends AsyncTask<Void, Void, List<branch>> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     lv.setVisibility(View.GONE); 
     linearLayout28.setVisibility(View.GONE); 
    } 

    @Override 
    protected List<branch> doInBackground(String... params) { 
     //Keep as your code 
     ... 
    } 

    @Override 
    protected void onPostExecute(final List<branch> result) { 
     super.onPostExecute(result); 

     if (result != null && result.size() != 0) { 
      final itemAdapter adapter = new itemAdapter(
       getActivity().getApplicationContext(), 
       R.layout.row_favmarkets, result); 
      lv.setAdapter(adapter); 
      lv.setVisibility(View.VISIBLE); 
      linearLayout28.setVisibility(View.GONE); 
     } else { 
      lv.setVisibility(View.GONE); 
      linearLayout28.setVisibility(View.VISIBLE); 
     } 
    } 
} 
+0

@Abdulelah、解決策はあなたのために働いていますか? – ahasbini

0

あなたのAsyncTaskからのユーザーインターフェイス。インターフェイスを作成し、アクティビティで実装します。データの準備が整ったときにUIのデータを更新したり、メッセージエラーを表示させたりします。一例として、

、のようなあなたのAsyncTaskのインターフェイスを定義します。あなたのタスクが完了したとき

private TaskCallback listener = null; 

public interface TaskCallback { 
    void OnTaskCompleted(); 
} 

public void setListener (TaskCallback listener) { 
    this.listener = listener; 
} 

@Override 
public void onPostExecute (Void v) { 
    if (listener != null) { 
     listener.OnTaskCompleted(); 
    } 
} 

あなたは、あなたのニーズにコールバックベースにパラメータを追加することができます。あなたの活動レベルで

public class YourActivity extends Activity implements YourTask.TaskCallback { 

// When you execute your task add the reference to the listener 
YourTask task = new YourTask (YourActivity.this); 
     task.setListener (YourActivity.this); 
     task.execute(); 


@Override 
public void OnTaskCompleted() { 
    // Update your screen or show an error message 
} 

} 
関連する問題