没有用于警告Firebase数据库检索数据的setter /字段填充Listview

时间:2016-06-18 23:48:32

标签: android firebase firebase-realtime-database

我只是尝试将Firebase数据库中的数据填充到我的列表视图中。日志显示正在检索数据,但适配器不会将值设置为列表中单个列表项中的文本?它只是说" INSERT VALUE "没有setter / field。这让我觉得我没有正确设置我的二传手,但Android Studio自动生成了这些。我不知道我在这里失踪了什么。任何帮助表示赞赏。

NODE OBJECT

package com.megliosolutions.ipd.Objects;

import android.graphics.Bitmap;

/**
 * Created by Meglio on 6/13/16.
 */
public class NodeObject {


public String mStaticAddress;
public String mLat;
public String mLong;

public NodeObject(){
    //needed for firebase
}

public NodeObject(String address, String lat, String Long){
    this.mStaticAddress = address;
    this.mLat = lat;
    this.mLong = Long;
}

public String getmStaticAddress() {
    return mStaticAddress;
}

public void setmStaticAddress(String mStaticAddress) {
    this.mStaticAddress = mStaticAddress;
}

public String getmLat() {
    return mLat;
}

public void setmLat(String mLat) {
    this.mLat = mLat;
}

public String getmLong() {
    return mLong;
}

public void setmLong(String mLong) {
    this.mLong = mLong;
}
}

STATIC LISTADAPTER

/**
 * Created by Meglio on 6/14/16.
 */
public class StaticListAdapter extends ArrayAdapter<NodeObject> {
public static String TAG = StaticListAdapter.class.getSimpleName();
public Context mContext;
public List<NodeObject> mNodes;

public class ViewHolder {
    TextView mStaticAddress;
    TextView mLAT;
    TextView mLONG;

}

@Override
public int getCount() {
    return mNodes.size();
}

public StaticListAdapter(Context context, List<NodeObject> objects) {
    super(context, R.layout.activity_main, objects);
    this.mContext = context;
    this.mNodes = objects;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = new ViewHolder();
    NodeObject node = mNodes.get(position);
    if (convertView == null) {
        convertView = LayoutInflater.from(this.mContext).inflate(R.layout.node_item, null);
        holder.mLONG = (TextView) convertView.findViewById(R.id.node_item_LONG);
        holder.mStaticAddress = (TextView) convertView.findViewById(R.id.node_item_IP);
        holder.mLAT = (TextView) convertView.findViewById(R.id.node_item_LAT);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.mStaticAddress.setText(node.getStaticAddress());
    holder.mLONG.setText(node.getLongitude());
    holder.mLAT.setText(node.getLatitude());

    return convertView;
}
}

MAINACTIVITY

public class MainActivity extends AppCompatActivity {

public static String TAG = MainActivity.class.getSimpleName();

public ListView main_ListView;
public FirebaseAuth mAuth;
public FirebaseUser mUser;
public DatabaseReference mDatabase;

//Strings
public String static_ip;
public String lat = "5.0";
public String mLong = "4.0";
public String currentUser;

//Adapters
public StaticListAdapter listAdapter;

//Node Object
NodeObject node;
public List<NodeObject> nodesList;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Instances
    mAuth = FirebaseAuth.getInstance();
    mUser = mAuth.getCurrentUser();
    mDatabase = FirebaseDatabase.getInstance().getReference();
    currentUser = mUser.getUid();

    main_ListView = (ListView)findViewById(R.id.Main_listview);

    //Toolbar
    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    //[End of Toolbar]
    nodesList = new ArrayList<>();

    retrieveData();

    listAdapter = new StaticListAdapter(getApplicationContext(),nodesList);

    main_ListView.setAdapter(listAdapter);


    Log.i(TAG, "USER: " + currentUser);


}

private void retrieveData() {
    mDatabase.child("nodes").child(mUser.getUid())
            .addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                    Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());

                    NodeObject nodeObject = dataSnapshot.getValue(NodeObject.class);

                    listAdapter.add(nodeObject);
                    listAdapter.setNotifyOnChange(true);
                }

                @Override
                public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onChildRemoved(DataSnapshot dataSnapshot) {

                }

                @Override
                public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.getGPS:
           //nothing
            return true;
        case R.id.addNode:
            addNode();
            return true;
        case R.id.logout:
            signOut();
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}





