Android——“i分享”APP開發Day11

通過前面一篇,“我的”頁面的後臺就搭建完整了,接下來就繼續處理一下Android端下“我的”頁面剩餘還未完成的功能——修改密碼、查看我的分享、查看我的日記、查看我的收藏、查看iShare相關

1.修改密碼的頁面設計——activity_change_password.xml

  • 頁面包括舊密碼輸入框、新密碼輸入框,具體代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#eeeeee">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="@color/color_minefragment_top"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:text="修改密碼"
            android:textColor="#fff"
            android:textSize="20dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            />
    </RelativeLayout>
    <LinearLayout
        android:id="@+id/password_change_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="120dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="舊 密 碼:"

                android:textSize="@dimen/textSize_normal"/>
            <EditText
                android:id="@+id/et_old_login_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="@dimen/textSize_normal"
                android:inputType="textPassword"

                android:hint="這裏填寫舊密碼"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="12dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="@dimen/textSize_normal"
                android:text="新 密 碼:"/>
            <EditText
                android:id="@+id/et_new_login_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="@dimen/textSize_normal"
                android:inputType="textPassword"
                android:hint="這裏填寫新密碼"/>
        </LinearLayout>
    </LinearLayout>


    <Button
        android:id="@+id/bt_change_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/password_change_layout"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="150dp"
        android:layout_marginRight="150dp"
        android:background="@drawable/selector_loginactivity_button"
        android:text="修改"
        android:textColor="#fff"
        android:gravity="center"
        android:onClick="onClick"
        />
    <TextView
        android:id="@+id/bt_changePassword_view_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginVertical="30dp"

        android:text="信息反饋"
        android:textColor="#B3B3B3"
        android:gravity="center"
        />
</RelativeLayout>

2.修改密碼頁面的邏輯

  • 在之前創建好的com.example.discover包中創建class命名爲ChangePassword.java
  • 在AndroidManifest.xml中添加配置:
<activity android:name=".ChangePassword"
    android:label="更改密碼">
</activity>
  • ChangePassword.java文件處理的邏輯就是獲取到用戶的暱稱、用戶輸入的舊密碼、用戶輸入的新密碼,然後請求已經創建好的servlet——ChangePassword,然後得到是否修改成功的返回信息,ChangePassword.java文件具體代碼如下
package com.example.discover;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class ChangePassword extends Activity {
    EditText old_pass_et;   //舊密碼
    EditText new_pass_et;   //新密碼
    Button sure_change_pass;   //確定修改
    String baseUrl = "http://10.0.2.2:8080/iShareService/servlet/";   //web服務器的地址
    String imgBaseUrl = "http://10.0.2.2:8080/iShareService/images/";  //圖片資源
    String userName,oldPass,newPass;   //存放用戶基本信息

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);

        old_pass_et = (EditText) findViewById(R.id.et_old_login_password); //新密碼
        new_pass_et = (EditText) findViewById(R.id.et_new_login_password); //新密碼
        sure_change_pass = (Button)findViewById(R.id.bt_change_password) ;//確定修改密碼的按鈕

        setBaseInfo();
        sure_change_pass.setOnClickListener(new ChangePassword.onClick());
    }

    class onClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            oldPass = old_pass_et.getText().toString();
            newPass = new_pass_et.getText().toString();
            StringBuilder stringBuilder = new StringBuilder();
            BufferedReader buffer = null;
            HttpURLConnection connGET = null;
            if(oldPass.length()==0||newPass.length()==0){
                Toast.makeText(ChangePassword.this, "密碼不能爲空", Toast.LENGTH_LONG).show();
                return;
            }

            try {
                String registerUrl = baseUrl+"ChangePassword?oldPass="+oldPass+"&newPass="+newPass+"&username="+userName;
                URL url = new URL(registerUrl);
                connGET = (HttpURLConnection) url.openConnection();
                connGET.setConnectTimeout(5000);
                connGET.setRequestMethod("GET");
                if (connGET.getResponseCode() == 200) {
                    buffer = new BufferedReader(new InputStreamReader(connGET.getInputStream()));
                    for (String s = buffer.readLine(); s != null; s = buffer.readLine()) {
                        stringBuilder.append(s);
                    }
                    Toast.makeText(ChangePassword.this, stringBuilder.toString(), Toast.LENGTH_LONG).show();

                    //通過handler設置延時1秒後執行r任務,跳轉到我的頁面
                    new Handler().postDelayed(new ChangePassword.LoadMainTask(),500);
                    buffer.close();
                }else{
                    Toast.makeText(ChangePassword.this,"非200.."+connGET.getResponseCode() , Toast.LENGTH_LONG).show();
                }
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(ChangePassword.this, "get 提交 err.." + e.toString(), Toast.LENGTH_LONG).show();
            }


        }
    }

    //啓動線程,加載我的頁面
    private class LoadMainTask implements Runnable{
        @Override
        public void run() {
            Intent intent = new Intent(ChangePassword.this,MyTabFragment.class);
            /*startActivity(intent);*/
            setResult(2, intent); //這裏的2就對應到onActivityResult()方法中的resultCode
            finish();
        }
    }

    //獲取用戶信息
    public void setBaseInfo(){
        //獲取最新的登錄狀態,SharePreferences爲永久存儲,需要手動退出
        SharedPreferences sp = ChangePassword.this.getSharedPreferences("save", Context.MODE_PRIVATE);
        userName = sp.getString("name","");
    }
}

 

