zoukankan      html  css  js  c++  java
  • android简易论坛的制作

    用到的技术:bmob后端云、ListView

    运行效果图:

                               

    项目结构图:

    首先要配置好bmob的环境:https://www.cnblogs.com/hemeiwolong/p/12874714.html

    剩下的看源码

    manifests:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="com.mingrisoft.cooking">
     4     <!--允许联网 -->
     5     <uses-permission android:name="android.permission.INTERNET" />
     6     <!--获取GSM(2g)、WCDMA(联通3g)等网络状态的信息  -->
     7     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
     8     <!--获取wifi网络状态的信息 -->
     9     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    10     <!--保持CPU 运转,屏幕和键盘灯有可能是关闭的,用于文件上传和下载 -->
    11     <uses-permission android:name="android.permission.WAKE_LOCK" />
    12     <!--获取sd卡写的权限,用于文件上传和下载-->
    13     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    14     <!--允许读取手机状态 用于创建BmobInstallation-->
    15     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    16 
    17     <application
    18         android:allowBackup="true"
    19         android:icon="@mipmap/ic_launcher"
    20         android:label="@string/app_name"
    21         android:roundIcon="@mipmap/ic_launcher_round"
    22         android:supportsRtl="true"
    23         android:theme="@style/AppTheme">
    24         <activity android:name=".MainActivity">
    25             <intent-filter>
    26                 <action android:name="android.intent.action.MAIN" />
    27 
    28                 <category android:name="android.intent.category.LAUNCHER" />
    29             </intent-filter>
    30         </activity>
    31 
    32         <provider
    33             android:name="cn.bmob.v3.util.BmobContentProvider"
    34             android:authorities="com.mingrisoft.cooking.BmobContentProvider"> //这里要改为自己的包名
    35         </provider>
    36     </application>
    37 
    38 </manifest>

    activity_main.xml:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:app="http://schemas.android.com/apk/res-auto"
     4     xmlns:tools="http://schemas.android.com/tools"
     5     android:layout_width="match_parent"
     6     android:layout_height="match_parent"
     7     android:orientation="vertical"
     8     tools:context=".MainActivity">
     9 
    10     <RelativeLayout
    11         android:layout_width="match_parent"
    12         android:layout_height="wrap_content">
    13 
    14         <ImageView
    15             android:id="@+id/iv_hf_cook"
    16             android:layout_width="wrap_content"
    17             android:layout_height="wrap_content"
    18             android:adjustViewBounds="true"
    19             android:maxWidth="100dp"
    20             android:maxHeight="100dp"
    21             android:src="@drawable/cooking"/>
    22 
    23         <TextView
    24             android:id="@+id/tv_hf_title"
    25             android:layout_toRightOf="@+id/iv_hf_cook"
    26             android:text="厨艺讨论区"
    27             android:textSize="20sp"
    28             android:textStyle="bold"
    29             android:layout_width="match_parent"
    30             android:layout_height="wrap_content" />
    31 
    32         <TextView
    33             android:layout_toRightOf="@+id/iv_hf_cook"
    34             android:layout_below="@+id/tv_hf_title"
    35             android:layout_width="wrap_content"
    36             android:layout_height="wrap_content"
    37             android:text="论坛介绍ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
    38 fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"/>
    39     </RelativeLayout>
    40 
    41     <View
    42         android:layout_width="match_parent"
    43         android:layout_height="5px"
    44         android:background="#dadef7"></View>
    45 
    46     <ScrollView
    47         android:layout_width="match_parent"
    48         android:layout_height="wrap_content">
    49     </ScrollView>
    50 
    51     <ListView
    52         android:id="@+id/lv_hf_listview"
    53         android:layout_width="match_parent"
    54         android:layout_height="300dp"
    55         android:dividerHeight="5dp"
    56         android:layout_weight="1">
    57     </ListView>
    58 
    59     <View
    60         android:layout_width="match_parent"
    61         android:layout_height="5px"
    62         android:background="#dadef7"></View>
    63 
    64     <LinearLayout
    65         android:layout_width="match_parent"
    66         android:layout_height="wrap_content">
    67 
    68         <EditText
    69             android:id="@+id/et_hf_content"
    70             android:layout_width="match_parent"
    71             android:layout_height="wrap_content"
    72             android:hint="发表东西吧"
    73             android:maxLines="2"
    74             android:layout_weight="1"/>
    75 
    76         <Button
    77             android:id="@+id/btn_hf_send"
    78             android:layout_width="wrap_content"
    79             android:layout_height="wrap_content"
    80             android:text="发送"/>
    81     </LinearLayout>
    82 
    83 </LinearLayout>

    content.xml:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <RelativeLayout
     3     xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
     4     android:layout_height="match_parent">
     5 
     6     <View
     7         android:id="@+id/splitLine"
     8         android:layout_width="match_parent"
     9         android:layout_height="5px"
    10         android:paddingTop="10dp"
    11         android:paddingBottom="10dp"
    12         android:background="#dadef7"></View>
    13 
    14     <ImageView
    15         android:id="@+id/headImage"
    16         android:layout_below="@+id/splitLine"
    17         android:layout_width="wrap_content"
    18         android:layout_height="wrap_content"
    19         android:src="@drawable/register"
    20         android:maxHeight="100dp"
    21         android:adjustViewBounds="true"
    22         android:maxWidth="100dp"/>
    23 
    24     <TextView
    25         android:id="@+id/content_name"
    26         android:maxWidth="100dp"
    27         android:layout_below="@+id/headImage"
    28         android:layout_width="wrap_content"
    29         android:layout_height="wrap_content"
    30         android:text="hemeiwolong"/>
    31 
    32     <TextView
    33         android:id="@+id/content_date"
    34         android:layout_below="@+id/content_name"
    35         android:layout_width="wrap_content"
    36         android:layout_height="wrap_content"
    37         android:maxWidth="100dp"
    38         android:text="xxxx/xx/xx hh:mm:ss"/>
    39 
    40     <TextView
    41         android:id="@+id/content_content"
    42         android:layout_marginLeft="110dp"
    43         android:layout_width="match_parent"
    44         android:layout_height="wrap_content"
    45         android:text="fffffffffffffffffffffffffffffffffff
    46 "/>
    47 
    48 </RelativeLayout>
    CookContent:
     1 package com.mingrisoft.cooking;
     2 
     3 import cn.bmob.v3.BmobObject;
     4 
     5 public class CookContent extends BmobObject {
     6     private String name;
     7     private String date;
     8     private String content;
     9 
    10     public String getName() {
    11         return name;
    12     }
    13     public void setName(String name) {
    14         this.name = name;
    15     }
    16     public String getDate() {
    17         return date;
    18     }
    19     public void setDate(String address) {
    20         this.date = address;
    21     }
    22 
    23     public String getContent() {
    24         return content;
    25     }
    26     public void setContent(String content) {
    27         this.content = content;
    28     }
    29 }
    MainActivity:
      1 package com.mingrisoft.cooking;
      2 
      3 import android.app.Activity;
      4 import android.support.v7.app.AppCompatActivity;
      5 import android.os.Bundle;
      6 import android.util.Log;
      7 import android.view.View;
      8 import android.widget.Button;
      9 import android.widget.EditText;
     10 import android.widget.ListView;
     11 import android.widget.SimpleAdapter;
     12 import android.widget.Toast;
     13 
     14 import java.text.SimpleDateFormat;
     15 import java.util.ArrayList;
     16 import java.util.Date;
     17 import java.util.HashMap;
     18 import java.util.List;
     19 import java.util.Map;
     20 
     21 import cn.bmob.v3.Bmob;
     22 import cn.bmob.v3.BmobQuery;
     23 import cn.bmob.v3.datatype.BmobQueryResult;
     24 import cn.bmob.v3.exception.BmobException;
     25 import cn.bmob.v3.listener.FindListener;
     26 import cn.bmob.v3.listener.SQLQueryListener;
     27 import cn.bmob.v3.listener.SaveListener;
     28 
     29 public class MainActivity extends Activity {
     30 
     31     @Override
     32     protected void onCreate(Bundle savedInstanceState) {
     33         super.onCreate(savedInstanceState);
     34         setContentView(R.layout.activity_main);
     35 
     36         //初始化 Bmob SDK,第一个参数为上下文,第二个参数为Application ID
     37         Bmob.initialize(this, "1368b7fd0a523efab971eb807e1b9bc2");
     38 
     39         Button send = findViewById(R.id.btn_hf_send);
     40         EditText et_hf_content = findViewById(R.id.et_hf_content);
     41         ListView listView = findViewById(R.id.lv_hf_listview);
     42 
     43         BmobQuery<CookContent> query = new BmobQuery<CookContent>();
     44 
     45         String bql ="select * from CookContent";//查询所有的游戏得分记录
     46         new BmobQuery<CookContent>().doSQLQuery(bql,new SQLQueryListener<CookContent>(){
     47 
     48             @Override
     49             public void done(BmobQueryResult<CookContent> result, BmobException e) {
     50                 if(e ==null){
     51                     List<CookContent> list = (List<CookContent>) result.getResults();
     52                     if(list!=null && list.size()>0){
     53                         ArrayList<Map<String, String>> resultList = new ArrayList<>();
     54 
     55                         for (CookContent cookContent : list) {
     56                             Map<String, String> map = new HashMap<>();
     57                             map.put("name", cookContent.getName());
     58                             map.put("date", cookContent.getDate());
     59                             map.put("content", cookContent.getContent());
     60                             resultList.add(map);
     61                         }
     62 
     63                         SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, resultList,
     64                                 R.layout.content, new String[]{"name", "date", "content"},
     65                                 new int[] {R.id.content_name, R.id.content_date, R.id.content_content});
     66                         listView.setAdapter(simpleAdapter);
     67                     } else {
     68                         Log.i("smile", "查询成功,无数据返回");
     69                         Toast.makeText(MainActivity.this, "讨论区还没内容哦!", Toast.LENGTH_LONG).show();
     70                     }
     71                 } else {
     72                     Log.i("smile", "错误码:"+e.getErrorCode()+",错误描述:"+e.getMessage());
     73                     Toast.makeText(MainActivity.this, "错误码:"+e.getErrorCode()+",错误描述:"+e.getMessage(), Toast.LENGTH_LONG).show();
     74                 }
     75             }
     76         });
     77 
     78         send.setOnClickListener(new View.OnClickListener() {
     79             @Override
     80             public void onClick(View view) {
     81                 String content = et_hf_content.getText().toString();
     82                 if (content.equals("")) {
     83                     Toast.makeText(MainActivity.this, "发送内容为空!", Toast.LENGTH_LONG).show();
     84                 } else {
     85                     CookContent cookContent = new CookContent();
     86                     cookContent.setName("hemeiwolong");
     87                     Date date = new Date();
     88                     SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
     89                     cookContent.setDate(simpleDateFormat.format(date));
     90                     cookContent.setContent(content);
     91                     cookContent.save(new SaveListener<String>() {
     92                         @Override
     93                         public void done(String objectId,BmobException e) {
     94                             if(e==null){
     95                                 Toast.makeText(MainActivity.this, "添加数据成功,返回objectId为:"+objectId, Toast.LENGTH_LONG).show();
     96                                 onCreate(null);
     97                             }else{
     98                                 Toast.makeText(MainActivity.this, "创建数据失败:" + e.getMessage(), Toast.LENGTH_LONG).show();
     99                             }
    100                         }
    101                     });
    102                 }
    103             }
    104         });
    105     }
    106 }
  • 相关阅读:
    移动端和pc端的判断,不同端做不同的处理
    easyUI combobox的使用
    [Codeforces #196] Tutorial
    [Codeforces #174] Tutorial
    [Codeforces #190] Tutorial
    [Codeforces #211] Tutorial
    [Codeforces #192] Tutorial
    [BZOJ 3196] 二逼平衡树
    [BZOJ 1058] 报表统计
    [P3709] 大爷的字符串题
  • 原文地址:https://www.cnblogs.com/hemeiwolong/p/12888213.html
Copyright © 2011-2022 走看看