第三周周考练习

权限:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

依赖:

compile 'com.google.code.gson:gson:2.8.2'
compile 'com.squareup.picasso:picasso:2.5.2'

MainActivity布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.mrzhao.examdemo.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/login_name_et"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入用户名" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密    码:"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/login_password_et"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入密码" />
    </LinearLayout>

    <Button
        android:onClick="onClick"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="登    陆"
        tools:ignore="OnClick" />


</LinearLayout>

MainActivity文件:

public class MainActivity extends AppCompatActivity {

    private EditText userNameEt;
    private EditText userpasswordEt;

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

        userNameEt = (EditText) findViewById(R.id.login_name_et);
        userpasswordEt = (EditText) findViewById(R.id.login_password_et);

    }

    public void onClick(View view) {
        String userName = userNameEt.getText().toString();
        String userPassword = userpasswordEt.getText().toString();
        // 判断用户名是否为空
        if (TextUtils.isEmpty(userName)) {
            Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
            return;
        }
        //判断密码是否为空
        if (TextUtils.isEmpty(userPassword)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }


        //启动跳转到第二个页面
        Intent intent = new Intent(this, HomeActivity.class);
        intent.putExtra("name",userName);
        intent.putExtra("password",userPassword);
        startActivity(intent);
    }
}

HomeActivity布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.mrzhao.examdemo.HomeActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</LinearLayout>

HomeActivity文件:

public class HomeActivity extends AppCompatActivity {

    private ListView listView;
    private String path = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1";

    private List<FoodEntity.DataBean> list = new ArrayList<>();
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        //获取传递过来的数据
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        String password = intent.getStringExtra("password");
        Toast.makeText(this, name + ":" + password, Toast.LENGTH_SHORT).show();


        //当前页面的数据展示
        listView = (ListView) findViewById(R.id.listView);

        myAdapter = new MyAdapter(list, this);
        listView.setAdapter(myAdapter);
        //添加条目点击监听器
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //获取对应位置的信息
                Toast.makeText(HomeActivity.this, list.get(position).getTitle(), Toast.LENGTH_SHORT).show();
            }
        });
        // 执行 异步任务
        new MyTask().execute();


    }

    class MyTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... strings) {
            return getDataFromNet(path);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (!TextUtils.isEmpty(s)){
                Gson gson = new Gson();
                FoodEntity foodEntity = gson.fromJson(s, FoodEntity.class);
                List<FoodEntity.DataBean> data = foodEntity.getData();
                list.addAll(data);
                myAdapter.notifyDataSetChanged();
            }
        }
    }

    private String getDataFromNet(String path) {
        InputStream inputStream = null;
        ByteArrayOutputStream outputStream = null;
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setReadTimeout(3 * 1000);
            conn.setConnectTimeout(3 * 1000);
            conn.connect();
            if (conn.getResponseCode() == 200) {
                inputStream = conn.getInputStream();
                outputStream = new ByteArrayOutputStream();

                int len = 0;
                byte[] bytes = new byte[1024];
                while ((len = inputStream.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, len);
                }
                return outputStream.toString();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream !=  null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}

适配器:

public class MyAdapter extends BaseAdapter {
    private List<FoodEntity.DataBean> list;
    private Context context;
    private LayoutInflater inflater;

    public MyAdapter(List<FoodEntity.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
        //布局加载器   作用是 可以将 布局文件 转换成为 视图View 对象
        inflater = LayoutInflater.from(context);
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder  holder = null;
        if (convertView ==  null){
            convertView =  inflater.inflate(R.layout.item_layout,parent,false);
            holder = new ViewHolder();
            holder.foodPicIv =  convertView.findViewById(R.id.food_pic_iv);
            holder.foodTitleTv =  convertView.findViewById(R.id.food_title_tv);
            holder.foodStrTv = convertView.findViewById(R.id.food_str_tv);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        FoodEntity.DataBean dataBean = list.get(position);

        holder.foodTitleTv.setText(dataBean.getTitle());
        holder.foodStrTv.setText(dataBean.getFood_str());

        Picasso.with(context).load(dataBean.getPic()).into(holder.foodPicIv);


        return convertView;
    }

    static class ViewHolder {
        ImageView foodPicIv;
        TextView foodTitleTv;
        TextView foodStrTv;
    }
}

条目布局文件:

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

    <ImageView
        android:id="@+id/food_pic_iv"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="10dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/food_title_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/food_str_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:textSize="28sp" />

    </LinearLayout>
</LinearLayout>


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值