From 55e289cf11a046c3f1852450a0f12ff508e9f6ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9B=9B=E6=B4=81?= <1126388959@qq.com> Date: Thu, 2 Jan 2020 11:52:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E4=B8=80=E9=83=A8?= =?UTF-8?q?=E5=88=86=E4=BB=A3=E7=A0=81=EF=BC=8C=E4=BD=BF=E7=94=A8listview?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BA=86=E6=B5=8F=E8=A7=88=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/dictionaries/.xml | 1 + app/src/main/AndroidManifest.xml | 6 +- .../sj/raise_money/BrowseRecordActivity.java | 15 +++ .../raise_money/DataBase/DatabaseHelper.java | 2 +- .../sj/raise_money/DataBase/UserService.java | 45 ++++++++- .../hunnu/sj/raise_money/MainActivity.java | 99 ++++++++++++++++--- .../java/hunnu/sj/raise_money/MyFragment.java | 24 +++++ .../main/java/hunnu/sj/raise_money/News.java | 24 +++-- .../hunnu/sj/raise_money/NewsAdapter.java | 55 ++++++++--- .../sj/raise_money/NewsTitleFragment.java | 69 ------------- .../java/hunnu/sj/raise_money/NewsUtils.java | 18 +++- .../hunnu/sj/raise_money/RegiteActivity.java | 15 +-- .../hunnu/sj/raise_money/SignInActivity.java | 12 ++- .../hunnu/sj/raise_money/UploadActivity.java | 47 ++++++++- .../res/layout/activity_browse_record.xml | 17 ++++ app/src/main/res/layout/activity_main.xml | 2 +- app/src/main/res/layout/activity_registe.xml | 2 +- app/src/main/res/layout/activity_signin.xml | 2 +- app/src/main/res/layout/activity_upload.xml | 14 ++- app/src/main/res/layout/content_main.xml | 26 +++-- app/src/main/res/layout/info_item_layout.xml | 40 ++++++++ app/src/main/res/layout/nav_header_main.xml | 2 +- app/src/main/res/layout/news_content.xml | 1 + .../main/res/menu/activity_main_drawer.xml | 4 + app/src/main/res/values/strings.xml | 1 + 25 files changed, 396 insertions(+), 147 deletions(-) create mode 100644 app/src/main/java/hunnu/sj/raise_money/BrowseRecordActivity.java create mode 100644 app/src/main/java/hunnu/sj/raise_money/MyFragment.java delete mode 100644 app/src/main/java/hunnu/sj/raise_money/NewsTitleFragment.java create mode 100644 app/src/main/res/layout/activity_browse_record.xml create mode 100644 app/src/main/res/layout/info_item_layout.xml diff --git a/.idea/dictionaries/.xml b/.idea/dictionaries/.xml index b7a9395..8ef0265 100644 --- a/.idea/dictionaries/.xml +++ b/.idea/dictionaries/.xml @@ -1,6 +1,7 @@ + descri sttmt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 07c38dc..b79be0e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,7 +1,9 @@ + + - + + @@ -22,6 +25,7 @@ diff --git a/app/src/main/java/hunnu/sj/raise_money/BrowseRecordActivity.java b/app/src/main/java/hunnu/sj/raise_money/BrowseRecordActivity.java new file mode 100644 index 0000000..97250df --- /dev/null +++ b/app/src/main/java/hunnu/sj/raise_money/BrowseRecordActivity.java @@ -0,0 +1,15 @@ +package hunnu.sj.raise_money; + +import androidx.appcompat.app.AppCompatActivity; + +import android.os.Bundle; + +public class BrowseRecordActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_browse_record); + + } +} diff --git a/app/src/main/java/hunnu/sj/raise_money/DataBase/DatabaseHelper.java b/app/src/main/java/hunnu/sj/raise_money/DataBase/DatabaseHelper.java index e5bd127..ca18397 100644 --- a/app/src/main/java/hunnu/sj/raise_money/DataBase/DatabaseHelper.java +++ b/app/src/main/java/hunnu/sj/raise_money/DataBase/DatabaseHelper.java @@ -13,7 +13,7 @@ public class DatabaseHelper { } - public static Connection getConnection(){ + public static Connection getConnection(){//数据库连接池 String username = "root"; String password ="12345678"; if(conn==null){ diff --git a/app/src/main/java/hunnu/sj/raise_money/DataBase/UserService.java b/app/src/main/java/hunnu/sj/raise_money/DataBase/UserService.java index 143a1a3..9478733 100644 --- a/app/src/main/java/hunnu/sj/raise_money/DataBase/UserService.java +++ b/app/src/main/java/hunnu/sj/raise_money/DataBase/UserService.java @@ -1,10 +1,17 @@ package hunnu.sj.raise_money.DataBase; +import android.content.Context; + +import androidx.core.content.ContextCompat; + import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.util.ArrayList; +import hunnu.sj.raise_money.News; +import hunnu.sj.raise_money.R; import hunnu.sj.raise_money.User; public class UserService { @@ -13,6 +20,7 @@ public class UserService { public UserService(){ this.db = DatabaseHelper.getDb(); } + public int login(String username,String password){ conn = db.getConnection(); Statement sttmt = null; @@ -42,7 +50,7 @@ public class UserService { } public boolean register(User user){ - conn = db.getConnection(); + conn = db.getConnection();//将用户的信息上传到数据库,上传成功返回true,失败false try{ String sql = "insert into user(username,password,role) values('"+user.getName()+"','"+user.getPasd()+"','"+user.getRole()+"')"; Statement sttmt = conn.createStatement(); @@ -55,7 +63,7 @@ public class UserService { return false; } - public String getRole(String username){ + public String getRole(String username){//从数据库获取用户的角色 //SQLiteDatabase sdb=dbHelper.getReadableDatabase(); String sql="select role from user where username = '" + username+"'"; conn = db.getConnection(); @@ -73,4 +81,37 @@ public class UserService { return null; } + public boolean UploadInfo(News news){//用户上传信息到数据库,服务器 + String sql = "insert into stu_info(name,descri) values('"+news.getTitle()+"','"+news.getDes() + "')"; + conn = db.getConnection(); + try{ + Statement sttmt = conn.createStatement(); + sttmt.executeUpdate(sql); + sttmt.close(); + return true; + }catch (SQLException e){ + e.printStackTrace(); + } + return false; + } + + public void getAllNews(Context context,ArrayList list){ + ArrayList mlist = list; + String sql = "select * from stu_info"; + conn = db.getConnection(); + try{ + Statement sttmt = conn.createStatement(); + ResultSet rs = sttmt.executeQuery(sql); + while (rs.next()){ + News news= new News(); + news.setTitle(rs.getNString("name")); + news.setDes(rs.getNString("descri")); + news.setHead_icon(ContextCompat.getDrawable(context, R.drawable.ic_launcher_background)); + list.add(news); + } + }catch(SQLException e){ + e.printStackTrace(); + } + } + } diff --git a/app/src/main/java/hunnu/sj/raise_money/MainActivity.java b/app/src/main/java/hunnu/sj/raise_money/MainActivity.java index e68d474..c8a732e 100644 --- a/app/src/main/java/hunnu/sj/raise_money/MainActivity.java +++ b/app/src/main/java/hunnu/sj/raise_money/MainActivity.java @@ -1,6 +1,8 @@ package hunnu.sj.raise_money; +import android.content.Context; import android.content.Intent; +import android.net.Uri; import android.os.Bundle; import com.google.android.material.floatingactionbutton.FloatingActionButton; @@ -9,6 +11,9 @@ import com.google.android.material.snackbar.Snackbar; import android.view.MenuItem; import android.view.View; +import androidx.appcompat.app.ActionBarDrawerToggle; +import androidx.core.content.ContextCompat; +import androidx.fragment.app.ListFragment; import androidx.navigation.NavController; import androidx.navigation.Navigation; import androidx.navigation.ui.AppBarConfiguration; @@ -22,13 +27,25 @@ import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import android.view.Menu; +import android.widget.AdapterView; +import android.widget.ListView; import android.widget.TextView; -public class MainActivity extends AppCompatActivity { - private AppBarConfiguration mAppBarConfiguration; +import java.util.ArrayList; +public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{ + private AppBarConfiguration mAppBarConfiguration; + private Toolbar toolbar; + private ActionBarDrawerToggle mToggle; + private DrawerLayout mDrawerlayout; + private NavigationView navigationView; + private TextView menu_name; TextView menu_role; + ArrayList list = new ArrayList<>(); + Context mContext; + private NewsAdapter newsAdapter; + private ListView lv_news; @Override protected void onCreate(Bundle savedInstanceState) { @@ -37,9 +54,20 @@ public class MainActivity extends AppCompatActivity { Intent intent = getIntent(); User user; user = (User) intent.getSerializableExtra("user"); - setContentView(R.layout.activity_main); - Toolbar toolbar = findViewById(R.id.toolbar); + mContext = this; + newsAdapter = new NewsAdapter(this,list); + + + init(); + if(navigationView.getHeaderCount() > 0) {//设置侧滑栏的显示的值 + View header = navigationView.getHeaderView(0); + TextView menu_name = header.findViewById(R.id.user_name); + TextView menu_role = header.findViewById(R.id.user_role); + menu_name.setText(user.getName()); + menu_role.setText(user.getRole()); + } + /*Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @@ -62,13 +90,47 @@ public class MainActivity extends AppCompatActivity { NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration); NavigationUI.setupWithNavController(navigationView, navController); - if(navigationView.getHeaderCount() > 0) { + if(navigationView.getHeaderCount() > 0) {//设置侧滑栏的显示的值 View header = navigationView.getHeaderView(0); TextView menu_name = header.findViewById(R.id.user_name); TextView menu_role = header.findViewById(R.id.user_role); menu_name.setText(user.getName()); menu_role.setText(user.getRole()); - } + }*/ + + //setContentView(R.layout.content_main); + //new UploadThread().start(); + lv_news = findViewById(R.id.list); + lv_news.setAdapter(newsAdapter); + lv_news.setOnItemClickListener(this); + News news = new News(); + news.setTitle("火箭发射成功"); + news.setDes("地方上的房贷首付读书首付第三方的手房贷首付第三方的手负担"); + news.setHead_icon(ContextCompat.getDrawable(this, R.drawable.ic_launcher_background)); + list.add(news); + new UploadThread().start(); + } + + + private void init(){ + toolbar = (Toolbar) findViewById(R.id.toolbar); + mDrawerlayout = (DrawerLayout)findViewById(R.id.drawer_layout); + toolbar.setTitle(""); + mToggle = new ActionBarDrawerToggle(this, mDrawerlayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); + mDrawerlayout.addDrawerListener(mToggle); + mToggle.syncState(); + + mDrawerlayout.setDrawerListener(mToggle); + navigationView = findViewById(R.id.nav_view); + } + @Override//点击条目时调用该方法,parent指listview,position指条目位置 + public void onItemClick(AdapterView parent, View view, int position, long id) { + News news = (News)parent.getItemAtPosition(position); + String url = "www.baidu.com"; + Intent intent = new Intent(); + intent.setAction(Intent.ACTION_VIEW); + intent.setData(Uri.parse(url)); + startActivity(intent); } @Override @@ -78,15 +140,30 @@ public class MainActivity extends AppCompatActivity { return true; } - @Override - public boolean onSupportNavigateUp() { + private class UploadThread extends Thread{ + @Override + public void run() { + NewsUtils.getAllNews(mContext,list); + newsAdapter.notifyDataSetChanged(); + } + } + + + /*public boolean onSupportNavigateUp() { NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment); return NavigationUI.navigateUp(navController, mAppBarConfiguration) || super.onSupportNavigateUp(); - } + }*/ - public void onNeedHelp(MenuItem item){ + public void onNeedHelp(MenuItem item){//侧滑栏的item,用于提交信息 Intent intent = new Intent(this, UploadActivity.class); startActivity(intent); - }//ssds + } + + public void onBrowseDonateRecord(MenuItem item){ + Intent intent = new Intent(this,BrowseRecordActivity.class); + startActivity(intent); + } + + } diff --git a/app/src/main/java/hunnu/sj/raise_money/MyFragment.java b/app/src/main/java/hunnu/sj/raise_money/MyFragment.java new file mode 100644 index 0000000..86e0fb5 --- /dev/null +++ b/app/src/main/java/hunnu/sj/raise_money/MyFragment.java @@ -0,0 +1,24 @@ +package hunnu.sj.raise_money; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.FrameLayout; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.navigation.fragment.NavHostFragment; + +public class MyFragment extends NavHostFragment { + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + FrameLayout frameLayout = new FrameLayout(inflater.getContext()); + // When added via XML, this has no effect (since this FrameLayout is given the ID + // automatically), but this ensures that the View exists as part of this Fragment's View + // hierarchy in cases where the NavHostFragment is added programmatically as is required + // for child fragment transactions + frameLayout.setId(getId()); + return frameLayout; + } +} \ No newline at end of file diff --git a/app/src/main/java/hunnu/sj/raise_money/News.java b/app/src/main/java/hunnu/sj/raise_money/News.java index 439f7a8..ce50abe 100644 --- a/app/src/main/java/hunnu/sj/raise_money/News.java +++ b/app/src/main/java/hunnu/sj/raise_money/News.java @@ -1,18 +1,30 @@ package hunnu.sj.raise_money; -public class News { +import android.graphics.drawable.Drawable; +import android.widget.ImageView; + +public class News {//主界面浏览的信息的类 private String title; - private String content; + private String des; + private Drawable head_icon; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } - public String getContent() { - return content; + public String getDes() { + return des; + } + public void setDes(String content) { + this.des = content; } - public void setContent(String content) { - this.content = content; + + public Drawable getHead_icon() { + return head_icon; + } + + public void setHead_icon(Drawable head_icon) { + this.head_icon = head_icon; } } \ No newline at end of file diff --git a/app/src/main/java/hunnu/sj/raise_money/NewsAdapter.java b/app/src/main/java/hunnu/sj/raise_money/NewsAdapter.java index 17174c6..918ed9e 100644 --- a/app/src/main/java/hunnu/sj/raise_money/NewsAdapter.java +++ b/app/src/main/java/hunnu/sj/raise_money/NewsAdapter.java @@ -1,31 +1,54 @@ package hunnu.sj.raise_money; import android.content.Context; -import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ArrayAdapter; +import android.widget.BaseAdapter; +import android.widget.ImageView; import android.widget.TextView; -import java.util.List; +import java.util.ArrayList; +//信息的适配器 +public class NewsAdapter extends BaseAdapter { + private ArrayList list; + private Context context; + public NewsAdapter(Context context,ArrayList list) { -public class NewsAdapter extends ArrayAdapter { - private int resourceId; - public NewsAdapter(Context context, int textViewResourceId, List objects) { - super(context, textViewResourceId, objects); - resourceId = textViewResourceId; + this.context = context; + this.list = list; } + + @Override + public int getCount() { + return list.size(); + } + + @Override + public Object getItem(int position){ + return list.get(position); + } + + @Override + public long getItemId(int position) { + return position; + } + @Override public View getView(int position, View convertView, ViewGroup parent) { - News news = getItem(position); - View view; - if (convertView == null) { - view = LayoutInflater.from(getContext()).inflate(resourceId, null); - } else { - view = convertView; + View view = null; + if(convertView != null){ + view = convertView; + }else{ + view = View.inflate(context,R.layout.info_item_layout,null); } - TextView newsTitleText =(TextView) view.findViewById(R.id.news_title); - newsTitleText.setText(news.getTitle()); + ImageView item_icon = (ImageView) view.findViewById(R.id.item_head_icon); + TextView item_title = (TextView) view.findViewById(R.id.item_info_title); + TextView item_des = (TextView) view.findViewById(R.id.item_info_des); + News news = list.get(position); + item_icon.setImageDrawable(news.getHead_icon()); + item_title.setText(news.getTitle()); + item_des.setText(news.getDes()); + return view; } } \ No newline at end of file diff --git a/app/src/main/java/hunnu/sj/raise_money/NewsTitleFragment.java b/app/src/main/java/hunnu/sj/raise_money/NewsTitleFragment.java deleted file mode 100644 index 1881d8d..0000000 --- a/app/src/main/java/hunnu/sj/raise_money/NewsTitleFragment.java +++ /dev/null @@ -1,69 +0,0 @@ -package hunnu.sj.raise_money; - -import android.app.Activity; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.AdapterView; -import android.widget.ListView; -import android.app.Fragment; - -import java.util.ArrayList; -import java.util.List; - -public class NewsTitleFragment extends Fragment implements AdapterView.OnItemClickListener { - private ListView newsTitleListView; - private List newsList; - private NewsAdapter adapter; - private boolean isTwoPane; - @Override - public void onAttach(Activity activity) { - super.onAttach(activity); - newsList = getNews(); // 初始化新闻数据 - adapter = new NewsAdapter(activity, R.layout.news_item, newsList); - } - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.fragment_news_title, container, false); - newsTitleListView = (ListView) view.findViewById(R.id.news_title_list_view); - newsTitleListView.setAdapter(adapter); - newsTitleListView.setOnItemClickListener(this); - return view; - } - @Override - public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - isTwoPane = false; // 可以找到news_content_layout布局时,为双页模式 - } - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - News news = newsList.get(position); - if (isTwoPane) { - // 如果是双页模式,则刷新NewsContentFragment中的内容 - NewsContentFragment newsContentFragment = (NewsContentFragment) getFragmentManager().findFragmentById(R.id.news_content_fragment); - newsContentFragment.refresh(news.getTitle(), news.getContent()); - } else { - // 如果是单页模式,则直接启动NewsContentActivity - NewsContentActivity.actionStart(getActivity(), news.getTitle(), news.getContent()); - } - } - private List getNews() { - List newsList = new ArrayList(); - News news1 = new News(); - news1.setTitle("Succeed in College as a Learning Disabled Student"); - news1.setContent("College freshmen will soon learn to live with a" - +"roommate, adjust to a new social scene and survive less-than-stellar" - +"dining hall food. Students with learning disabilities will face these" - +"transitions while also grappling with a few more hurdles."); - newsList.add(news1); - News news2 = new News(); - news2.setTitle("Google Android exec poached by China's Xiaomi"); - news2.setContent("China's Xiaomi has poached a key Google executive"+ - "involved in the tech giant's Android phones, in a move seen as a coup"+ - " for the rapidly growing Chinese smartphone maker."); - newsList.add(news2); - return newsList; - } -} \ No newline at end of file diff --git a/app/src/main/java/hunnu/sj/raise_money/NewsUtils.java b/app/src/main/java/hunnu/sj/raise_money/NewsUtils.java index 1c1f55d..f9e4954 100644 --- a/app/src/main/java/hunnu/sj/raise_money/NewsUtils.java +++ b/app/src/main/java/hunnu/sj/raise_money/NewsUtils.java @@ -2,11 +2,23 @@ package hunnu.sj.raise_money; import android.content.Context; +import androidx.core.content.ContextCompat; + import java.util.ArrayList; +import hunnu.sj.raise_money.DataBase.UserService; + public class NewsUtils { - public static ArrayList getAllNews(Context context){//从数据库获取信息到本地 - ArrayList list = new ArrayList(); - return list; + public static void getAllNews(Context context,ArrayList list){//从数据库获取信息到本地 + UserService uService = new UserService(); + uService.getAllNews(context,list); + /*for(int i = 0 ;i <100;i++) + { + News news = new News(); + news.setTitle("火箭发射成功"); + news.setDes("地方上的房贷首付读书首付第三方的手房贷首付第三方的手负担"); + news.setHead_icon(ContextCompat.getDrawable(context, R.drawable.ic_launcher_background)); + list.add(news); + }*/ } } diff --git a/app/src/main/java/hunnu/sj/raise_money/RegiteActivity.java b/app/src/main/java/hunnu/sj/raise_money/RegiteActivity.java index 3be7ffc..ed29096 100644 --- a/app/src/main/java/hunnu/sj/raise_money/RegiteActivity.java +++ b/app/src/main/java/hunnu/sj/raise_money/RegiteActivity.java @@ -13,7 +13,7 @@ import android.widget.RadioButton; import hunnu.sj.raise_money.DataBase.UserService; - +//用户注册 public class RegiteActivity extends AppCompatActivity { EditText user_name; EditText pass_word; @@ -24,7 +24,7 @@ public class RegiteActivity extends AppCompatActivity { Handler rHandler = new Handler(){ @Override public void handleMessage(@NonNull Message msg) { - super.handleMessage(msg); + super.handleMessage(msg);//用户在主线程中处理子线程发送回来的消息,更改控件内容 String txt = (String) msg.obj; switch (msg.what){ case 0: @@ -61,6 +61,7 @@ public class RegiteActivity extends AppCompatActivity { }else if(radio2.isChecked()){ role = "贫困学生"; } + //获取注册界面的控件 new RegiteThread().start(); } @@ -69,10 +70,10 @@ public class RegiteActivity extends AppCompatActivity { public void run() { String username = user_name.getText().toString(); String password = pass_word.getText().toString(); - String repassword = repass_word.getText().toString(); + String repassword = repass_word.getText().toString();//从控件获取字符 UserService uService = new UserService(); - Message message = rHandler.obtainMessage(); - if(username.trim().equals("")){ + Message message = rHandler.obtainMessage();//使用handler来处理信息 + if(username.trim().equals("")){//一系列条件来判断是否可注册 message.what = 0; message.obj = "请输入用户名"; rHandler.sendMessage(message); @@ -87,13 +88,13 @@ public class RegiteActivity extends AppCompatActivity { }else if(uService.login(username,password)!=0){ message.what = 3; message.obj = "用户已存在"; - rHandler.sendMessage(message); + rHandler.sendMessage(message);//发送信息给handler }else{ User user = new User(); user.setName(username); user.setPasd(password); user.setRole(role); - uService.register(user); + uService.register(user);//注册 Intent intent = new Intent(RegiteActivity.this,SignInActivity.class); startActivity(intent); finish(); diff --git a/app/src/main/java/hunnu/sj/raise_money/SignInActivity.java b/app/src/main/java/hunnu/sj/raise_money/SignInActivity.java index faceffb..e8db57e 100644 --- a/app/src/main/java/hunnu/sj/raise_money/SignInActivity.java +++ b/app/src/main/java/hunnu/sj/raise_money/SignInActivity.java @@ -13,6 +13,8 @@ import android.widget.Button; import android.widget.EditText; +import java.util.ArrayList; + import hunnu.sj.raise_money.DataBase.UserService; public class SignInActivity extends AppCompatActivity { @@ -22,7 +24,7 @@ public class SignInActivity extends AppCompatActivity { EditText pswd; UserService uService; User user; - Handler mHandler = new Handler(){ + Handler mHandler = new Handler(){//用于处理子线程传回的消息,对主线的UI进行修改 int flag = -1; @Override public void handleMessage(@NonNull Message msg) { @@ -65,16 +67,16 @@ public class SignInActivity extends AppCompatActivity { class LoginThread extends Thread{ @Override public void run() { - int flag = -1; + int flag = -1;//先设置flag为-1 String name = usnm.getText().toString(); String pasd = pswd.getText().toString(); //UserService uService = new UserService(); - flag = uService.login(name,pasd); + flag = uService.login(name,pasd);//用login函数来判断用户输入的用户名与密码是否正确 Message message = mHandler.obtainMessage(); if(flag==0){ message.what = 0; message.obj = "用户名不存在"; - mHandler.sendMessage(message); + mHandler.sendMessage(message);//使用handler给主线程发送消息 }else if(flag==1){ message.what = 1; message.obj = "密码错误"; @@ -85,7 +87,7 @@ public class SignInActivity extends AppCompatActivity { user.setPasd(pasd); String role = uService.getRole(name); user.setRole(role); - Bundle bundle = new Bundle(); + Bundle bundle = new Bundle();//将类包装到bundle里,再放到intent里 bundle.putSerializable("user",user); Intent intent =new Intent(SignInActivity.this,MainActivity.class); intent.putExtras(bundle); diff --git a/app/src/main/java/hunnu/sj/raise_money/UploadActivity.java b/app/src/main/java/hunnu/sj/raise_money/UploadActivity.java index 5d78215..857aeda 100644 --- a/app/src/main/java/hunnu/sj/raise_money/UploadActivity.java +++ b/app/src/main/java/hunnu/sj/raise_money/UploadActivity.java @@ -3,21 +3,27 @@ package hunnu.sj.raise_money; import androidx.appcompat.app.AppCompatActivity; import androidx.core.content.FileProvider; +import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.os.Looper; import android.provider.MediaStore; import android.view.View; import android.widget.Button; +import android.widget.EditText; import android.widget.ImageView; +import android.widget.TextView; +import android.widget.Toast; import java.io.File; import java.io.IOException; import java.net.URI; +import hunnu.sj.raise_money.DataBase.UserService; import hunnu.sj.raise_money.R; public class UploadActivity extends AppCompatActivity { @@ -25,14 +31,18 @@ public class UploadActivity extends AppCompatActivity { private ImageView stu_pic; private ImageView[] stu_src; private Uri imageUri; + private EditText info_name; + private EditText info_desc; + private Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_upload); - stu_pic = findViewById(R.id.stu_pic); + context = this; + //stu_pic = findViewById(R.id.stu_pic); } - protected void onUploadPic(View view){ + public void onUploadPic(View view){ /*File outputImage = new File(getExternalCacheDir(),"output_image.jpg"); try { if(outputImage.exists()){ @@ -48,9 +58,38 @@ public class UploadActivity extends AppCompatActivity { imageUri = Uri.fromFile(outputImage); }*/ - Intent intent = new Intent(Intent.ACTION_PICK,null); + /*Intent intent = new Intent(Intent.ACTION_PICK,null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*"); - startActivityForResult(intent,1); + startActivityForResult(intent,1);*/ + info_name = findViewById(R.id.info_name); + info_desc = findViewById(R.id.info_desc); + new UploadThread().start(); + } + + + private class UploadThread extends Thread{ + @Override + public void run() { + String name = info_name.getText().toString(); + String desc = info_desc.getText().toString(); + News news = new News(); + news.setTitle(name); + news.setDes(desc); + UserService uService = new UserService(); + if(uService.UploadInfo(news)){ + Looper.prepare(); + Toast toast =Toast.makeText(context,"信息上传成功",Toast.LENGTH_SHORT); + toast.show(); + Intent intent = new Intent(UploadActivity.this,MainActivity.class); + startActivity(intent); + Looper.loop(); + }else{ + Looper.prepare(); + Toast toast =Toast.makeText(context,"信息上传失败",Toast.LENGTH_SHORT); + toast.show(); + Looper.loop(); + } + } } private void onLoadPic(){ diff --git a/app/src/main/res/layout/activity_browse_record.xml b/app/src/main/res/layout/activity_browse_record.xml new file mode 100644 index 0000000..74a7e77 --- /dev/null +++ b/app/src/main/res/layout/activity_browse_record.xml @@ -0,0 +1,17 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 3c36b67..3049db9 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -7,7 +7,7 @@ android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="start"> - + - + - + - + @@ -34,7 +34,6 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@android:dimen/app_icon_size" - android:onClick="onUploadPic" android:text="上传图片" /> @@ -44,7 +43,15 @@ android:orientation="vertical"> + + diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml index 175e74d..38b0b54 100644 --- a/app/src/main/res/layout/content_main.xml +++ b/app/src/main/res/layout/content_main.xml @@ -6,21 +6,17 @@ android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/app_bar_main"> + - + + + - \ No newline at end of file diff --git a/app/src/main/res/layout/info_item_layout.xml b/app/src/main/res/layout/info_item_layout.xml new file mode 100644 index 0000000..18442c9 --- /dev/null +++ b/app/src/main/res/layout/info_item_layout.xml @@ -0,0 +1,40 @@ + + + + + + + + + diff --git a/app/src/main/res/layout/nav_header_main.xml b/app/src/main/res/layout/nav_header_main.xml index 971e2bb..8c82b31 100644 --- a/app/src/main/res/layout/nav_header_main.xml +++ b/app/src/main/res/layout/nav_header_main.xml @@ -12,7 +12,7 @@ android:paddingRight="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:theme="@style/ThemeOverlay.AppCompat.Dark"> - + + \ No newline at end of file diff --git a/app/src/main/res/menu/activity_main_drawer.xml b/app/src/main/res/menu/activity_main_drawer.xml index 4cc23e1..ad72dc7 100644 --- a/app/src/main/res/menu/activity_main_drawer.xml +++ b/app/src/main/res/menu/activity_main_drawer.xml @@ -4,4 +4,8 @@ android:id="@+id/needhelp" android:onClick="onNeedHelp" android:title="@string/need_help" /> + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3e41eed..ffaf46f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -17,6 +17,7 @@ 用户角色 HeadPortrait 申请帮助 + 查看捐款记录 onUploadPic