使用Parse保存详细信息

时间:2016-01-19 08:31:09

标签: android parse-platform

我无法在parse.com仪表板中查看已保存的数据。其次,如何使用解析和适配器检索数据并存储在recyclerview中。如果代码有误。提前谢谢 Mainactivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.parse.Parse;
import com.parse.ParseObject;
import parse.example.com.parseregistration.R;

public class MainActivity extends AppCompatActivity
{
    EditText firstName,lastName,age,dob;
    Button saveOne,saveTwo,showDetails;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        firstName=(EditText)findViewById(R.id.fn_Input);
        lastName=(EditText)findViewById(R.id.ln_Input);
        age=(EditText)findViewById(R.id.age_input);
        dob=(EditText)findViewById(R.id.dob_Input);
        saveOne=(Button)findViewById(R.id.saveBtn);
        saveTwo=(Button)findViewById(R.id.saveBtn2);
        showDetails=(Button)findViewById(R.id.sdBtn);
        final ParseObject user=new ParseObject("userdata");
        Parse.enableLocalDatastore(this);
        Parse.initialize(this);
        saveOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                user.put("firstname",firstName.getText().toString());
                user.put("lastname",lastName.getText().toString());
                user.put("age",age.getText().toString());
                user.put("dob",dob.getText().toString());
                user.saveInBackground();
                Toast.makeText(getApplicationContext(),"SAVED!",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Userdata.java

public class UserData1
{
    private String firstname;
    private String lastname;
    private int age;
    private String dob;

    public UserData1()
    {
        //Default Constrcuctor
    }
    public UserData1(String firstname,String lastname,String age,String dob)
    {
        firstname=firstname;
        lastname=lastname;
        age=age;
        dob=dob;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getDob() {
        return dob;
    }

    public void setDob(String dob) {
        this.dob = dob;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    public String toSreing()
    {
        return firstname+"\t"+lastname+"\t"+age+"\t"+dob;
    }
}

MyRecyclerView.java

import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.msit.example.bunty.regform.R;
import com.msit.example.bunty.regform.entities.DataStore;
import com.parse.ParseObject;
import com.parse.ParseQuery;


public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder>
{
    ParseQuery<ParseObject> parseQuery;
    //ArrayList<UserData> userDataStore=new ArrayList<UserData>();
    private MyRecyclerAdapter myRecyclerAdapter=this;
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view,parent,false);
        ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }

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

       /*holder.fNameView.setText(userDataStore.getDataStore().get(position).getFirstName());
        holder.lNameView.setText(userDataStore.getDataStore().get(position).getLastName());
        holder.addressView.setText(userDataStore.getDataStore().get(position).getAddress());
        holder.cityView.setText(userDataStore.getDataStore().get(position).getCity());
        final String city=userDataStore.getDataStore().get(position).city;
        */
        holder.cityView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(),city,Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        if(userDataStore==null)
        {
            return 0;
        }
        if(!userDataStore.getDataStore().isEmpty()&&userDataStore.getDataStore()!=null)
        {
            return userDataStore.getDataStore().size();
        }
        else
        {
            return 0;
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

         TextView fNameView;
         TextView lNameView;
         TextView addressView;
         Button cityView;
         CardView cardView;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);
            fNameView = (TextView) itemView.findViewById(R.id.fn_view);
            lNameView = (TextView) itemView.findViewById(R.id.ln_view);
            addressView = (TextView) itemView.findViewById(R.id.addr_view);
            cityView=(Button)itemView.findViewById(R.id.city_view);
            cardView = (CardView) itemLayoutView.findViewById(R.id.card_list);
        }
    }
}`

2 个答案:

答案 0 :(得分:0)

对于初学者来说,不要让你的解析对象最终,这使得它不可变,这意味着它不能被修改或添加...而且,我认为最好的做法是将解析初始化的东西放在应用程序类中,否则你的每次活动开始时重新焕发活力

答案 1 :(得分:0)

以下是您重构的代码,我建议您不要在Parse内进行Adapter次查询。最好在Activity内的某个地方执行此操作,并在保留数据后,将这些值传递给MyRecyclerAdapter

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.msit.example.bunty.regform.R;

import java.util.ArrayList;


public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder>
{
    //ParseQuery<ParseObject> parseQuery;
    private LayoutInflater inflater;
    ArrayList<UserData> userDataStore = new ArrayList<UserData>();
    private MyRecyclerAdapter myRecyclerAdapter=this;

    public MyRecyclerAdapter(Context context, ArrayList<UserData> data) {
        inflater = LayoutInflater.from(context);
        userDataStore = data;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View view = inflater.inflate(R.layout.listitem_category,parent,false);
        ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }

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

       /*holder.fNameView.setText(userDataStore.getDataStore().get(position).getFirstName());
        holder.lNameView.setText(userDataStore.getDataStore().get(position).getLastName());
        holder.addressView.setText(userDataStore.getDataStore().get(position).getAddress());
        holder.cityView.setText(userDataStore.getDataStore().get(position).getCity());
        final String city=userDataStore.getDataStore().get(position).city;
        */
        holder.cityView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(),city,Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        if(userDataStore==null)
        {
            return 0;
        }
        if(userDataStore !=null && !userDataStore.isEmpty())
        {
            return userDataStore.size();
        }
        else
        {
            return 0;
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView fNameView;
        TextView lNameView;
        TextView addressView;
        Button cityView;
        CardView cardView;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);
            fNameView = (TextView) itemView.findViewById(R.id.fn_view);
            lNameView = (TextView) itemView.findViewById(R.id.ln_view);
            addressView = (TextView) itemView.findViewById(R.id.addr_view);
            cityView=(Button)itemView.findViewById(R.id.city_view);
            cardView = (CardView) itemLayoutView.findViewById(R.id.card_list);
        }
    }
}

这是如何从Parse保留数据的简单示例,将这些代码放在MainActivity内的某处:

ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("userdata");
        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> list, ParseException e) {
                if (e == null) {
                    ArrayList<UserData> userDataList = new ArrayList<UserData>();
                    UserData userData;
                    for (ParseObject object : list) {
                        userData = new UserData();
                        userData.setFirstName(object.get("firstname"));
                        userData.setLastName(object.get("lastname"));
                        //...
                        userDataList.add(userData);
                    }
                    MyRecyclerAdapter adapter = new MyRecyclerAdapter(MainActivity.this, userDataList);
                    // instance of RecyclerView declared somewhere in your Activity
                    recyclerView.setAdapter(adapter);
                } else {
                    // Something went wrong, see exception
                }
            }
        });

顺便说一下。 here你可以找到很好解释的Android开发指南:D