0

I have created an input class like 3 inputs Map: String, List, List, and then use it in a RecyclerView.
In my constructor, I input strings in the list then clear it, so I have to get my data from the mainExampleClass

How can I access it? For instance, in the List which contains the Examples List

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

how could I access the data after get(i)?

The RecylerView Data object:

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();
    }

}

The example object class:

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;
    }

}

More Details

The method by which I add the data to the list

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();
            }

The Adapter

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();
    }
}

This is the exactly line that I want to get this example of data in separately

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

where

  • the string is 2 Adding b
  • the 1st List is [1aaaa, 2baaa, 3caaa]
  • the 2nd List is [1sttt, 2nddd, 3rddd]

The Line

tv.setText(String.valueOf(exampleList.get(0).getClass().toString()));
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Dasser Basyouni
  • 3,142
  • 5
  • 26
  • 50

1 Answers1

3

Based on this line seems you want a String representation of all the variables in your class.

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

Well, getClass() returns you a Java Class variable, and toString on a Class tells nothing about its fields.

Please see How to override toString() properly in Java? and apply it to your class after you fix whatever you did to think you needed static everywhere

If done correctly, this would work.

 setText(String.valueOf(exampleList.get(0))) 
Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • thank you so much, I will try this, also if you added another reference of not using the static in setters and getter that will be nice – Dasser Basyouni Feb 03 '17 at 07:37
  • 1
    static values are called class variables. They are shared by all instances http://stackoverflow.com/a/16686545/2308683 – OneCricketeer Feb 03 '17 at 07:39