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" ;
//学期
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" });
//设置下拉样式
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);
}
/**
* 设置监听器
*/
private void initListener() {
mBtnLogin.setOnClickListener(this);
}
/*
单击事件监听
*/
@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, "学期或班级不能为空", Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent(this, com.example.gridview.ClassActivity.class);
//向ClassActivity传参数
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);
//把请求的内容字符串转换为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, "结果:" + 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 {
//把请求的内容字符串转换 为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;
}
}