我在使用自定義Adapter
刷新我的Spinner
時遇到問題。自定義適配器顯示「名稱」,但可以引用所有對象數據(例如「ID」,「年齡」)。使用自定義對象在Asyctask後刷新微調器
我使用Asynctask將在線數據導入爲JSON並將它們存儲在SQLite數據庫中。
在我的MainActivity
,我有一個Spinner
自定義對象。 現在我嘗試了很多,並更新SQLite數據庫'onPostExecute'後更新spinneradapter失敗。
我將跳過正在工作的代碼的某些部分,不應該影響(導入,獲取數據,數據庫內容等)以保持清潔。
這裏是我的班
users.java
package domain.project;
public class user{
private String name;
private int age;
private long id;
public User(String name, int age, long id) {
this.name = name;
this.age= age;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Override
public String toString() {
String output = name;
return output;
}
}
UserDataSource.java
package domain.project;
// imports skipped
public class UserDataSource {
private SQLiteDatabase database;
public UserDataSource(Context context) {
}
public void open() {
//Open database stuff
}
public void close() {
//closing database stuff
}
public User createUser(String name, int age) {
//writing users into SQLitedatabase stuff
cursor.moveToFirst();
User user = cursorToUser(cursor);
cursor.close();
return user;
}
public void deleteUser(User user) {
//delete user from SQLitedatabase
}
public void refreshUser() {
GetUsersTask getUsersTask = new GetUsersTask();
getUsersTask.execute("Users");
}
public User updateUser(long id, String newName, int newAge) {
//update User in SQLitedatabase
}
private User cursorToUser (Cursor cursor) {
//getting User from SQLite Database
User user = new User(name, age, id);
return user;
}
public User[] allUsers() {
{
//filling Array with all Users from SQLitedatabase
}
return allUsers;
}
public class GetUsersTask extends AsyncTask<String, Integer, String[]> {
private String[] readJsonUser(String jsonString) {
try {
JSONArray jArray = new JSONArray(jsonString);
String[] returnArray = new String[jArray.length()];
//parsing JSON String to Array
}
return returnArray ;
} catch (final JSONException e) {
}
return null;
}
@Override
protected String[] doInBackground(String... strings) {
if (strings.length == 0) {
return null;
}
// grabbing JSON string from web and store it in userJsonString
return readJsonUser(userJsonString);
}
@Override
protected void onProgressUpdate(Integer... values) {
}
@Override
protected void onPostExecute(String[] strings) {
if (strings != null) {
//write and update SQLite database
}
}
}
}
}
MainActivity.java
package domain.project;
//skipping the imports
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataSource = new UserDataSource(this);
}
@Override
protected void onResume() {
super.onResume();
showAllListEntries();
}
@Override
protected void onPause() {
super.onPause();
dataSource.close();
}
public void showAllListEntries() {
final Spinner sqlUserSpinner = (Spinner) findViewById(R.id.spinner_user);
final SpinAdapter userSpinAdapter = new SpinAdapter(this, android.R.layout.simple_spinner_dropdown_item, dataSource.allUsers());
usersSpinner.setAdapter(userSpinAdapter);
usersSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView,
View view, int i, long l) {
// Testing the selected Item
User user = userSpinAdapter.getItem(i);
Toast.makeText(MainActivity.this, "Selected : " + user.getId() + "<-ID age->" + user.getAge(), Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class SpinAdapter extends ArrayAdapter<User>{
private Context context;
private User[] values;
public SpinAdapter(Context context, int textViewResourceId,
User[] values) {
super(context, textViewResourceId, values);
this.context = context;
this.values = values;
}
public int getCount(){
return values.length;
}
public SQLShot getItem(int position){
return values[position];
}
public long getItemId(int position){
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView label = new TextView(context);
label.setTextColor(Color.BLACK);
label.setText(values[position].getName());
return label;
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
TextView label = (TextView) super.getView(position, convertView, parent);
label.setTextColor(Color.BLACK);
label.setText(values[position].getName());
return label;
}
}
}