課表簡單查詢代碼

MainActivity.java

package com.example.gridview;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   private List<String> data_list;
   private ArrayAdapter<String> arr_adapter;
   private Spinner spinner;
   private List<String> list;
   private ArrayAdapter<String> adapter;
   private static final String TAG ="MainActivity" ;
   //學(xué)期
   private Spinner mSpisemster;
  // private EditText mEtsemster;
   //班級
   private EditText mEtclassName;
   //查詢按鍵
   private Button mBtnLogin;
   private TextView mTvResult;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       spinner = (Spinner) this.findViewById(R.id.spinner1);
       ArrayAdapter adapter = new ArrayAdapter(this,
               android.R.layout.simple_spinner_item, new String[] { " ","16-17-2", "16-17-1",
               "15_16-2","15_16-1","14_15-2","14_15-1","13_14-2"
               ,"13_14-1","12_13-2","12_13-1","11_12-2","11_12-1","10_11-2","10_11-1","09_10-2","09_10-1","08_09-2","08_09-1","07_08-2"
               ,"07_08-1","06_07-2","06_07-1","05_06-2","05_06-1","04_05-2","04_05-1","03_04-2","03_04-1","02_03-2","02_03-1","01_02-2"
               ,"01_02-1" });

       //設(shè)置下拉樣式
       adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
       spinner.setAdapter(adapter);
       initView();
       initListener();
   }
   /**
    * 初始化組件
    */
   private void initView() {
       mSpisemster = (Spinner) findViewById(R.id.spinner1);
      // mEtsemster = (EditText) findViewById(R.id.login_et_name);
       mEtclassName = (EditText) findViewById(R.id.login_et_pwd);
       mBtnLogin = (Button) findViewById(R.id.login_btn_login);
   }

   /**
    * 設(shè)置監(jiān)聽器
    */
   private void initListener() {
       mBtnLogin.setOnClickListener(this);
   }

   /*
   單擊事件監(jiān)聽
    */
   @Override
   public void onClick(View v) {
       if(v==mBtnLogin){
           //final String Semester = mEtsemster.getText().toString().trim();

           final String Semester = mSpisemster.getSelectedItem().toString().trim();
           final String ClassName = mEtclassName.getText().toString().trim();

           if(TextUtils.isEmpty(Semester) || TextUtils.isEmpty(ClassName)){

               Toast.makeText(MainActivity.this, "學(xué)期或班級不能為空", Toast.LENGTH_SHORT).show();
               return;
           }
           Intent intent = new Intent(this, com.example.gridview.ClassActivity.class);
           //向ClassActivity傳參數(shù)
           intent.putExtra("Semester", Semester);
           intent.putExtra("ClassName",ClassName);
           startActivity(intent);
       }
   }
}

BeanTable.java

package com.example.gridview;
import java.lang.reflect.Array;
import static android.R.attr.id;
/**
* Created by a302 on 2017/7/26.
*/
public class BeanTable {
   private String section;
   private String  one;
   private String  two;
   private String  three;
   private String  four;
   private String  five;
   private String  six;
   private String  seven;
   public String getSection() {
       return section;
   }
   public void setSection(String section) {
       this.section = section;
   }
   public String getOne() {
       return one;
   }
   public void setOne(String one) {
       this.one = one;
   }
   public String getTwo() {
       return two;
   }
   public void setTwo(String two) {
       this.two = two;
   }
   public String getThree() {
       return three;
   }
   public void setThree(String three) {
       this.three = three;
   }
   public String getFour() {
       return four;
   }
   public void setFour(String four) {
       this.four = four;
   }
   public String getFive() {
       return five;
   }
   public void setFive(String five) {
       this.five = five;
   }

   public String getSix() {
       return six;
   }
   public void setSix(String six) {
       this.six = six;
   }
   public String getSeven() {
       return seven;
   }
   public void setSeven(String seven) {
       this.seven = seven;
   }
}

ClassActivity.java


