简体   繁体   English

getValue setter不存储值

[英]getValue setter not storing value

I am using firebase and passing the values to a listview 我正在使用Firebase并将值传递给ListView

to pass the value to the listview i use the following 将值传递给listview我使用以下

requestList request = requestSnapshot.getValue(requestList.class);

i also need to store the key for the record, but it is not included within the getValue call. 我还需要存储记录的键,但是它不包含在getValue调用中。

I decided to write my own setter for the key and add it to the requestList class 我决定为密钥编写自己的setter并将其添加到requestList类

i am setting the key manually like this 我像这样手动设置密钥

request.setrequestKey(requestSnapshot.getKey());

when i print the values of request to the log directly after adding them i get the correct values 当我将请求的值添加后直接将其打印到日志中时,我得到正确的值

 Log.i("Chat", request.getRequestTitle()+": "+request.getRequestDescription()+"    "+request.getrequestKey());

returns this 返回这个

RequestTitle: RequestDescription    -KK4yufLTSScKHzvZtM4

i then wanted to use the request.getrequestkey within an onclicklistener like this 然后我想在像这样的onclicklistener中使用request.getrequestkey

 messagesView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
        requestList o = (requestList) messagesView.getItemAtPosition(position);

        String requestKey = o.getrequestKey();

        Toast.makeText(getActivity(),requestKey,Toast.LENGTH_SHORT).show();
    }
});

however the requestKey is null but the requestTitle and requestDescription is present and correct 但是requestKey为null,但requestTitle和requestDescription存在且正确

i cant understand why the class isnt retaining the value 我不明白为什么班级没有保留价值

can anyone help please 谁能帮忙吗

here is my requestList class 这是我的requestList类

public class requestList {

    String RequestTitle;
    String RequestDescription;
    String requestKey;

    //DataSnapshot requestSnapshot;


    public requestList() {
    }

    public requestList(String RequestTitle, String RequestDescription) {
        this.RequestTitle = RequestTitle;
        this.RequestDescription = RequestDescription;
        //this.key = key;

        //this.requestSnapshot = requestSnapshot;
    }

    public String getRequestTitle() {
        return RequestTitle;
    }

    public String getrequestKey() {
        return requestKey;
    }

    public String getRequestDescription() {
        return RequestDescription;
    }


    public void setrequestKey(String requestKey) {
        this.requestKey = requestKey;
    }
}

i am now including the full class as per comment requests 我现在根据评论要求包括全班

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        myView = inflater.inflate(R.layout.activity_request, container, false);

        final ListView messagesView = (ListView) myView.findViewById(listView);


        test mApp = (test) getActivity().getApplication();

        //test mApp = ((getView())getApplicationContext());
        String UID = mApp.getUID();

        DatabaseReference ref = FirebaseDatabase.getInstance().getReference("users/" + UID + "/Requests" );

        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                for (DataSnapshot requestSnapshot: snapshot.getChildren()) {
                    //requestList request = requestSnapshot.getValue(requestList.class);
                    requestList request = requestSnapshot.getValue(requestList.class);
                    request.setRequestKey(requestSnapshot.getKey());
                    Log.i("Chat", request.getRequestTitle()+": "+request.getRequestDescription()+"    "+request.getRequestKey());

                    }
            }

            @Override
            public void onCancelled(DatabaseError firebaseError) {
                Log.e("Chat", "The read failed: " + firebaseError.getMessage());
            }
        });



        ListAdapter mAdapter = new  FirebaseListAdapter<requestList>(getActivity(), requestList.class, android.R.layout.two_line_list_item, ref) {
            @Override
            protected void populateView(View view, requestList requestListItem, int position) {
                ((TextView)view.findViewById(android.R.id.text1)).setText(requestListItem.getRequestTitle());
                ((TextView)view.findViewById(android.R.id.text2)).setText(requestListItem.getRequestDescription());

            }


        };


        messagesView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                requestList o = (requestList) messagesView.getItemAtPosition(position);

                String requestKey = o.getRequestKey();

                Toast.makeText(getActivity(),requestKey,Toast.LENGTH_SHORT).show();
            }
        });


        messagesView.setAdapter(mAdapter);


        return myView;



    }
}

I am now including the database values 我现在包括数据库值

{
  "AccountID" : "ABC123",
  "Created" : "today",
  "Name" : "value",
  "Requests" : {
    "-KK4yufLTSScKHzvZtM4" : {
      "RequestDescription" : "Request Description 3",
      "RequestItems" : {
        "-KK4yv8qnh7YHe7WuJ8B" : {
          "Description" : "Item Descriptiopn",
          "Title" : "Item Title"
        },
        "-KK4yv8rPsnJr824rAON" : {
          "Description" : "Item Descriptiopn",
          "Title" : "Item Title"
        },
        "-KK4yv8s3ivK7Mfx166H" : {
          "Description" : "Item Descriptiopn",
          "Title" : "Item Title"
        },
        "-KK4yv8tKoM2sV1gyOkz" : {
          "Description" : "Item Descriptiopn",
          "Title" : "Item Title"
        }
      },
      "RequestTitle" : "value"
    }
  }
}

Since you're using FirebaseUI, you don't need to track the key yourself. 由于您使用的是FirebaseUI,因此您无需自己跟踪密钥。 You can easily get it with adapter.getRef(...).getKey() : 您可以使用adapter.getRef(...).getKey()轻松获得它:

messagesView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
    requestList o = (requestList) messagesView.getItemAtPosition(position);

    String requestKey = adapter.getRef(position).getKey();

    Toast.makeText(getActivity(),requestKey,Toast.LENGTH_SHORT).show();
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM