獲取

2017-02-03 68 views
0

我已經創建了一個輸入類像3個輸入地圖對象列表的數據:StringListList, 然後在RecyclerView使用它。
在我的構造函數中,我輸入的字符串列表中,則明確了,所以我不得不從mainExampleClass獲取

我怎樣才能訪問它讓我的數據?例如,列表,其中包含了例子列表

List<Example> exampleList; 
exampleList.get(0); 

我怎麼會get(i)後訪問數據?

的RecylerView數據對象:

public class Example { 
    private static MainExampleObject exampleObject; 
    private static String StepName; 
    private static List<String> TemporaryCode = new ArrayList<>(), TemporaryExplanation = new ArrayList<>(); 

    public Example(MainExampleObject exampleObject) { 
     this.exampleObject = exampleObject; 
    } 

    public static void addCode(String code) { 
     TemporaryCode.add(code); 
    } 

    public static void addExplanation(String explanation) { 
     TemporaryExplanation.add(explanation); 
    } 

    public static void setStepName(String stepName) { 
     StepName = stepName; 
    } 

    public static MainExampleObject getExampleObject() { 
     return exampleObject; 
    } 

    static List<String> getTemporaryCode() { 
     return TemporaryCode; 
    } 

    static List<String> getTemporaryExplanation() { 
     return TemporaryExplanation; 
    } 

    static String getStepName() { 
     return StepName; 
    } 

    public static void addExample(){ 
     exampleObject = new MainExampleObject(StepName, TemporaryCode, TemporaryExplanation); 
     TemporaryCode.clear(); 
     TemporaryExplanation.clear(); 
    } 

} 

示例對象類:

class MainExampleObject { 
    private static String StepName; 
    private static List<String> Code, Explanation; 

    MainExampleObject(String stepHeader, List<String> code, List<String> explanation) { 
     StepName = stepHeader; 
     Code = code; 
     Explanation = explanation; 
    } 

    public static String getStepNamex() { 
     return StepName; 
    } 

} 

更多詳細信息

通過該方法1中的數據添加到列表中

public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
       Example.setStepName(String.valueOf(dataSnapshot.getKey())); 

       for (DataSnapshot childSnapshot : dataSnapshot.child("Code").getChildren()) { 
        Example.addCode(String.valueOf(childSnapshot.getValue())); 
       } 

       for (DataSnapshot childSnapshot : dataSnapshot.child("Explaination").getChildren()) { 
        Example.addExplanation(String.valueOf(childSnapshot.getValue())); 
       } 
       addExample(); 
       exampleList.add(new Example(getExampleObject())); 
       adapter.notifyDataSetChanged(); 
      } 

適配器

List<Example> exampleList; 
    ViewLesson viewLesson; 

    public interface OnItemSelectedListenerCustom { 
     void onItemClicked(int selectedPosition); 
    } 

    public class ExampleHolder extends RecyclerView.ViewHolder { // here is where you define what text have value 
     CardView cv; 
     LinearLayout ll; 

     public ExampleHolder(View itemView) { 
      super(itemView); 
      cv = (CardView) itemView.findViewById(R.id.CV); 
      ll = (LinearLayout) itemView.findViewById(R.id.CV_LL); 
     } 
    } 

    public ExampleAdapter(ViewLesson viewLesson, List<Example> exampleList) { 
     this.viewLesson = viewLesson; 
     this.exampleList = exampleList; 
    } 


    @Override 
    public ExampleHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.card_view, parent, false); 
     return new ExampleHolder(itemView); 
    } 

    @Override 
    public void onBindViewHolder(final ExampleHolder holder, int position) { 

     TextView tv = new TextView(holder.cv.getContext()); 
     if (Example.getStepName() != null) { 
      tv.setText(Example.getStepName()); 
      holder.ll.addView(tv); 
     } 

     if (Example.getTemporaryCode() != null && Example.getTemporaryExplanation() != null) { 
      int i = 0; 
      for (String code : Example.getTemporaryCode()) { 
       tv = new TextView(holder.cv.getContext()); 
       tv.setText(code); 
       holder.ll.addView(tv); 

       tv = new TextView(holder.cv.getContext()); 
       tv.setText(Example.getTemporaryCode().get(i)); 
       holder.ll.addView(tv); 
       i++; 
      } 
      tv = new TextView(holder.cv.getContext()); 
      tv.setText(String.valueOf(exampleList.get(0).getClass().toString())); 
      holder.ll.addView(tv); 

      tv = new TextView(holder.cv.getContext()); 
      tv.setText(String.valueOf(exampleList.get(1).getClass().toString())); 
      holder.ll.addView(tv); 
     } 
    } 

    @Override 
    public int getItemCount() { 
     return exampleList.size(); 
    } 
} 

這是我想在單獨

StepName = 2 Adding b, TemporaryCode = [1aaaa, 2baaa, 3caaa], TemporaryExplanation = [1sttt, 2nddd, 3rddd] 

數據的這個例子

  • 字符串爲2 Adding b
  • 完全相同線第一份名單是[1aaaa, 2baaa, 3caaa]
  • 第二個名單是[1sttt, 2nddd, 3rddd]

tv.setText(String.valueOf(exampleList.get(0).getClass().toString())); 
+0

請看一下[mcve]。回收站在哪裏?適配器在哪裏?你需要哪些數據?你明白'靜態'實際上做了什麼嗎? –

+1

爲什麼你有這麼多的靜態方法?我建議你花幾分鐘時間來了解「靜態」的含義。 –

+0

@ cricket_007很抱歉,我認爲我發佈的代碼足以解決問題,並且我在帖子中添加了更多詳細信息,請參閱 –

回答

3

基於這一行似乎要在類的所有變量的字符串表示。

exampleList.get(0).getClass().toString() 

好,getClass()回報你一個Java類變量和toString上Class並不能說明它的領域。

請參閱How to override toString() properly in Java?和後把它應用到你的類,你解決你做任何認爲你需要static到處

如果做得正確,這會工作。

setText(String.valueOf(exampleList.get(0))) 
+0

非常感謝你,我會試試這個,如果你添加了另一個不使用setter和getter的靜態引用,那將會很好 –

+1

靜態值稱爲類變量。它們由所有實例共享http://stackoverflow.com/a/16686545/2308683 –