package com.example.gridview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.GridView;
import com.example.gridview.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class ClassActivity extends AppCompatActivity {
   private static final String TAG ="ClassActivity" ;
   private GridView gridView;
   private String Semester;
   private String ClassName;
   private final String url ="http://192.168.3.146:8080/Educational/GetJson";
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_class);
       //gridView = (GridView) findViewById(R.id.gridView1);
       gridView = (GridView) findViewById(R.id.gridView);
       Semester = getIntent().getStringExtra("Semester");
       ClassName = getIntent().getStringExtra("ClassName");
       login();
   }
   /*
  查詢
   */
   prinate void login() {
       new Thread(new Runnable() {
           @Override
           public void run() {
               OkHttpClient client = new OkHttpClient();
               Bean bean = new Bean();
               bean.setSemester(Semester);
               bean.setClassName(ClassName);
               Gson gson = new Gson();
               String strJson = gson.toJson(bean);
               //把請求的內(nèi)容字符串轉(zhuǎn)換為json
               MediaType JSON = MediaType.parse("application/json; charset=utf-8");
               RequestBody body = RequestBody.create(JSON, strJson);
               Request request = new Request.Builder()
                       .url(url)
                       .post(body)
                       .build();
               Response response = null;
               try {
                   response = client.newCall(request).execute();
                   String result = response.body().string();
                   List<BeanTable> tables = gson.fromJson(result, new
                           TypeToken<List<BeanTable>>()
                   {}.getType());
                   initGridView(tables);
                   Log.e(TAG, "結(jié)果:" + result);
               } catch (IOException e) {
                   e.printStackTrace();
                   Log.e(TAG, "錯誤:" + e.toString());
               }
               Log.e(TAG, "semester:" + Semester);
               Log.e(TAG, "classname:" + ClassName);
           }
       }).start();
   }
   private void initGridView(final List<BeanTable> tables) {
       runOnUiThread(new Runnable() {
           @Override
           public void run() {
               List<String> strList = new ArrayList<String>();

               for (BeanTable table : tables) {
                   strList.add(table.getSection());
                   strList.add(table.getOne());
                   strList.add(table.getTwo());
                   strList.add(table.getThree());
                   strList.add(table.getFour());
                   strList.add(table.getFive());
                   strList.add(table.getSix());
                   strList.add(table.getSeven());
               }
               GridViewAdapter adapter = new GridViewAdapter(ClassActivity.this, strList);
               gridView.setAdapter(adapter);
           }
       });
   }
}

GridViewAdapter.java

package com.example.gridview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.gridview.R;
import java.util.ArrayList;
import java.util.List;
import static com.example.gridview.R.id.textView;
/**
 * Created by a302 on 2017/7/26.
 */
public class GridViewAdapter extends BaseAdapter {
    private Context mContext;
    private List<String> stringList = new ArrayList<>();
    public GridViewAdapter(Context mContext, List<String> stringList) {
        this.mContext = mContext;
        this.stringList = stringList;
    }
    @Override
    public int getCount() {
        return stringList.size();
    }
    @Override
    public Object getItem(int position) {
        return stringList.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHodle hodle = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_gridview, null);
            hodle = new ViewHodle();
            hodle.textView = (TextView) convertView.findViewById(textView);
            convertView.setTag(hodle);
        } else {
            hodle = (ViewHodle) convertView.getTag();
        }
        hodle.textView.setText(stringList.get(position));
        return convertView;
    }
    class ViewHodle{
        private TextView textView;
    }
}

HttpUtils.java

package com.example.gridview;
import android.util.Log;
import java.io.IOException;
        import android.util.Log;
        import com.google.gson.Gson;
        import java.io.IOException;
        import okhttp3.MediaType;
        import okhttp3.OkHttpClient;
        import okhttp3.Request;
        import okhttp3.RequestBody;
        import okhttp3.Response;
        import okhttp3.ResponseBody;
        import static android.R.attr.password;
/**
 * Created by Administrator on 2016-03-27.
 */
public class HttpUtils {
    OkHttpClient client = new OkHttpClient();
    public static final MediaType JSON
            = MediaType.parse("application/json; charset=utf-8");
    public String login(String url, String json) throws IOException {
        //把請求的內(nèi)容字符串轉(zhuǎn)換 為json
        RequestBody body = RequestBody.create(JSON, json);
        //RequestBody formBody = new FormEncodingBuilder()
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        String result = response.body().string();
        return result;
    }
    public String bolwingJson(Bean bean) {
        Gson gson = new Gson();
        String strJson = gson.toJson(bean);
        Log.e("strJson", strJson);
        return strJson;
    }
}

