我有这个代码,当用户登录并按下它时,它仍会返回到登录页面。有人可以帮我吗?这是我的代码
package com.example.kun.carkila;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.kosalgeek.genasync12.AsyncResponse;
import com.kosalgeek.genasync12.PostResponseAsyncTask;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
final String LOG = "MainActivity";
Button btnLogin;
EditText etUsername, etPassword;
TextView tvRegister;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
etUsername = (EditText) findViewById(R.id.etFirstname);
etPassword = (EditText) findViewById(R.id.etPassword);
btnLogin = (Button) findViewById(R.id.btnLogin);
tvRegister = (TextView) findViewById(R.id.tvRegister);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
HashMap postData = new HashMap();
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
postData.put("username", username);
postData.put("password", password);
PostResponseAsyncTask task1 = new PostResponseAsyncTask(MainActivity.this, postData,
new AsyncResponse() {
@Override
public void processFinish(String s) {
if (s.contains("renterowner")) {
Toast.makeText(MainActivity.this, "Renter Login Successful!", Toast.LENGTH_SHORT).show();
Intent in = new Intent(MainActivity.this, ListActivity.class);
startActivity(in);
} else if (s.contains("ownerrenter")) {
Toast.makeText(MainActivity.this, "Owner Login Successful!", Toast.LENGTH_SHORT).show();
Intent in = new Intent(MainActivity.this, ownerhome.class);
startActivity(in);
} else {
Toast.makeText(MainActivity.this, "Login Failed!", Toast.LENGTH_SHORT).show();
}
}
});
task1.execute("http://carkila.esy.es/authenticate.php");
}
});
tvRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(in);
}
});
}
}
或者我会修改登录时重定向的类? 以下是重定向的页面。
package com.example.kun.carkila;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import com.amigold.fundapter.BindDictionary;
import com.amigold.fundapter.FunDapter;
import com.amigold.fundapter.extractors.StringExtractor;
import com.amigold.fundapter.interfaces.DynamicImageLoader;
import com.kosalgeek.android.json.JsonConverter;
import com.kosalgeek.genasync12.AsyncResponse;
import com.kosalgeek.genasync12.PostResponseAsyncTask;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
public class ListActivity extends AppCompatActivity implements AsyncResponse, AdapterView.OnItemClickListener {
private ArrayList<Cars> carList;
private ListView lvCars;
private FunDapter<Cars> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
ImageLoader.getInstance().init(UILConfig.config(ListActivity.this));
PostResponseAsyncTask taskRead = new PostResponseAsyncTask(ListActivity.this, this);
taskRead.execute("http://carkila.esy.es/product.php");
lvCars = (ListView) findViewById(R.id.lvCars);
registerForContextMenu(lvCars);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.list_context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
final Cars selectedCars = adapter.getItem(info.position);
if(item.getItemId() == R.id.menuDelete) {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setMessage("Do you want to remove " + selectedCars.Car_Model+ " ?");
alert.setPositiveButton("Remove", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
carList.remove(selectedCars);
adapter.notifyDataSetChanged();
}
});
alert.setNegativeButton("Cancel", null);
alert.show();
}
return super.onContextItemSelected(item);
}
@Override
public void processFinish (String s){
carList = new JsonConverter<Cars>().toArrayList(s, Cars.class);
BindDictionary<Cars> dict = new BindDictionary<Cars>();
dict.addDynamicImageField(R.id.ivImg, new StringExtractor<Cars>() {
@Override
public String getStringValue(Cars product, int position) {
return product.Image;
}
}, new DynamicImageLoader() {
@Override
public void loadImage(String url, ImageView imageView) {
ImageLoader.getInstance().displayImage(url, imageView); // Default options will be used
}
});
dict.addStringField(R.id.tvCarModel, new StringExtractor<Cars>() {
@Override
public String getStringValue(Cars product, int position) {
return "Car Model: " + product.Car_Model;
}
});
dict.addStringField(R.id.tvCarType, new StringExtractor<Cars>() {
@Override
public String getStringValue(Cars product, int position) {
return "Car Type: " + product.Car_Type;
}
});
dict.addStringField(R.id.tvCapacity, new StringExtractor<Cars>() {
@Override
public String getStringValue(Cars product, int position) {
return "Capacity: " + product.Capacity;
}
});
dict.addStringField(R.id.tvFuelType, new StringExtractor<Cars>() {
@Override
public String getStringValue(Cars product, int position) {
return "Fuel Type: " + product.FuelType;
}
});
dict.addStringField(R.id.tvPlateNumber, new StringExtractor<Cars>() {
@Override
public String getStringValue(Cars product, int position) {
return "Plate Number: " + product.PlateNumber;
}
});
adapter = new FunDapter<>(
ListActivity.this, carList, R.layout.layout_list, dict);
lvCars.setAdapter(adapter);
lvCars.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Cars selectedCars = carList.get(position);
Intent in = new Intent(ListActivity.this, DetailActivity.class);
in.putExtra("cars", selectedCars);
startActivity(in);
}
}
另一个
package com.example.kun.carkila;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class ownerhome extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ownerhome);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent in = new Intent(ownerhome.this, InsertActivity.class);
startActivity(in);
}
});
}
}
提前谢谢你们。 :)
答案 0 :(得分:2)
使用此代码
Intent in = new Intent(MainActivity.this, ListActivity.class);
startActivity(in);
finish();
它是立即退出还是完成调用它的函数?
调用finish()的方法将运行完成。在将控制权返回给Android之前,finish()操作甚至不会开始
共享优先
查找this link
对你有帮助