Chào các bạn mình đang thực hành cập nhật dữ liệu ListView từ 1 mảng là các đối tượng Employee. Code của mình như sau:
Mã:
package com.example.androidstudy.androidstudy; import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.EditText;import java.util.ArrayList;import android.view.View;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ListView;import android.widget.RadioGroup; public class ListViewObjectActivity extends ActionBarActivity { EditText EmployeeId,EmployeeName; Button btnEnterNV; RadioGroup radgroup; ListView listViewNhanVien; ArrayList<Employee>arrEmployee=null; ArrayAdapter<Employee>adapter=null; //Khai báo 1 employee object Employee employee=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_view_object); EmployeeId =(EditText) findViewById(R.id.editTxtEmployeeId); EmployeeName=(EditText) findViewById(R.id.editTxtEmployeeName); btnEnterNV=(Button) findViewById(R.id.btnEnterNV); radgroup=(RadioGroup) findViewById(R.id.radgroup1); listViewNhanVien=(ListView) findViewById(R.id.listViewNhanVien); arrEmployee = new ArrayList<Employee>(); //đưa Data Source là các employee vào Adapter adapter = new ArrayAdapter<Employee>(this,android.R.layout.simple_list_item_1,arrEmployee); //đưa adapter vào ListView listViewNhanVien.setAdapter(adapter); btnEnterNV.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { processNhap(); } }); } //Xử lý sự kiện nhập public void processNhap(){ //Lấy ra đúng id của Radio Button được checked int radId = radgroup.getCheckedRadioButtonId(); String id = EmployeeId.getText() + ""; String name = EmployeeName.getText() + ""; if(radId == R.id.radFullTime) { //tạo instance là FullTime employee = new EmployeeFullTime(); } else{ //Tạo instance là Partime employee = new EmployeePartTime(); } //FullTime hay Partime thì cũng là Employee /ên có các hàm này là hiển nhiên employee.setId(id); employee.setName(name); //Đưa employee vào ArrayList arrEmployee.add(employee); //Cập nhật giao diện adapter.notifyDataSetChanged(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_list_view_object, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); /oinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
Ko hiểu sao khi nhập Id và tên NV và nhấn button btnEnterNV lần thứ nhất thì ListView cập nhật được dữ liệu. Mình muốn nhập thêm id và tên NV thứ 2 thì ListView ko cập nhật được (Mình có debug thì thấy biến arrEmployee chứ 2 nhân viên)
- - - Nội dung đã được cập nhật ngày 20-01-2015 lúc 10:55 AM - - -
Mình làm đc rồi do mình chỉnh độ cao chỉ đủ hiển thị 1 mẫu tin nên ko hiển thị đc mẫu tin thứ 2
View more random threads:
Trứng rung điều khiển từ xa Vibrator Spark Of Love Sản phẩm trứng rung bluetooth có điều khiển từ xa này được thiết kế với tông màu Rose Gold viền kim khí trải qua. kích tấc 7,4cm x 3,5cm giúp trứng...
TOP 3 trùy rung điều khiển từ xa...