activity_class.xml

 <?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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#CFCFCF">
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:text=""
            android:textColor="#EE0000"/>

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:text="星期一"
            android:textColor="#EE0000"/>

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:text="星期二"
            android:textColor="#000000"/>

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:text="星期三"
            android:textColor="#EE0000"/>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:text="星期四"
            android:textColor="#000000"/>

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:text="星期五"
            android:textColor="#EE0000"/>

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:text="星期六"
            android:textColor="#000000"/>

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:text="星期日"
            android:textColor="#EE0000"/>
    </LinearLayout>
            <GridView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/bg_border"
                android:numColumns="8"
                android:verticalSpacing="80dp"/>
</LinearLayout>

activity_mian.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10sp"
        android:textColor="#000000"

        />
</LinearLayout>```

item_gridview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="10sp"
    android:textColor="#000000"

    />

</LinearLayout>```
Bean.java

 package com.example.gridview;

/**
 * Created by a302 on 2017/7/26.
 */

public class Bean {
    private String Semester;
    private String ClassName;

    public String getSemester() {
        return Semester;
    }

    public void setSemester(String semester) {
        Semester = semester;
    }

    public String getClassName() {
        return ClassName;
    }

    public void setClassName(String className) {
        ClassName = className;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末循帐,一起剝皮案震驚了整個濱河市溯捆,隨后出現(xiàn)的幾起案子科平,更是在濱河造成了極大的恐慌,老刑警劉巖宰僧,帶你破解...
    沈念sama閱讀 218,525評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡音半,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來曹鸠,“玉大人煌茬,你說我怎么就攤上這事〕固遥” “怎么了坛善?”我有些...
    開封第一講書人閱讀 164,862評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長邻眷。 經(jīng)常有香客問我浑吟,道長,這世上最難降的妖魔是什么耗溜? 我笑而不...
    開封第一講書人閱讀 58,728評論 1 294
  • 正文 為了忘掉前任组力,我火速辦了婚禮,結(jié)果婚禮上抖拴,老公的妹妹穿的比我還像新娘燎字。我一直安慰自己,他們只是感情好阿宅,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,743評論 6 392
  • 文/花漫 我一把揭開白布候衍。 她就那樣靜靜地躺著,像睡著了一般洒放。 火紅的嫁衣襯著肌膚如雪蛉鹿。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,590評論 1 305
  • 那天往湿,我揣著相機(jī)與錄音妖异,去河邊找鬼。 笑死领追,一個胖子當(dāng)著我的面吹牛他膳,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播绒窑,決...
    沈念sama閱讀 40,330評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼棕孙,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了些膨?” 一聲冷哼從身側(cè)響起蟀俊,我...
    開封第一講書人閱讀 39,244評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎订雾,沒想到半個月后肢预,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,693評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡葬燎,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,885評論 3 336
  • 正文 我和宋清朗相戀三年误甚,在試婚紗的時候發(fā)現(xiàn)自己被綠了缚甩。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,001評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡窑邦,死狀恐怖擅威,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情冈钦,我是刑警寧澤郊丛,帶...
    沈念sama閱讀 35,723評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站瞧筛,受9級特大地震影響厉熟,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜较幌,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,343評論 3 330
  • 文/蒙蒙 一揍瑟、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧乍炉,春花似錦绢片、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至槐瑞,卻和暖如春熙涤,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背困檩。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評論 1 270
  • 我被黑心中介騙來泰國打工帅刊, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留八孝,地道東北人犀变。 一個月前我還...
    沈念sama閱讀 48,191評論 3 370
  • 正文 我出身青樓荒给,卻偏偏與公主長得像,于是被迫代替她去往敵國和親显沈。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,955評論 2 355

推薦閱讀更多精彩內(nèi)容

  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程逢唤,因...
    小菜c閱讀 6,419評論 0 17
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理拉讯,服務(wù)發(fā)現(xiàn),斷路器鳖藕,智...
    卡卡羅2017閱讀 134,657評論 18 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,815評論 6 342
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法魔慷,類相關(guān)的語法,內(nèi)部類的語法著恩,繼承相關(guān)的語法院尔,異常的語法蜻展,線程的語...
    子非魚_t_閱讀 31,632評論 18 399
  • 如果說當(dāng)下的一切都是提升自己的修行,那么我認(rèn)為修行的目的是獲得自由邀摆。 人偏向于呆在自己的舒適圈里纵顾,因?yàn)樵谀抢锸窍鄬?..
    洋蔥書單閱讀 466評論 0 5