zoukankan      html  css  js  c++  java
  • 2021/1/28

    recyclerview

    package com.example.myapplication;
    
    public class music {
        private String url;
        private String mname;
        private String geshou;
    
        public music(String url, String mname, String geshou) {
            this.url = url;
            this.mname = mname;
            this.geshou = geshou;
        }
    
        public String getUrl() {
            return url;
        }
    
        public void setUrl(String url) {
            this.url = url;
        }
    
        public String getMname() {
            return mname;
        }
    
        public void setMname(String mname) {
            this.mname = mname;
        }
    
        public String getGeshou() {
            return geshou;
        }
    
        public void setGeshou(String geshou) {
            this.geshou = geshou;
        }
    }
    
    
    package com.example.myapplication;
    
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Looper;
    import android.os.Message;
    import android.widget.Toast;
    
    import androidx.appcompat.app.AlertDialog;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.recyclerreview.widget.LinearLayoutManager;
    import androidx.recyclerview.widget.RecyclerView;
    
    
    import net.sf.json.JSONArray;
    import net.sf.json.JSONException;
    import net.sf.json.JSONObject;
    
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import okhttp3.Call;
    import okhttp3.Callback;
    import okhttp3.MediaType;
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.RequestBody;
    import okhttp3.Response;
    
    public class MainActivity extends AppCompatActivity {
    
    
        private List<music> list=new ArrayList<>();
        RecyclerView recyclerView;
        Handler handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(msg.what==111){
                    list=(List<music>)msg.obj;
                    LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
                    recyclerView.setLayoutManager(layoutManager);
                    FruitAdapter adapter = new FruitAdapter(list);
                    adapter.setOnItemClickListener(new FruitAdapter.OnItemClickListener() {
                        @Override
                        public void onClick(int position) {
    
                            if(position==1) {
                                Intent intent = new Intent();
                                intent.setClass(MainActivity.this, Main2Activity.class);
                                intent.putExtra("url",list.get(position).getUrl());
                                MainActivity.this.startActivity(intent);
    
                            }
                            Toast.makeText(getBaseContext(), "点击的是" + position + "的itemClick", Toast.LENGTH_SHORT).show();
                        }
                    });
                    recyclerView.setAdapter(adapter);
                }
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //initFruits();
            recyclerView = findViewById(R.id.recycler_view);
            /*
            LinearLayoutManager layoutManager = new LinearLayoutManager(this);
            recyclerView.setLayoutManager(layoutManager);
            FruitAdapter adapter = new FruitAdapter(fruitList);
            adapter.setOnItemClickListener(new FruitAdapter.OnItemClickListener() {
                @Override
                public void onClick(int position) {
    
                    if(position==1) {
                        Intent intent = new Intent();
                        intent.setClass(MainActivity.this, Main2Activity.class); //设置跳转的Activity
                        MainActivity.this.startActivity(intent);
    
                    }
                    Toast.makeText(getBaseContext(), "点击的是" + position + "的itemClick", Toast.LENGTH_SHORT).show();
                }
            });
            recyclerView.setAdapter(adapter);
    
             */
            nettest();
        }
        public void nettest(){
            String path="http://39.103.179.153:8080/first/Servlet?method=bianli";
            OkHttpClient client = new OkHttpClient();
            JSONObject jsob = new JSONObject();
            try {
                jsob.put("id", "123");
    
            } catch (JSONException e) {
                e.printStackTrace();
            }
            MediaType type1 = MediaType.parse("appliaction/json;charset=utf-8");
            RequestBody body1 = RequestBody.create(type1, jsob + "");
            Request request = new Request.Builder()
                    .url(path)
                    .post(body1)
                    .build();
            client.newCall(request).enqueue(new Callback() {
                //失败
                @Override
                public void onFailure(Call call, IOException e) {
                    Looper.prepare();
    
                    AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
                    alertDialog.setMessage("网络或服务器原因注册失败");
                    alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "我知道了", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    });
                    alertDialog.show();
                    Looper.loop();
                }
                //成功
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                   // Looper.prepare();
                    String result = response.body().string();
                    JSONArray jsar = JSONArray.fromObject(result);
                    List<music> list1 = new ArrayList<>();
                    music bean = null;
                    for (int i = 0; i < jsar.size(); i++) {
                        JSONObject jsob = jsar.getJSONObject(i);
                        String url = jsob.getString("url");
                        String manme=jsob.getString("mname");
                        String geshou=jsob.getString("geshou");
                        bean=new music(url,manme,geshou);
                        list1.add(bean);
                    }
                    //Toast.makeText(getBaseContext(), "点击的是" + list1.get(0).getGeshou() + "的itemClick"+list1.get(0).getMname(), Toast.LENGTH_SHORT).show();
                    //Looper.loop();
                    Message message=handler.obtainMessage();
                    message.what=111;
                    message.obj=list1;
                    handler.sendMessage(message);
                }
            });
    
        }
        /*
        private void initFruits() {
            for (int i = 0; i < 2; i++) {
                Fruit apple = new Fruit("Apple", R.drawable.mm);
                fruitList.add(apple);
                Fruit banana = new Fruit("Banana", R.drawable.mm);
                fruitList.add(banana);
                Fruit orange = new Fruit("Orange", R.drawable.mm);
                fruitList.add(orange);
                Fruit watermelon = new Fruit("Watermelon", R.drawable.mm);
                fruitList.add(watermelon);
                Fruit pear = new Fruit("Pear", R.drawable.mm);
                fruitList.add(pear);
                Fruit grape = new Fruit("Grape", R.drawable.mm);
                fruitList.add(grape);
                Fruit pineapple = new Fruit("Pineapple", R.drawable.mm);
                fruitList.add(pineapple);
    
    
            }
        }
    
         */
    }
    package com.example.myapplication;
    
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import androidx.recyclerview.widget.RecyclerView;
    
    import java.util.List;
    
    public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
    
        private List<music> mFruitList;
    
        static class ViewHolder extends RecyclerView.ViewHolder {
            ImageView fruitImage;
            TextView fruitName;
            TextView singertext;
    
            public ViewHolder(View view) {
                super(view);
                fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
                fruitName = (TextView) view.findViewById(R.id.music);
                singertext=view.findViewById(R.id.singer);
            }
    
        }
    
        public FruitAdapter(List<music> fruitList) {
            mFruitList = fruitList;
        }
    
        @Override
    
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item, parent, false);
            ViewHolder holder = new ViewHolder(view);
            return holder;
        }
    
        @Override
        public void onBindViewHolder(ViewHolder holder, final int position) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null)
                        onItemClickListener.onClick(position);
                }
            });
    
            music fruit = mFruitList.get(position);
            holder.fruitImage.setImageResource(R.drawable.mm);
            holder.fruitName.setText("歌名:"+fruit.getMname());
            holder.singertext.setText("歌手:"+fruit.getGeshou());
        }
    
        @Override
        public int getItemCount() {
            return mFruitList.size();
        }
        public interface OnItemClickListener {
            void onClick(int position);
        }
    
        OnItemClickListener onItemClickListener;
    
    
        public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
            this.onItemClickListener = onItemClickListener;
        }
    
    }
  • 相关阅读:
    构建之法阅读笔记01
    学习进度13
    学习进度12
    个人冲刺十
    个人冲刺九
    个人冲刺八
    学习进度11
    个人冲刺七
    个人冲刺六
    [HDU 1232 ]畅通工程
  • 原文地址:https://www.cnblogs.com/qiangini/p/14864785.html
Copyright © 2011-2022 走看看