3.我的分享、我的日記、我的收藏 頁面設計

  • 這三個功能按鈕都是跳轉到的頁面其實是同一個,只不過內容獲取是不一樣的,所以在這裏只需要寫一個詳情頁就可以了
  • 在layout下新建activity_about_me.xml文件,在該佈局下就只需要要一個ListView就可以了,詳細代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:orientation="vertical"
        android:paddingTop="20dp"
        android:paddingBottom="20dp">

        <ListView
            android:id="@+id/my_share_listView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:dividerHeight="15dp" />
    </LinearLayout>
</LinearLayout>

4.我的分享、我的日記、我的收藏 頁面的邏輯

  • 在已經創建好的com.example.discover包中創建class——MyShareList.java
  • 因爲是跳轉頁面,所以在AndroidMainifest.xml中要添加配置
<activity android:name=".MyShareList"
    android:label="關於我的">
</activity>
  • 該文件處理的主要部分就是,獲取到上一個頁面傳遞的type(用於判斷請求的是我的分享、我的日記還是我的收藏),然後獲取到該用戶的暱稱,通過這兩個參數請求後臺的servlet——QueryAboutMe.java,返回相對應類型的內容,MyShareList.java具體代碼如下:
package com.example.discover;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bean.FindInfo;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;

public class MyShareList extends Activity implements AbsListView.OnScrollListener{
    Activity mActivity;     //存放當前的activity
    String baseUrl = "http://10.0.2.2:8080/iShareService/servlet/";   //web服務器的地址
    String imgBaseUrl = "http://10.0.2.2:8080/iShareService/images/";  //圖片資源
    int requestType=0;   //請求類型,0:我的分享、1:我的日記;2:我的收藏
    String userName="";
    String requestUrl="";


    private TextView title;
    private MyShareList.PaginationAdapter adapterAction;
    private ScheduledExecutorService scheduledExecutorService;

    private ListView listView;
    private int page = 1; //請求頁數
    private int count = 10; //每次請求的數量
    private int visibleLastIndex = 0;  //最後的可視項索引
    private int visibleItemCount;    // 當前窗口可見項總數
    private int dataSize = 28;     //數據集的條數
    private MyShareList.PaginationAdapter adapter;
    private View loadMoreView;
    private Button loadMoreButton;
    private Handler handler = new Handler();

