我有三个活动,第一个用于从数据库中保存一些值,第二个显示列表视图,当我单击该列表视图中的任何项目时,我将跳转到第三个活动,能够编辑我选择的项目,在更新数据库中的数据后,新数据将显示在第二个活动中,但是当我返回到第一个活动时,没有任何内容被刷新。如何在第一个活动中获取更新的数据太? 任何帮助将不胜感激。
第二个活动代码:
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class SetupActivity extends AppCompatActivity {
private Context context;
static String extra = "values";
ListView mListView;
final String[] setItems = {"name","birthday","type","sex"};
@Override
public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.setuplist);
mListView = (ListView)findViewById(R.id.listview);
context = this;
updateView();
}
private void updateView(){
DogDatabaseHelper dbHelper= new DogDatabaseHelper(getApplicationContext());
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("Dog",null,null,null,null,null,null,null);
if(cursor.moveToFirst()){
String name = cursor.getString(cursor.getColumnIndex("name"));
String birthday = cursor.getString(cursor.getColumnIndex("birthday"));
String size = cursor.getString(cursor.getColumnIndex("size"));
String sex = cursor.getString(cursor.getColumnIndex("sex"));
final String[] setValues = {name,birthday,size,sex};
ListAdapter listAdapter = new CustomAdapter(this,setItems,setValues);
mListView.setAdapter(listAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String setupItemValue = setValues[position];
String setupItem = setItems[position];
Intent intent;
if(setupItem.equals("name")){
intent = new Intent(SetupActivity.this,ChangeName.class);
intent.putExtra(extra,setupItemValue);
startActivityForResult(intent,1);
} else if(setupItem.equals("birthday")){
intent = new Intent(SetupActivity.this,ChangeBirthday.class);
intent.putExtra(extra,setupItemValue);
startActivityForResult(intent,2);
}else if(setupItem.equals("type")){
intent = new Intent(SetupActivity.this,ChangeType.class);
intent.putExtra(extra,setupItemValue);
startActivityForResult(intent,3);
}else{
intent = new Intent(SetupActivity.this,ChangeSex.class);
intent.putExtra(extra,setupItemValue);
startActivityForResult(intent,4);
}
}
});
}
cursor.close();
}
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
DogDatabaseHelper dbHelper= new DogDatabaseHelper(getApplicationContext());
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("Dog",null,null,null,null,null,null,null);
switch (requestCode){
case 1:
if (resultCode == RESULT_OK){
ContentValues cv = new ContentValues();
cv.put("name",data.getStringExtra("return_name"));
db.update("dog",cv,"id=?",new String[]{"1"});
}
break;
case 2:
Toast.makeText(this, "Update2", Toast.LENGTH_SHORT).show();
if(resultCode == RESULT_OK){
ContentValues cv = new ContentValues();
cv.put("birthday",data.getStringExtra("return_birthday"));
db.update("dog",cv,"id=?",new String[]{"1"});
}
break;
case 3:
Toast.makeText(this, "Update3", Toast.LENGTH_SHORT).show();
if(resultCode == RESULT_OK){
ContentValues cv = new ContentValues();
cv.put("size",data.getStringExtra("return_type"));
db.update("dog",cv,"id=?",new String[]{"1"});
}
break;
case 4:
if(resultCode == RESULT_OK){
ContentValues cv = new ContentValues();
cv.put("sex",data.getStringExtra("return_sex"));
Toast.makeText(this, data.getStringExtra("return_sex"), Toast.LENGTH_SHORT).show();
int r = db.update("dog",cv,"id=?",new String[]{"1"});
if(r != 1){
Toast.makeText(this, "Update3", Toast.LENGTH_SHORT).show();
}
}
break;
}
db.close();
updateView();
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.add,menu);
return true;
}
@Override
public boolean onOptionsItem
Selected(MenuItem item){
int id = item.getItemId();
if(id == R.id.add){
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:1)
您应该使用startActivityForResult(intent,requestCode)
打开第二个活动,然后您可以在第一个活动中实现onActivityResult(...)
,您可以再次从db获取数据并刷新视图。