package com.example.my2mysql.activiy;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.example.my2mysql.Pojo.Plan;
import com.example.my2mysql.Pojo.PlanDAO;
import com.example.my2mysql.R;
import com.example.my2mysql.tool.MyAdapter;
import java.util.List;
public class APPactivity extends AppCompatActivity {
private ListView mLvPlans;
private List<Plan> mDataList;
private PlanDAO mPlanDAO;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_appactivity);
mLvPlans = findViewById(R.id.listView);
mPlanDAO = new PlanDAO(this);
mPlanDAO.open();
mDataList = mPlanDAO.getAllPlans();
MyAdapter adapter = new MyAdapter(this, mDataList);
mLvPlans.setAdapter(adapter);
setupButtons();
}
private void setupButtons(){
Button btnAdd = findViewById(R.id.btnAdd);
Button btnDelete = findViewById(R.id.btnDelete);
Button btnUpdate = findViewById(R.id.btnUpdate);
btnAdd.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
showAddPlanDialog();
}
});
btnDelete.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
showDeletePlanDialog();
}
});
btnUpdate.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
showUpdatePlanDialog();
}
});
}
private void showAddPlanDialog() {
} AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Add Plan");
builder.setMessage("Enter plan details:");
final EditText input = new EditText(this);
builder.setView(input);
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String planDetails = input.getText().toString();
if (!planDetails.isEmpty()) {
Plan newPlan = new Plan(); // Assuming isDone is initially set to false
mPlanDAO.addPlan(newPlan);
updateListView();
} else {
Toast.makeText(APPactivity.this, "Plan details cannot be empty", Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("Cancel", null);
builder.show();
}
private void showDeletePlanDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Delete Plan");
builder.setMessage("Enter plan ID to delete:");
final EditText input = new EditText(this);
builder.setView(input);
builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String planId = input.getText().toString();
if (!planId.isEmpty()) {
mPlanDAO.deletePlan(Integer.parseInt(planId));
updateListView();
} else {
Toast.makeText(APPactivity.this, "Plan ID cannot be empty", Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("Cancel", null);
builder.show();
}
private void showUpdatePlanDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Update Plan");
builder.setMessage("Enter plan ID and new details:");
final EditText inputId = new EditText(this);
inputId.setHint("Plan ID");
final EditText inputDetails = new EditText(this);
inputDetails.setHint("New Plan Details");
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(inputId);
layout.addView(inputDetails);
builder.setView(layout);
builder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String planId = inputId.getText().toString();
String newDetails = inputDetails.getText().toString();
if (!planId.isEmpty() && !newDetails.isEmpty()) {
Plan updatedPlan = new Plan(Integer.parseInt(planId), newDetails, false); // Assuming Plan constructor takes id, details and isDone
mPlanDAO.updatePlan(updatedPlan);
updateListView();
} else {
Toast.makeText(APPactivity.this, "Plan ID and new details cannot be empty", Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("Cancel", null);
builder.show();
}
private void updateListView() {
mDataList.clear();
mDataList.addAll(mPlanDAO.getAllPlans());
((MyAdapter) mLvPlans.getAdapter()).notifyDataSetChanged();
}
@Override
protected void onDestroy() {
super.onDestroy();
mPlanDAO.close();
}
}
package com.example.my2mysql.activiy;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import com.example.my2mysql.R;
import com.example.my2mysql.tool.okhttp3manager;
public class LoginActivity extends AppCompatActivity {
private static final String TAG = "LoginActivity";
private Button login;
private Button register;
private EditText et_id;
private EditText et_passwd;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_login);
handler = new Handler(Looper.getMainLooper());
initview();
}
public void initview() {
et_id = findViewById(R.id.lg_id);
et_passwd = findViewById(R.id.lg_password);
login = findViewById(R.id.bt_login);
register = findViewById(R.id.bt_register);
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this, registerActivity.class);
startActivity(intent);
finish();
}
});
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userId = et_id.getText().toString();
String password = et_passwd.getText().toString();
new Thread() {
@Override
public void run() {
okhttp3manager.login(userId, password, new okhttp3manager.LoginCallback() {
@Override
public void LoginSuccess(int auth) {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
}
});
if (auth == 0) {
handler.post(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(LoginActivity.this, MainActivity2.class);
startActivity(intent);
finish();
}
});
}
}
@Override
public void LoginFailure(final String errorMessage) {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(LoginActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
}
});
}
});
}
}.start();
}
});
}
}
package com.example.my2mysql.activiy;标签:6.13,void,public,new,import,android,builder From: https://www.cnblogs.com/wcy1111/p/18253706
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import com.example.my2mysql.R;
import com.example.my2mysql.activiy.registerActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
}
}