    //返回測試信息
    /*TextView testTxt;*/
    public  void refreshDiscover(){
        page = 1;
        visibleLastIndex = 0;
        listView.setAdapter(null);   //將ListView控件的內容清空
        initializeAdapter();
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_me);
        mActivity = MyShareList.this;

        //取得啓動該Activity的Intent對象
        Intent intent =getIntent();
        //獲取最新的登錄狀態,SharePreferences爲永久存儲,需要手動退出
        SharedPreferences sp = MyShareList.this.getSharedPreferences("save", Context.MODE_PRIVATE);
        userName = sp.getString("name","");

        requestType =intent.getIntExtra("requestType",0);
        //取出Intent中附加的數據
        if(requestType==0){
            requestUrl = baseUrl+"QueryAboutMe?username="+userName+"&type=0&page="+page+"&count="+count;
        }else if(requestType==1){
            requestUrl = baseUrl+"QueryAboutMe?username="+userName+"&type=1&page="+page+"&count="+count;
        }else{
            requestUrl = baseUrl+"QueryAboutMe?username="+userName+"&type=2&page="+page+"&count="+count;
        }

        loadMoreView = getLayoutInflater().inflate(R.layout.activity_find_loadmore, null);
        loadMoreButton = (Button)loadMoreView.findViewById(R.id.loadMoreButton);
        loadMoreButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                loadMoreButton.setText("正在加載中...");  //設置按鈕文字
                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        loadMoreData();
                        adapter.notifyDataSetChanged();
                        loadMoreButton.setText("查看更多..."); //恢復按鈕文字
                    }
                },2000);

            }
        });

        //返回測試信息
        /* testTxt = (TextView)getActivity().findViewById(R.id.test_discover_tab);*/

        listView = (ListView)findViewById(R.id.my_share_listView);
        listView.addFooterView(loadMoreView);  //設置列表底部視圖
        initializeAdapter();
        listView.setAdapter(adapter);
        listView.setOnScrollListener(this);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView getTvId = (TextView)view.findViewById(R.id.cardId);
                /*Toast.makeText(mActivity,"點擊了"+getTvId.getText().toString()+"id", Toast.LENGTH_SHORT).show();*/
                SharedPreferences sp = mActivity.getSharedPreferences("save", Context.MODE_PRIVATE);
                Boolean isLogin = sp.getBoolean("isLogin",false);

                if(isLogin){
                    //點擊了進入詳情
                    Intent display_info_intent = new Intent();
                    display_info_intent.setClass(mActivity, DisplayDetail.class);  //創建intent對象,並制定跳轉頁面
                    display_info_intent.putExtra("infoId",getTvId.getText().toString());
                    mActivity.setResult(1, display_info_intent); //這裏的1就對應到onActivityResult()方法中的resultCode
                    startActivity(display_info_intent);    //跳轉到詳情頁面
                }else{
                    //尚未登錄
                    Toast.makeText(MyShareList.this,"請先登錄", Toast.LENGTH_LONG).show();
                }

            }
        });
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        int itemsLastIndex = adapter.getCount()-1; //數據集最後一項的索引
        int lastIndex = itemsLastIndex + 1;
        if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE
                && visibleLastIndex == lastIndex) {
            // 如果是自動加載,可以在這裏放置異步加載數據的代碼
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {
        this.visibleItemCount = visibleItemCount;
        visibleLastIndex = firstVisibleItem + visibleItemCount - 1;  //最後的可視項索引

        //如果所有的記錄選項等於數據集的條數,則移除列表底部視圖
        if(totalItemCount == dataSize+1){
            listView.removeFooterView(loadMoreView);
            Toast.makeText(MyShareList.this,  "數據全部加載完!", Toast.LENGTH_LONG).show();
        }
    }
    /**
     * 查詢數據庫中的數據
     */
    private JSONArray loadDataFromDataBase(String loadDataUrl){
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader buffer = null;
        HttpURLConnection connGET = null;

        try {
            URL url = new URL(loadDataUrl);
            connGET = (HttpURLConnection) url.openConnection();
            connGET.setConnectTimeout(5000);
            connGET.setRequestMethod("GET");
            if (connGET.getResponseCode() == 200) {
                buffer = new BufferedReader(new InputStreamReader(connGET.getInputStream()));
                for (String s = buffer.readLine(); s != null; s = buffer.readLine()) {
                    stringBuilder.append(s);
                }

                if(stringBuilder.toString()!="null"){
                    //返回測試信息
                    JSONArray jsonArray = new JSONArray(stringBuilder.toString());

                    //獲取到的數據,對Json進行解析
                    page = page+1;  //一次成功請求後更新請求頁面
                    buffer.close();
                    return jsonArray;
                }else{
                    return null;
                }

            }else{
                Toast.makeText(MyShareList.this,"非200", Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(MyShareList.this, "get 提交 err.." + e.toString(), Toast.LENGTH_LONG).show();
        }
        return null;
    }

    //初始化將詳情設置到FindInfo bean中
    public List<FindInfo> initSetDataToBean(String detail){
        List<FindInfo> findInfo = new ArrayList<FindInfo>();
        try {
            JSONArray detailJsonArray = new JSONArray(detail);
            for (int i = 0; i < detailJsonArray.length(); i++) {
                FindInfo items = new FindInfo();

                JSONObject temp = (JSONObject) detailJsonArray.get(i);

                Integer infoId = temp.getInt("infoId");    //內容ID
                String infoTitle = temp.getString("infoTitle");   //內容標題
                String infoDescribe = temp.getString("infoDescribe");   //內容簡述
                String infoDetail = temp.getString("infoDetail");   //內容詳情
                Integer type = temp.getInt("infoType");    //類型:0表示日記,1表示趣事
                Integer support = temp.getInt("infoSupport");   //點贊數
                String infoAuthor = temp.getString("infoAuthor");  //作者

                items.setInfoId(infoId);
                items.setInfoTitle(infoTitle);
                items.setInfoDescribe(infoDescribe);
                items.setInfoDetail(infoDetail);
                items.setType(type);
                items.setSupport(support);
                items.setInfoAuthor(infoAuthor);

                findInfo.add(items);
            }
            return findInfo;

        }catch (JSONException e){
            Toast.makeText(MyShareList.this, "initSetDataToBean異常 err.." + e.toString(), Toast.LENGTH_LONG).show();
            return null;
        }
    }


    //加載更多將詳情設置到FindInfo bean中
    public void loadMoreSetDataToBean(String detail){

        try {
            JSONArray detailJsonArray = new JSONArray(detail);

            for (int i = 0; i < detailJsonArray.length(); i++) {
                FindInfo items = new FindInfo();
                JSONObject temp = (JSONObject) detailJsonArray.get(i);
                Integer infoId = temp.getInt("infoId");    //內容ID
                String infoTitle = temp.getString("infoTitle");   //內容標題
                String infoDescribe = temp.getString("infoDescribe");   //內容簡述
                String infoDetail = temp.getString("infoDetail");   //內容詳情

                Integer type = temp.getInt("infoType");    //類型:0表示日記,1表示趣事
                Integer support = temp.getInt("infoSupport");   //點贊數
                String infoAuthor = temp.getString("infoAuthor");  //作者

                items.setInfoId(infoId);
                items.setInfoTitle(infoTitle);
                items.setInfoDescribe(infoDescribe);
                items.setInfoDetail(infoDetail);
                items.setType(type);
                items.setSupport(support);
                items.setInfoAuthor(infoAuthor);

                adapter.addNewsItem(items);   //與初始化是有差異的
            }

        }catch (JSONException e){
            Toast.makeText(MyShareList.this, "loadMoreSetDataToBean異常 err.." + e.toString(), Toast.LENGTH_LONG).show();
        }

    }
    /**
     * 初始化ListView的適配器,即打開頁面展示的數據
     */
    private void initializeAdapter(){
        // 設置線程策略
        setVersion();
        JSONArray jsonArray = loadDataFromDataBase(requestUrl);

        if(jsonArray!=null){
            try {
                JSONObject totalObject = (JSONObject)jsonArray.get(0);

                dataSize = totalObject.getInt("totalRecord");  //總記錄數
                String detail= totalObject.getString("RecordDetail");   //詳情

                if(initSetDataToBean(detail)!=null) {
                    adapter = new MyShareList.PaginationAdapter(initSetDataToBean(detail));   //將詳情設置到bean中
                }

            }catch (JSONException e){
                Toast.makeText(MyShareList.this, "initializeAdapter異常 err.." + e.toString(), Toast.LENGTH_LONG).show();
            }
        }else{
            listView.removeFooterView(loadMoreView);
            Toast.makeText(MyShareList.this, "查詢爲空" , Toast.LENGTH_LONG).show();
        }
    }

    //APP如果在主線程中請求網絡操作,將會拋出異常,所以需要用線程來操作網絡請求
    void setVersion() {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads()
                .detectDiskWrites()
                .detectNetwork()
                .penaltyLog()
                .build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedSqlLiteObjects() //探測SQLite數據庫操作
                .penaltyLog() //打印logcat
                .penaltyDeath()
                .build());
    }



    /**
     * 加載更多數據
     */
    private void loadMoreData(){
        JSONArray jsonArray = loadDataFromDataBase(requestUrl);

        try {
            JSONObject total = (JSONObject) jsonArray.get(0);
            dataSize = total.getInt("totalRecord");  //總記錄數
            String detail= total.getString("RecordDetail");   //詳情

            loadMoreSetDataToBean(detail);   //將更多的詳情設置到bean中
        }catch (JSONException e){
            Toast.makeText(MyShareList.this, "loadMoreData()異常 err.." + e.toString(), Toast.LENGTH_LONG).show();
        }

    }


    /**
     * 將一組數據傳到ListView等UI顯示組件
     */
    class PaginationAdapter extends BaseAdapter {

        List<FindInfo> newsItems;

        public PaginationAdapter(List<FindInfo> newsitems) {
            this.newsItems = newsitems;
        }

        @Override
        public int getCount() {
            return newsItems.size();
        }

        @Override
        public Object getItem(int position) {
            return newsItems.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }


        //在這裏將Item設置到每個卡片
        @Override
        public View getView(int position, View view, ViewGroup parent) {
            if (view == null) {
                view = getLayoutInflater().inflate(R.layout.activity_find_list_item, null);
            }

            //標題
            TextView tvTitle = (TextView) view.findViewById(R.id.cardTitle);
            tvTitle.setText(newsItems.get(position).getInfoTitle());

            //文章ID
            TextView tvId = (TextView) view.findViewById(R.id.cardId);
            tvId.setText(newsItems.get(position).getInfoId().toString());

            int type = newsItems.get(position).getType();

            if (type == 0 || type == 3) {
                ImageView cardImgTip = (ImageView) view.findViewById(R.id.cardImgTip);
                if (type == 0) {
                    cardImgTip.setImageResource(R.drawable.share_funny_select);
                } else {
                    cardImgTip.setImageResource(R.drawable.share_diary_select);
                }

                TextView tvContent = (TextView) view.findViewById(R.id.cardContent);
                tvContent.setVisibility(View.VISIBLE);  //顯示文字框

                ImageView ivContent = (ImageView) view.findViewById(R.id.cardContent_pic);
                ivContent.setVisibility(View.GONE);   //設置圖片顯示框隱藏

                LinearLayout layoutMusic = (LinearLayout) view.findViewById(R.id.cardContent_music);
                layoutMusic.setVisibility(View.GONE);   //設置音樂顯示框隱藏

                //分享的普通內容

                tvContent.setText(newsItems.get(position).getInfoDescribe());
            } else if (type == 1) {
                ImageView cardImgTip = (ImageView) view.findViewById(R.id.cardImgTip);
                cardImgTip.setImageResource(R.drawable.share_pic_select);
                //分享圖片
                TextView tvContent = (TextView) view.findViewById(R.id.cardContent);
                tvContent.setVisibility(View.GONE);  //隱藏文字框
                LinearLayout layoutMusic = (LinearLayout) view.findViewById(R.id.cardContent_music);
                layoutMusic.setVisibility(View.GONE);   //設置音樂顯示框隱藏

                ImageView ivContent = (ImageView) view.findViewById(R.id.cardContent_pic);
                //通過網絡鏈接獲取圖片
                Bitmap one;
                String describeUrl = newsItems.get(position).getInfoDescribe().trim();
                try {
                    one = LoadImgByNet.getBitmap(imgBaseUrl + describeUrl);  //設置圖片
                    ivContent.setImageBitmap(one);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                ivContent.setVisibility(View.VISIBLE);   //設置圖片顯示框

            } else if (type == 2) {
                ImageView cardImgTip = (ImageView) view.findViewById(R.id.cardImgTip);
                cardImgTip.setImageResource(R.drawable.share_music_select);

                //分享的音樂
                TextView tvContent = (TextView) view.findViewById(R.id.cardContent);
                tvContent.setVisibility(View.GONE);  //顯示文字框
                ImageView ivContent = (ImageView) view.findViewById(R.id.cardContent_pic);
                ivContent.setVisibility(View.GONE);   //設置圖片顯示框隱藏

                LinearLayout layoutMusic = (LinearLayout) view.findViewById(R.id.cardContent_music);

                TextView tvMusicContent = (TextView) view.findViewById(R.id.cardTitle_music);
                tvMusicContent.setText(newsItems.get(position).getInfoDescribe());

                layoutMusic.setVisibility(View.VISIBLE);   //設置音樂顯示框顯示
            }

            return view;
        }

        /**
         * 添加數據列表項
         *
         * @param infoItem
         */
        public void addNewsItem(FindInfo infoItem) {
            newsItems.add(infoItem);
        }
        }
    }

 

5.iShare相關頁面設計

  • 在layout下新建文件activity_about_ishare.xml,具體代碼如下
  • 這個頁面設計比較簡單,主要就是介紹iShare應用的開發目的和實現功能,沒有功能,只是展示信息
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fadingEdge="none" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:paddingLeft="14dp"
            android:paddingRight="14dp"
            android:paddingBottom="60dp"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="22dp"
                android:textStyle="bold"
                android:text="關於iShare" />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:textSize="18sp"
                    android:layout_weight="1"
                    android:textColor="#000000"
                    android:text="開發者學號:\n
                    201611671208\n
                    201611671206\n
                    201611671221\n
                     " />

            </LinearLayout>

            <TextView
                android:background="@drawable/list_card_style"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/margin_10"
                android:padding="10dp"
                android:textSize="18sp"
                android:textColor="#000000"
                android:text="iShare應用:發現有趣的人和事,在這個平臺可以發佈自己感興趣的內容以及查看大家的分享,簡歷良好的交流氛圍"/>
            <TextView
                android:id="@+id/detail_info_detail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/margin_10"
                android:padding="10dp"
                android:textSize="18sp"
                android:textColor="#000000"
                android:singleLine="false"
                android:text="  開發目的:Android課程設計\n\n

開發時間:2019年5月\n\n

素材來源:LOGO是網絡資源圖,Icon來自www.iconfont.cn,其中用於測試的圖片均來自網絡\n\n

項目代碼:部分結構參考自CSDN、GitHub\n\n

實現功能:起始頁、註冊、登錄、更新簽名、更改密碼、瀏覽分享、按標題和簡述關鍵字查找分享的內容、根據用戶暱稱查找用戶、查看分享詳情、點贊、收藏、發佈四種類型的內容、查看按點贊數排行前十的內容、刪除發佈的內容、評論、查看我的分享、查看我的日記、查看我的收藏" />


        </LinearLayout>
    </ScrollView>
</LinearLayout>

6.“我的”頁面跳轉事件

  • 打開之前創建好的MyTabFragment.java文件,給對應的五個Button——“修改密碼”、“查看我的分享”、“查看我的日記”、“查看我的收藏”、“查看iShare相關”添加點擊監聽
//“我的”頁面中相關點擊事件
    class mClick implements View.OnClickListener {

        public void onClick(View v) {
            if(isLogin) {
                if (v == account_set_bt) {
                    //點擊了賬號管理
                    Intent account_manage_intent = new Intent();
                    account_manage_intent.setClass(mActivity, AccountManagement.class);  //創建intent對象,並制定跳轉頁面
                    startActivity(account_manage_intent);    //跳轉到賬號管理頁面

                /*TextView textView = (TextView) getActivity().findViewById(R.id.test_text_view);  //用於放測試的提示信息
                Toast.makeText(getActivity(), textView.getText(), Toast.LENGTH_LONG).show();*/
                }else if(v == signature_set_bt){
                    //點擊了更改簽名
                    Intent signature_manage_intent = new Intent();
                    signature_manage_intent.setClass(mActivity, ChangeSignature.class);  //創建intent對象,並指定跳轉頁面
                    //startActivity(signature_manage_intent);    //跳轉到更新簽名頁面
                    startActivityForResult(signature_manage_intent,1);   //需要返回該頁面
                }else if(v == password_set_bt){
                    //點擊了更改密碼
                    Intent password_manage_intent = new Intent();
                    password_manage_intent.setClass(mActivity, ChangePassword.class);  //創建intent對象,並指定跳轉頁面
                    /*startActivity(password_manage_intent);   */
                    startActivityForResult(password_manage_intent,2);   // 跳轉到修改密碼頁面
                }else if(v == share_list_bt){
                    //點擊了我的分享
                    Intent intent = new Intent();
                    intent.setClass(mActivity, MyShareList.class);  //創建intent對象,並指定跳轉頁面
                    intent.putExtra("requestType",0);
                    mActivity.setResult(4, intent); //這裏的4就對應到onActivityResult()方法中的resultCode
                    startActivity(intent);    //跳轉到list
                }else if(v==diary_list_bt){
                    //點擊了我的日記
                    Intent intent = new Intent();
                    intent.setClass(mActivity, MyShareList.class);  //創建intent對象,並指定跳轉頁面
                    intent.putExtra("requestType",1);
                    mActivity.setResult(5, intent); //這裏的4就對應到onActivityResult()方法中的resultCode
                    startActivity(intent);    //跳轉到list
                }else if(v==focus_list_bt){
                    //點擊了我的收藏
                    Intent intent = new Intent();
                    intent.setClass(mActivity, MyShareList.class);  //創建intent對象,並指定跳轉頁面
                    intent.putExtra("requestType",2);
                    mActivity.setResult(6, intent); //這裏的4就對應到onActivityResult()方法中的resultCode
                    startActivity(intent);    //跳轉到list
                } else if(v==about_iShare){
                    //點擊了關於iShare
                    Intent about_iShare_intent = new Intent();
                    about_iShare_intent.setClass(mActivity, AboutIShare.class);  //創建intent對象,並指定跳轉頁面
                    startActivity(about_iShare_intent);    //跳轉到iShare介紹頁面
                }
            }else{

                Toast.makeText(getActivity(), "請先登錄!", Toast.LENGTH_LONG).show();
            }
        }
    }

 

——到這整個iShare項目就開發完成了,整個過程可能會有部分代碼或者配置不是很完整,接下來將會分模塊寫一下整個項目的詳細代碼,以及如何運行整個項目 和運行的結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章