Monday, July 6, 2015
Truyền - Nhận dữ liệu đến Parse.com
Sau khi kết nối được với Parse, tiến hành gửi và nhận dư liệu từ parse về app của chúng ta trên android. Trong bài tập này mình sẽ tiến hành nhập tên công việc vào EditText rồi đưa lên parse, sau đó mình sẽ nhận dữ liệu mình đã nhập ở EditText về và hiện lên ListView trong App.
Hình minh họa:
Bắt đầu :
1. Các bạn có thể xem Video này để thực hiện
1. Các bạn có thể xem Video này để thực hiện
2. Các bạn xem source của bài tập này
Đây là file giao diện của bài tập
Đây là code trong file Java:
public class MainActivity extends ActionBarActivity { EditText edtTextJob; Button btnSave; ListView lvJob; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Parse.enableLocalDatastore(this); Parse.initialize(this, "WDBD1bPq13lqIH38ZijDWgGLLmzULsiHWB76S9UI", "b2tx1pGeABFKVHeeSa2F81tRsExXS5FBgkZVbs8w"); edtTextJob = (EditText) findViewById(R.id.editText_job); btnSave = (Button) findViewById(R.id.button_job); lvJob = (ListView) findViewById(R.id.listView_job); btnSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ParseObject job = new ParseObject("Job"); job.put("JobName", edtTextJob.getText().toString()); job.saveInBackground(new SaveCallback() { @Override public void done(ParseException e) { edtTextJob.setText(""); Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show(); ParseQuery<ParseObject> query = ParseQuery.getQuery("Job"); // query.whereEqualTo("playerName", "Dan Stemkoski"); query.findInBackground(new FindCallback<ParseObject>() { public void done(List<ParseObject> scoreList, ParseException e) { if (e == null) { ArrayList<String> arrayjob = new ArrayList<String>(); for (ParseObject j : scoreList) { arrayjob.add(j.getString("JobName")); } ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,arrayjob); lvJob.setAdapter(adapter); // Log.d("score", "Retrieved " + scoreList.size() + " scores"); } else { // Log.d("score", "Error: " + e.getMessage()); } } }); } }); } }); }