private void signOut() {
    mAuth.signOut();
    Intent intent = new Intent(MainActivity.this, Login.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    Toast.makeText(getApplicationContext(), "Logging Out.", Toast.LENGTH_SHORT).show();
    startActivity(intent);

}

private void addNode() {
    //AlertDialog
    final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

    dialogBuilder.setTitle("Dude, assign something...");

    LayoutInflater inflater = this.getLayoutInflater();

    View dialogView = inflater.inflate(R.layout.main_add_node_dialog, null);

    dialogBuilder.setView(dialogView);

    final EditText editText = (EditText)
            dialogView.findViewById(R.id.static_et);
    dialogBuilder.setPositiveButton("Assign", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            static_ip = editText.getText().toString();
            String ip = static_ip;
            node = new NodeObject(ip, lat, mLong);
            mDatabase.child("nodes").child(currentUser).push().setValue(node);
            Toast.makeText(getApplicationContext(), "Static IP: " + static_ip + " assigned!"
                    , Toast.LENGTH_SHORT).show();
        }
    }).
            setNegativeButton("Or Not...", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getApplicationContext(), "Fine, nvm then..."
                            , Toast.LENGTH_SHORT).show();
                }
            });

    dialogBuilder.create().show();
}




}

调试显示我能够获得客户端的所有内容。日志也显示了这一点。

06-18 18:25:42.981 12962-12962/com.megliosolutions.ipd D/MainActivity: zHF4TGnRvkeXEbKiLegiUNLGHX12:{-KKLeBAe9pV1Umm3qQMo={mStaticAddress=26161910494949, mLong=3.0, mLat=2.0}, -KKG_ACFvdX7aJOR98-o={mStaticAddress=10.223.22.250, mLong=3.0, mLat=2.0}, -KKWKMZS7WkE_xWbL3rC={mStaticAddress=, mLong=4, mLat=5}, -KKQQLITf9-7iMFlqEWR={mStaticAddress=123123123123, mLong=3.0, mLat=2.0}, -KKG_J6PKwogjBFdk52Z={mStaticAddress=10.333.555.888, mLong=3.0, mLat=2.0}}

enter image description here

更新

对我来说没有意义的部分,但我确信对那些了解它的人有意义。正在理解我在阅读firebase文档的内容。我读了几遍,但我猜它不是点击。我发现构建firebase中的数据是 KEY 。如果没有它,你就无法正确编码,因为一切都依赖于此。 Firebase 让我可以轻松使用,因为我看到它正常运行。我将在此发表一篇博文来解释我的麻烦以及如何超越它们,以免其他人遇到这个烂摊子。下面的代码将使用firebase后端构建功能列表视图!

public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                    Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());

                    NodeObject nodeObject = dataSnapshot.getValue(NodeObject.class);

                    listAdapter.add(nodeObject);
                    listAdapter.setNotifyOnChange(true);
                }

4 个答案:

答案 0 :(得分:6)

retrieveData()中,您应该使用ChildEventListener,而不是ValueEventListener。您正在使用Node生成的密钥将node/$uid个对象添加为push()的子级。 ValueEventListener正在返回包含所有Map<String,Object>的{​​{1}}。这会显示在您发布的Nodes输出中。您可以使用logcat的{​​{1}}回调来获取每个onChildAdded(),然后将其添加到您的适配器。

答案 1 :(得分:6)

警告是因为您的字段和您的设置者之间的外壳不匹配。

按照Firebase文档中的示例,这似乎是建模Java类的更好方法:

public class NodeObject {
    public String staticAddress;
    public String lat;
    public String lon;

    public NodeObject(){
        //needed for firebase
    }

    public NodeObject(String address, String lat, String lon){
        this.staticAddress = address;
        this.lat = lat;
        this.lon = lon;
    }
}

作为额外的奖励,这将导致更合理的JSON属性名称。

答案 2 :(得分:3)

我刚才遇到同样的问题&amp;答案非常令人沮丧。

我认为问题在于您的命名约定。例如,看起来您命名变量mLat,然后您的访问者/变更者是getmLat()setmLat()。我认为当Firebase进行反序列化时,它们依赖于某些命名。例如,如果您有一个名为lat的成员变量,则需要getLat()setLat()

对于您的情况,您可能只能将方法更改为getMLat()setMLat()。虽然我建议稍微改变你的约定。

作为旁注,请注意您的命名通常违反惯例。通常,以m为前缀的变量是私有成员变量,访问权限和&amp;公共方法(如你的)突变。

答案 3 :(得分:0)

由于访问错误的变量(父变量)而出现错误

public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()){
                   list.add(snapshot.getValue(CategoryModel.class));
                   // Here in list I should have added dataSnapshot.getValue(CategoryModel.class)
               }

            
}

可能有帮助。