Firebase商店成分为数组

时间:2017-09-21 07:28:44

标签: android arrays list listview firebase

我需要用firebase制作配方应用程序。我设法将所有文件上传到firebase但无法上传数组列表。这是我的firebase在上传数据时的样子: firebase structure

有成分的字段说android.widget.ArrayAdapter@374bb。这是我的上传数据代码:

PostActivity.java

public class PostActivity extends AppCompatActivity {

    private ImageButton mSelectImage;
    private EditText mPostTitle;
    private EditText mPostDesc;
    private Button mSubmitBtn;

    private EditText editTxt;
    private Button btn;
    private ListView list;
    private ArrayAdapter<String> adapter;
    private ArrayList<String> arrayList;

    private Uri mImageUri = null;

    private static final int GALLERY_REQUEST = 1;

    private StorageReference mStorage;
    private DatabaseReference mDatabase;

    private ProgressDialog mProgress;

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

        mStorage = FirebaseStorage.getInstance().getReference();
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");

        mSelectImage = (ImageButton) findViewById(R.id.imageSelect);

        mPostTitle = (EditText) findViewById(R.id.titleField);
        mPostDesc = (EditText) findViewById(R.id.descField);
        mSubmitBtn = (Button) findViewById(R.id.submitBtn);

        editTxt = (EditText) findViewById(R.id.ingField);
        btn = (Button) findViewById(R.id.addBtn);
        list = (ListView) findViewById(R.id.list);
        arrayList = new ArrayList<String>();

        adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList);


        list.setAdapter(adapter);

        mProgress = new ProgressDialog(this);



        mSelectImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
                galleryIntent.setType("image/*");
                startActivityForResult(galleryIntent, GALLERY_REQUEST);
            }
        });

        mSubmitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startPosting();
            }
        });


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                arrayList.add(editTxt.getText().toString());

                adapter.notifyDataSetChanged();
            }
        });
    }

    private void startPosting(){

        mProgress.setMessage("Posting to Blog...");

        final String title_val = mPostTitle.getText().toString().trim();
        final String desc_val = mPostDesc.getText().toString().trim();
        final String ingList_val = list.getAdapter().toString().trim();

        if (!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(desc_val) && !TextUtils.isEmpty(ingList_val) && mImageUri !=null){
            mProgress.show();

            StorageReference filepath = mStorage.child("Blog_Images").child(mImageUri.getLastPathSegment());


            filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                    Uri downloadUrl = taskSnapshot.getDownloadUrl();

                    DatabaseReference newPost = mDatabase.push();


                    newPost.child("title").setValue(title_val);
                    newPost.child("desc").setValue(desc_val);
                    newPost.child("ingList").setValue(ingList_val);
                    newPost.child("image").setValue(downloadUrl.toString());


                    mProgress.dismiss();

                    startActivity(new Intent(PostActivity.this, MainActivity.class));
                }
            });
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){

            mImageUri = data.getData();

            mSelectImage.setImageURI(mImageUri);
        }
    }
}

当我运行程序时,这是我添加项目的方式: item add

将成分逐一写入配料领域,添加到清单,最后提交全部。如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

当您使用以下代码行时:

final String ingList_val = list.getAdapter().toString().trim();

您没有获得所需的ArrayListlist.getAdapter()返回 ListAdapter ,而不是ArrayList。注意,android.widget.ArrayAdapter@374bb不是它自己的对象,只是对象从内存中的引用,因此toString()方法返回该引用。

因此,当使用以下代码行时:

newPost.child("ingList").setValue(ingList_val);

您正在参与该引用的子ingList。为了解决这个问题,您可以将ArrayList设置为自己,但我不会重新执行此操作。由于Firebase数据库是JSON数据库,并且所有内容都是按键和值对组织的,因此我使用Map重新命名您。

希望它有所帮助。