我有一个Listview
我正在使用sqlite数据库填充数据,我使用相同的活动在其中加载不同的数据,当点击Listview
的一行时,它会显示信息另一个Activity
,所以我的问题是如何处理后退按钮,以便它到达适当的列表。
点击第一个按钮:
Intent i=new Intent(Hotel.class.this,Activity_hotel);
i.putExtra("btn",1);
startActivity(i);
点击第二个按钮:
Intent i=new Intent(Hotel.class.this,Activity_hotel.class);
i.putExtra("btn",2);
startActivity(i);
用于后退按钮:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent home_intent = new Intent(getApplicationContext(),Hotel.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(home_intent);
return true;
}
return super.onKeyDown(keyCode, event);
}
编辑:
MainActivity中的
case 2:
Intent intent3 = new Intent(MainActivity.this,Hotel.class);
String lien="hotel";
intent3.putExtra("choix",lien);
startActivity(intent3);
break;
case 3:
Intent intent4 = new Intent(MainActivity.this,Hotel.class);
lien="restaurant";
intent4.putExtra("choix",lien);
startActivity(intent4);
break;
Hotel.class:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hotel);
mDbHelper = new DataBaseHelper(this);
try {
mDbHelper.CopyDataBaseFromAsset();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mDbHelper.openDataBase();
Intent i = getIntent();
String choix = i.getStringExtra("choix");
c = mDbHelper.fetchAllservices(choix); //récupération des données
startManagingCursor(c);
String[] fromColumns = {DataBaseHelper.KEY_NAME}; //from
int[] toViews = {R.id.name}; // to
// définition des éléments de la liste
dataAdapter = new SimpleCursorAdapter(this,R.layout.rows_services, c, fromColumns, toViews, 0);
list = (ListView) findViewById(R.id.list);
list.setAdapter(dataAdapter); //afficher la liste
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
nom = (TextView) view.findViewById(R.id.name); // nom c'est memID_tv
String nom_val = nom.getText().toString(); // nom_val c'est memberID_val
Intent modify_intent = new Intent(getApplicationContext(),
ExtendList.class); // ExtendList c'est Modify_member
modify_intent.putExtra("thename", nom_val); // thename c'est memberID
startActivity(modify_intent);
}
});
详细信息活动(ExtendList.class)
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_daughter);
dbHelper = new DataBaseHelper(this);
try {
dbHelper.CopyDataBaseFromAsset();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dbHelper.openDataBase();
name = (TextView) findViewById(R.id.name1);
adresse = (TextView) findViewById(R.id.adresse);
numtel = (TextView) findViewById(R.id.tel);
email = (TextView) findViewById(R.id.email);
website = (TextView) findViewById(R.id.website);
img = (ImageView)findViewById(R.id.img1);
Intent i = getIntent();
String thename = i.getStringExtra("thename"); // thename c'est membername ( memberID )
services = new Service();
services = dbHelper.Get_ServicesNAME(thename);
path = services.image;
Resources res = getResources();
int resId = res.getIdentifier(path, "drawable", "com.example.guide_oran");
img.setImageResource(resId);
name.setText( services.name);
adresse.setText(services.adresse);
numtel.setText(services.numtel);
email.setText(services.email);
website.setText(services.website);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent home_intent = new Intent(getApplicationContext(),
Hotel.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
home_intent.putExtra("choix",choix); // THE SOLUTION I WAS SEARCHING FOR
startActivity(home_intent);
return true;
}
return super.onKeyDown(keyCode, event);
}
希望我提供了所有细节。 谢谢(问题已解决,我已将解决方案添加到帖子中)
答案 0 :(得分:0)
此代码如下所示:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
if(getIntent().getExtras().getString("btn").equals("1"))
{
// go to appropraite activity
}
else if(getIntent().getExtras().getString("btn").equals("2"))
{
// go to appropraite activity
}
}
return super.onKeyDown(keyCode, event);
}
return super.onKeyDown(keyCode, event);
}
答案 1 :(得分:0)
你的问题可能是以下snippnet:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent home_intent = new Intent(getApplicationContext(),
Hotel.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(home_intent);
return true;
}
return super.onKeyDown(keyCode, event);
}
实际上,(即使使用已编辑的解决方案),这是您的新Intent
,这会导致问题返回到上一个列表。当您返回Hotel.class
时,重新初始化此活动。然后,删除ListView并重新上载。
为避免这种情况,为什么不能只调用finish()
方法来销毁您的实际活动并返回Hotel
活动?话虽如此,您将不会重新创建活动并再次使用资源来重新填充您的列表。
我认为,您应该使用另一种方法来处理后退状态,如下所示:
// Use this method to handle the back button pressed
public void onBackPressed() {
super.onBackPressed();
// then, just finish ExtendList.class
this.finish();
}
然后ExtendList
活动被销毁,而仍然落后的Hotel
活动出现并显示您之前的列表,而不再执行任何操作。
如果有帮助,请告诉我。