package com.example.practice2;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.practice2.adapter.MyRecycleViewAdapter;
import com.example.practice2.utils.Download;
import com.example.practice2.utils.SongBean;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private List<SongBean.ResultBean> mapList=new ArrayList<>();
private EditText mName;
private MyRecycleViewAdapter adapter;
private RecyclerView mRecycleView;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==110){
String s = (String) msg.obj;
Gson gson = new Gson();
SongBean songBean = gson.fromJson(s, SongBean.class);
SongBean.ResultBean result = songBean.getResult();
for (int i = 0; i <result.getSongCount() ; i++) {
mapList.add(result);
result.getSongs().get(i).getAlias().get(i);
}
Log.i(TAG, "handleMessage: "+mapList.toString());
}
adapter=new MyRecycleViewAdapter(MainActivity.this,mapList);
mRecycleView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
adapter.notifyDataSetChanged();
mRecycleView.setAdapter(adapter);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mName = findViewById(R.id.name);
mRecycleView = findViewById(R.id.recycleView);
// mRecycleView.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
//
// }
// });
// adapter.setOnItemOnClick(new MyRecycleViewAdapter.onItemOnClick() {
// @Override
// public void onClick() {
// Intent intent = new Intent(MainActivity.this,Main2Activity.class);
// startActivity(intent);
// }
// });
}
public void click(View view) {
String name = mName.getText().toString().trim();
if(name!=null){
new Download(name,handler,0).start();
}else {
Toast.makeText(this, "搜索栏不得为空", Toast.LENGTH_SHORT).show();
}
}
}
package com.example.practice2.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.practice2.R;
import com.example.practice2.utils.SongBean;
import java.util.HashMap;
import java.util.List;
public class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewAdapter.MyHolder> {
private Context context;
private List<SongBean.ResultBean> list;
private LayoutInflater inflater;
private onItemOnClick onItemOnClick;
public interface onItemOnClick{
void onClick();
}
public MyRecycleViewAdapter.onItemOnClick getOnItemOnClick() {
return onItemOnClick;
}
public void setOnItemOnClick(MyRecycleViewAdapter.onItemOnClick onItemOnClick) {
this.onItemOnClick = onItemOnClick;
}
public MyRecycleViewAdapter(Context context, List<SongBean.ResultBean> list) {
this.context = context;
this.list = list;
inflater=LayoutInflater.from(context);
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new MyHolder(LayoutInflater.from(context).inflate(R.layout.recycleview_item,null));
}
@Override
public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
list.get(i).getSongs().get(i).getAlias().get(i);
myHolder.name.setText(list.get(i).getSongs().get(i).getName());
myHolder.name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(onItemOnClick!=null){
onItemOnClick.onClick();
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
class MyHolder extends RecyclerView.ViewHolder{
TextView name;
TextView songer;
public MyHolder(@NonNull View itemView) {
super(itemView);
name=itemView.findViewById(R.id.name);
songer=itemView.findViewById(R.id.songer);
}
}
}
<?xml version="1.0" encoding="utf-8"?><LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/name"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#2975E6"
android:hint="请输入您所要搜索的歌曲"
android:textColorHint="#fff"
android:layout_weight="8"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="#2975E6"
android:onClick="click"
android:textSize="16sp"
android:textColor="#fff"
android:text="搜索"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9">
</android.support.v7.widget.RecyclerView>