第一个月总结

Android 第一个月总结

ListView和适配器 的应用

public class Main2Activity extends AppCompatActivity {
    private TextView popup;
    private ListView lvList;
    private  List<JavaBean.DataBean> data;
    private MYadapter mYadapter;
    private  AlertDialog alertDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        popup = findViewById(R.id.popup);
        lvList = findViewById(R.id.lv_list);

        //定义自定义的popupweidows
        popup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                PopupWindow popupWindow = new PopupWindow(Main2Activity.this);
                View inflate = LayoutInflater.from(Main2Activity.this).inflate(R.layout.popup_item, null);
                popupWindow.setContentView(inflate);
                popupWindow.setWidth(350);
                popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);


                popupWindow.setOutsideTouchable(true);
                popupWindow.showAsDropDown(popup,-300,0);
            }
        });

        lvList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @SuppressLint("InflateParams")
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int i, long l) {
                final AlertDialog.Builder builder = new AlertDialog.Builder(Main2Activity.this);
                View inflate = LayoutInflater.from(Main2Activity.this).inflate(R.layout.tanchu_item, null);
                View del = inflate.findViewById(R.id.del);
                del.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        data.remove(i);
                        mYadapter.notifyDataSetChanged();
                        alertDialog.dismiss();
                    }
                });

                builder.setView(inflate);
               alertDialog = builder.create();
                alertDialog.dismiss();
                alertDialog.show();

                return true;
            }
        });
        new MYtask().execute("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1");
    }
    class MYtask extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... strings) {
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.connect();
                if (connection.getResponseCode()==200){
                    InputStream inputStream = connection.getInputStream();
                    StringBuilder stringBuilder = new StringBuilder();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

                    String line = null;
                    while((line =bufferedReader.readLine())!=null){stringBuilder.append(line);}
                    String s = stringBuilder.toString();
                    return s;

                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s!=null){
                Gson gson = new Gson();
                JavaBean javaBean = gson.fromJson(s, JavaBean.class);
                data = javaBean.getData();

                mYadapter = new MYadapter();
                lvList.setAdapter(mYadapter);
            }
        }
    }
    class MYadapter extends BaseAdapter{

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

        @Override
        public Object getItem(int i) {
            return data.get(i);
        }

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

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            VH vh = null;
            if (view==null){
                vh = new VH();
                view  = LayoutInflater.from(Main2Activity.this).inflate(R.layout.item_myadapter,null);
                vh.titl= view.findViewById(R.id.titl);
                vh.ok = view.findViewById(R.id.ok);
                view.setTag(vh);

            }else{
                vh = (VH) view.getTag();
            }
                vh.ok.setText(data.get(i).getNum()+"");
                vh.titl.setText(data.get(i).getTitle());
            return view;
        }
        class VH{
            private TextView titl;
            private TextView ok;
        }
    }

##布局内容

<?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=".Main2Activity">
    <LinearLayout
        android:background="#03A9F4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher_round"
            />
        <TextView
            android:layout_weight="1"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="30sp"
            android:textColor="#fff"
            android:textStyle="bold"
            android:text="消息"/>
        <TextView
            android:id="@+id/popup"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="+"
            android:layout_gravity="center"
            android:textSize="50dp"
            android:paddingRight="10dp"
            android:textColor="#fff"
            android:gravity="center"
            />

    </LinearLayout>
    <ListView
        android:id="@+id/lv_list"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>
    <RadioGroup
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioButton
            android:padding="10dp"
            android:layout_weight="1"
            android:text="消息"
            android:button="@null"
            android:textSize="20sp"
            android:gravity="center"
            android:textColor="@drawable/test_color"
            android:checked="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <RadioButton
            android:padding="10dp"
            android:layout_weight="1"
            android:text="消息"
            android:button="@null"
            android:textSize="20sp"
            android:gravity="center"
            android:textColor="@drawable/test_color"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <RadioButton
            android:padding="10dp"
            android:layout_weight="1"
            android:text="消息"
            android:button="@null"
            android:textSize="20sp"
            android:gravity="center"
            android:textColor="@drawable/test_color"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </RadioGroup>

</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值