package com.example.yangguoying;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TabHost;
import android.widget.Toast;
import com.example.yangguoying.SqLiteDatabase.GetDatabase;
import com.example.yangguoying.adapter.StudentInfoAdapter;
import com.example.yangguoying.adapter.ViewPagerAdapter;
import com.example.yangguoying.datasource.DataSource;
import com.example.yangguoying.entity.StudentInfo;
import com.example.yangguoying.util.JDBC.JDBCAPI.JDBCUtilImpl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{
ImageButton edit_button;
SharedPreferences.Editor dataEdit;
SharedPreferences getDataEdit;
Button main_add_student_info;
ListView main_student_info_list;
StudentInfoAdapter studentInfoAdapter;
List<StudentInfo> list;
DataSource dataSource;
JDBCUtilImpl jdbcUtil;
GetDatabase database;
SQLiteDatabase sqLiteDatabase;
Cursor cursor;
StudentInfo studentInfo;
ContentValues contentValues;
ProgressDialog progressDialog;
Button view_one_button;
Button view_tow_button;
LinearLayout view_one_line;
LinearLayout view_tow_line;
Button cloud_view_button;
List<StudentInfo> cloud_list;
ListView cloud_view_list;
StudentInfoAdapter cloudStudentInfoAdapter;
private LayoutInflater layoutInflater;
private ViewPager viewPager;
private ViewPagerAdapter viewPagerAdapter;
private List<View> viewList;
private View viewOne;
private View viewTow;
private int curIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setView();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 1:
onLoadStudentInfo();
break;
case 2:
case 4:
case 6:
studentInfoAdapter.notifyDataSetChanged();
break;
}
}
private void initView() {
edit_button = findViewById(R.id.edit_button);
dataEdit = getSharedPreferences("user_data",MODE_PRIVATE).edit();
getDataEdit = getSharedPreferences("user_data",MODE_PRIVATE);
main_add_student_info = findViewById(R.id.main_add_student_info);
// main_student_info_list = findViewById(R.id.main_student_info_list);
dataSource = new DataSource();
jdbcUtil = new JDBCUtilImpl(dataSource.getConnectionUrl(),dataSource.getDatabaseUser(),dataSource.getDatabasePassword(),"com.mysql.jdbc.Driver");
database = new GetDatabase();
sqLiteDatabase = database.getDatabase(this).getWritableDatabase();
contentValues = new ContentValues();
progressDialog = new ProgressDialog(MainActivity.this);
view_one_button = findViewById(R.id.view_one_button);
view_tow_button = findViewById(R.id.view_tow_button);
view_one_line = findViewById(R.id.view_one_line);
view_tow_line = findViewById(R.id.view_tow_line);
layoutInflater = LayoutInflater.from(this);
viewList = new ArrayList<>();
viewPager = findViewById(R.id.view_pager);
viewOne = layoutInflater.inflate(R.layout.local_database_view,null);
viewTow = layoutInflater.inflate(R.layout.cloud_database_view,null);
viewList.add(viewOne);
viewList.add(viewTow);
viewPagerAdapter = new ViewPagerAdapter(viewList);
viewPager.setAdapter(viewPagerAdapter);
viewPager.setCurrentItem(curIndex);
viewPager.setOffscreenPageLimit(2);
viewPager.addOnPageChangeListener(this);
main_student_info_list =viewOne.findViewById(R.id.main_student_info_list);
onLoadStudentInfo();
cloud_view_button = viewTow.findViewById(R.id.cloud_view_button);
cloud_view_list = viewTow.findViewById(R.id.cloud_view_list);
cloudStudentInfoAdapter = new StudentInfoAdapter(MainActivity.this,cloud_list);
}
private void setView() {
main_student_info_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this,ShowStudentInfoActivity.class);
intent.putExtra("id",list.get(position).getId());
intent.putExtra("isCloud",false);
startActivityForResult(intent,2);
}
});
main_add_student_info.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AddStudentInfoActivity.class);
startActivityForResult(intent, 1);
finish();
}
});
main_student_info_list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("提示");
builder.setMessage("确认删除学生信息");
builder.setPositiveButton("删除", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//TODO 删除本地数据
sqLiteDatabase.delete("student_info","student_info_id = ?",new String[]{String.valueOf(list.get(position).getId())});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
AlertDialog.Builder builder_delete_cloud = new AlertDialog.Builder(MainActivity.this);
builder_delete_cloud.setTitle("提示");
builder_delete_cloud.setMessage("是否删除云数据信息");
builder_delete_cloud.setPositiveButton("删除", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//TODO 删除云数据
new Thread(new Runnable() {
@Override
public void run() {
jdbcUtil.deleteData("DELETE FROM `student_info` WHERE `student_info_id` = ?",new Object[]{list.get(position).getId()});
deleteBefore(position);
}