登录
public class MainActivity extends AppCompatActivity {
EditText editText;
EditText editText2;
Button button;
RequestQueue queue;
JSONObject jsonArray2 = new JSONObject();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.editText);
editText2=findViewById(R.id.editText2);
button=findViewById(R.id.button);
queue= Volley.newRequestQueue(this);
}
public void Click2(View view){
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
public void Click(View view){
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("123456");
builder.setMessage("123456");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
String base_url="";
String name=editText.getText().toString();
String pwd=editText2.getText().toString();
String url=base_url+"?"+"user="+name+"&pwd="+pwd;
JsonObjectRequest request = new JsonObjectRequest(url, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
jsonArray2 = response;
//myAdapter.notifyDataSetChanged();
Log.d("test",response.toString());
/* try {
/*
if(jsonArray2.getString("result").equals("OK")){
Intent intent=new Intent(loginActivity.this,MainActivity.class);
startActivity(intent);
}*/
// Intent intent=new Intent(loginActivity.this,MainActivity.class);
// startActivity(intent);
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("123456");
builder.setMessage("123456");
//builder.setPositiveButton("ok",nil)
builder.show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//数据添加到队列
queue.add(request);
}
}
第二
public class Main2Activity extends AppCompatActivity {
private static final String TAG = "Main2Activity";
ListView listView;
JSONArray jsonArray = new JSONArray();
JSONObject jsonArray2 = new JSONObject();
MyAdapter myAdapter;
RequestQueue queue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
listView = findViewById(R.id.listView);
//自定制适配器
myAdapter = new MyAdapter();
listView.setAdapter(myAdapter);
//新建队列
queue = Volley.newRequestQueue(this);
downloadJsonData();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try{
JSONObject jsonObject = jsonArray.getJSONObject(position);
//界面传参
Intent intent = new Intent(Main2Activity.this,Main3Activity.class);
intent.putExtra("student",jsonObject.toString());
startActivity(intent);
}catch (JSONException e){
e.printStackTrace();
}
}
});
}
public void downloadJsonData(){
JsonArrayRequest request = new JsonArrayRequest("http://10.0.1.2/piclist.php", new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
jsonArray = response;
myAdapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//数据添加到队列
queue.add(request);
}
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return jsonArray.length();
}
@Override
public Object getItem(int position) {
try{
return jsonArray.getJSONObject(position).toString();
}catch (JSONException e){
e.printStackTrace();
}
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null){
convertView = LayoutInflater.from(Main2Activity.this).inflate(R.layout.stulayout,null);
viewHolder = new ViewHolder();
viewHolder.textView_name = convertView.findViewById(R.id.textView_name);
viewHolder.textView_stuno = convertView.findViewById(R.id.textView_stuno);
viewHolder.textView_gender = convertView.findViewById(R.id.textView_gender);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag();
}
try{
JSONObject jsonObject = jsonArray.getJSONObject(position);
viewHolder.textView_name.setText(jsonObject.getString("stuName"));
viewHolder.textView_stuno.setText(jsonObject.getString("stuNo"));
viewHolder.textView_gender.setText(jsonObject.getString("stuGender"));
}catch (JSONException e){
e.printStackTrace();
}
return convertView;
}
class ViewHolder{
TextView textView_name,textView_stuno,textView_gender;
}
}
}
第三
public class Main3Activity extends AppCompatActivity {
TextView textView_name,textView_stuno,textView_gender,textView_class;
ImageView imageView;
RequestQueue queue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
textView_name = findViewById(R.id.textView_name);
textView_stuno = findViewById(R.id.textView_stuno);
textView_gender = findViewById(R.id.textView_gender);
textView_class = findViewById(R.id.textView_class);
imageView = findViewById(R.id.imageView);
try{
JSONObject jsonObject = new JSONObject(getIntent().getStringExtra("student"));
textView_name.setText(jsonObject.getString("stuName"));
textView_stuno.setText(jsonObject.getString("stuno"));
textView_gender.setText(jsonObject.getString("stuGender"));
textView_class.setText(jsonObject.getString("stuClass"));
queue = Volley.newRequestQueue(this);
ImageRequest request = new ImageRequest("http://10.0.1.2/" + jsonObject.getString("imagePath"), new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
imageView.setImageBitmap(response);
}
}, 0, 0, ImageView.ScaleType.CENTER_INSIDE, Bitmap.Config.RGB_565, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(request);
}catch (JSONException e){
e.printStackTrace();
}
}
}
xml 111111111
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bluegrey.mynewadapter.MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="29dp"
android:layout_marginEnd="33dp"
android:layout_marginStart="33dp"
android:layout_marginTop="31dp"
android:ems="10"
android:hint="姓名"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/editText2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="73dp"
android:layout_marginEnd="34dp"
android:layout_marginStart="34dp"
android:ems="10"
android:hint="密码"
android:inputType="textWebPassword"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginStart="90dp"
android:onClick="Click"
android:text="LIGON"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintStart_toStartOf="@+id/editText2"
app:layout_constraintTop_toBottomOf="@+id/editText2" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="168dp"
android:onClick="Click2"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/button" />
</android.support.constraint.ConstraintLayout>
xml 2
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bluegrey.mynewadapter.Main2Activity">
<ListView
android:id="@+id/listView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
xml 3
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bluegrey.mynewadapter.Main3Activity">
<TextView
android:id="@+id/textView_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="51dp"
android:text="TextView"
android:textSize="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView_stuno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:text="TextView"
android:textSize="24dp"
app:layout_constraintEnd_toEndOf="@+id/textView_class"
app:layout_constraintStart_toStartOf="@+id/textView_class"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="155dp"
android:text="TextView"
android:textSize="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView_class"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginEnd="51dp"
android:layout_marginRight="51dp"
android:text="TextView"
android:textSize="24dp"
app:layout_constraintBottom_toTopOf="@+id/imageView"
app:layout_constraintEnd_toEndOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="231dp"
android:layout_marginEnd="37dp"
android:layout_marginLeft="46dp"
android:layout_marginRight="37dp"
android:layout_marginStart="46dp"
android:layout_marginTop="12dp"
app:layout_constraintEnd_toEndOf="@+id/textView_class"
app:layout_constraintStart_toStartOf="@+id/textView_gender"
app:layout_constraintTop_toBottomOf="@+id/textView_gender"
app:srcCompat="@mipmap/ic_launcher" />
</android.support.constraint.ConstraintLayout>
stu 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">
<TextView
android:id="@+id/textView_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#00ff00"
android:text="TextView" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView_stuno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
android:orientation="vertical">
<TextView
android:id="@+id/textView_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="@color/colorPrimary"
android:textSize="20dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView_stuno"
android:layout_width="166dp"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="@color/colorAccent" />
<TextView
android:id="@+id/textView_gender"
android:layout_width="185dp"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="@color/colorAccent" />
</LinearLayout>