2017-02-02 12 views
0

私のRecyclerViewのヘルプが必要です。私はこのようになります私のSQLデータベースからatring配列している:AndroidのRecyclerViewへのJsonのテキスト

  • order_number
  • typ_all(最初と最後を入力)
  • :私のRecyclerViewで

    {"success":true,"0":{"order_number":"078","typ_first":"E3rft","typ_last":"Split","order_date_time":"2016-10-11 19:20:03"},"1":{"order_number":"166","typ_first":"E483f","typ_last":"Split_test","order_date_time":"2016-10-12 18:39:30"}} 
    

    は、私は、次のフィールドを持っていますdate(時間のない日付のみ)

これは私は私の文字列配列を取得する方法をされています

String plansData = plansPreferenceData.getString("plansPreferenceData", ""); 

これは私が私のRecyclerViewにデータを設定する方法である:

// Set plan data 
Plans plan = new Plans("123", "E3rft Split", "11.10.2016"); 
// Add Object to list 
planList.add(plan); 

// Notify data changes 
pAdapter.notifyDataSetChanged(); 

マイPlansクラス:

public class Plans { 
    private String planTitle, planType, planDate; 

    public Plans(String planTitle, String planType, String planDate) { 
     this.planTitle = planTitle; 
     this.planType = planType; 
     this.planDate = planDate; 
    } 

    public void setPlanTitle(String planTitle) { 
     this.planTitle = planTitle; 
    } 

    public String getPlanTitle() { 
     return planTitle; 
    } 

    public void setPlanType(String planType) { 
     this.planType = planType; 
    } 

    public String getPlanType() { 
     return planType; 
    } 

    public void setPlanDate(String planDate) { 
     this.planDate = planDate; 
    } 

    public String getPlanDate() { 
     return planDate; 
    } 
} 

マイonCreateView

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_training, container, false); 

    preparePlansData(); 

    return view; 
} 

マイpreparePlansData()

private void preparePlansData() { 

    // Set plan data 
    Plans plan = new Plans("123", "fkfjfjeje", "21.04.1977"); 
    // Add Object to list 
    planList.add(plan); 

    plan = new Plans("test", "tttt", "22.01.2017"); 
    planList.add(plan); 

    // Notify data changes 
    pAdapter.notifyDataSetChanged(); 

} 

私の質問は、私は私のアダプタに文字列配列のうち、情報を得ることができる方法ですか?追加する前に日付をフォーマットする必要もあります。ご協力いただきありがとうございます!その後

http://guides.codepath.com/android/leveraging-the-gson-library

あなたがそのようなコードを書くことができるようになります:

Type type = new TypeToken<Map<Integer, Plans>>(){}.getType(); 
Map<Integer, Plans> myMap = gson.fromJson("your json from db", type); 

をして、このmap.values()を使用し、あなたのアダプタでは約Gsonここで読む

+0

このチュートリアルでは、https://developer.androidをあなたを助ける必要があります。com/training/material/lists-cards.html –

+0

最初のビューで本当に役に立たない。配列からデータを取り出し、RecyclerViewのエントリを作成する必要があります。 –

+0

答えはありますか?私はこれが必要です... –

答えて

1

Plansクラスは次のようになります。

class Plans { 
    String order_number; 
    String typ_first; 
    String typ_last; 
    String order_date_time; 
} 

あなたが別のフィールド名をしたい場合は、@SerializedName注釈

最後に、あなたは(構文は100%になりましIDEを開いていない場合、私は知らない)、そのようなことを書くべきを使用する必要があります。

private void preparePlansData() { 
    String plansData = plansPreferenceData.getString("plansPreferenceData", ""); 
    Type type = new TypeToken<Map<Integer, Plans>>(){}.getType(); 
    Map<Integer, Plans> myMap = gson.fromJson(plansData, type); 
    planList.addAll(myMap.values()); 

    // Notify data changes 
    pAdapter.notifyDataSetChanged(); 
} 

とモデルクラスを変更します。

public class Plans { 
    @SerializedName("order_number") 
    String planTitle; 
    @SerializedName("typ_last") 
    String planType; 
    @SerializedName("order_date_time") 
    String planDate; 
    .... 

私はそれはヨーヨーを助けることを願っていますu。このコード(あなたは@Test注釈を削除することができます)

+0

しかし、どのように異なる情報を抽出するのですか?この配列には2つのオブジェクトがありますので、2つの異なる行があるはずです –

+0

ここで 'Map'を使用してください:http://stackoverflow.com/a/15943171/2318843 – RadekJ

+0

良いですね。あなたは私の文字列から1つの値の例を教えてもらえますか?結果量があなたが投稿したソリューションのように静的でないことを忘れないでください –

0

チェック:

class Plans { 
    String typ_firstString; 
    String typ_last; 
    String order_date_time; 

    public Plans(String typ_firstString, String typ_last, String order_date_time) { 
     this.typ_firstString = typ_firstString; 
     this.typ_last = typ_last; 
     this.order_date_time = order_date_time; 
    } 
} 

class PlansResponse { 
    boolean status; 
    List<Plans> plans; 
} 

@Test 
public void testPlacesResponseDeserializer() { 
    Gson gson = new GsonBuilder() 
      .registerTypeAdapter(PlansResponse.class, new PlansResponseDeserializer()) 
      .create(); 
    String jsonString = "{\"success\":true,\"0\":{\"order_number\":\"078\",\"typ_first\":\"E3rft\",\"typ_last\":\"Split\",\"order_date_time\":\"2016-10-11 19:20:03\"},\"1\":{\"order_number\":\"166\",\"typ_first\":\"E483f\",\"typ_last\":\"Split_test\",\"order_date_time\":\"2016-10-12 18:39:30\"}}"; 
    PlansResponse plansResponse = gson.fromJson(jsonString, PlansResponse.class); 

    assert plansResponse.status == true; 
    assert plansResponse.plans.size() == 2; 
} 

class PlansResponseDeserializer implements JsonDeserializer<PlansResponse> { 

    private String getElementAsString(JsonObject jsonObject, String key) { 
     JsonElement element = jsonObject.get(key); 
     return element.getAsString(); 
    } 

    @Override 
    public PlansResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { 
     PlansResponse plansResponse = new PlansResponse(); 

     List<Plans> plansList = new ArrayList<>(); 
     Gson gson = new Gson(); 
     Type type = new TypeToken<Map<String, JsonObject>>(){}.getType(); 
     Map<String, JsonElement> map = gson.fromJson(json, type); 

     for(Map.Entry<String, JsonElement> entry : map.entrySet()) { 
      String key = entry.getKey(); 

      if("success".equals(key)) { 
       JsonPrimitive jsonPrimitive = (JsonPrimitive) entry.getValue(); 
       plansResponse.status = jsonPrimitive.getAsBoolean(); 
       continue; 
      } 

      JsonObject jsonObject = (JsonObject)entry.getValue(); 
      String typ_firstString = getElementAsString(jsonObject, "typ_first"); 
      String typ_last = getElementAsString(jsonObject, "typ_last"); 
      String order_date_time = getElementAsString(jsonObject, "order_date_time"); 
      plansList.add(new Plans(typ_firstString, typ_last, order_date_time)); 
     } 
     plansResponse.plans = plansList; 
     return plansResponse; 
    } 
} 
+0

日付この '2016-10-11 19:20:03'のように' '11.10.2016' –

+0

は' SimpleDateFormat'について読む:h ttps://www.mkyong.com/java/java-date-and-calendar-examples/ – RadekJ

+0

そして私もjsonの文字列でこれを行うことができますか? –

関連する問題