今天完成的是带历史记录的搜索框
利用的是一个集成好的框架,一个带历史记录的框架。首先在主界面设置一个搜索的按钮,点击后来到全局搜索的布局页面,然后输入搜索的关键字, 点击搜索就会出现对应的文章,点击任意文章后就会看到相应的博文。
SearchActivity
package com.example.newbsh.UI.Find;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import scut.carson_ho.searchview.ICallBack;
import scut.carson_ho.searchview.SearchView;
import scut.carson_ho.searchview.bCallBack;
import com.example.newbsh.R;
public class SearchActivity extends AppCompatActivity {
private SearchView searchView;
public static String text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
// 3. 绑定组件
searchView = (SearchView) findViewById(R.id.search_view);
// 4. 设置点击搜索按键后的操作(通过回调接口)
// 参数 = 搜索框输入的内容
searchView.setOnClickSearch(new ICallBack() {
@Override
public void SearchAciton(String string) {
text=string;
Intent intent=new Intent(SearchActivity.this,FindActivity.class);
startActivity(intent);
//finish();
}
});
// 5. 设置点击返回按键后的操作(通过回调接口)
searchView.setOnClickBack(new bCallBack() {
@Override
public void BackAciton() {
finish();
}
});
}
public static String getText() {
return text;
}
}
FindActivity
package com.example.newbsh.UI.Find;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;
import com.example.newbsh.HttpUtil.HttpUtil;
import com.example.newbsh.R;
import org.json.JSONArray;
import org.json.JSONException;
public class FindActivity extends AppCompatActivity {
private JSONArray jsonArray;
private ListView listView;
FindAdapter findAdapter;
private Handler handler;
private String title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find);
title=SearchActivity.getText();
jsonArray=new JSONArray();
getThreadData();
handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(@NonNull Message msg) { //线程回调更新数据
if (msg.what == 200) {
jsonArray = ((JSONArray) msg.obj);
Log.e("输出jsonarray", "onCreate: "+jsonArray );
findAdapter=new FindAdapter(jsonArray,FindActivity.this);
listView=(ListView) findViewById(R.id.listviewlike);
listView.setAdapter(findAdapter);
}
return true;
}
});
}
private void getThreadData() { //线程封装网络请求
new Thread(new Runnable() {
@Override
public void run() {
String reqdata = "";
String address="http://39.97.181.86/BaiXiaoSheng/findlike?title="+title;
try {
final JSONArray json = new JSONArray(String.valueOf(HttpUtil.sendPost(address, reqdata)));
Log.d("tag", json.toString());
Message msg = new Message();
msg.what = 200;
msg.obj = json;
handler.sendMessage(msg);
} catch (JSONException e) {
e.printStackTrace();
}
}
}).start();
}
}
FindAdapter
package com.example.newbsh.UI.Find;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.newbsh.HttpUtil.HttpUtil;
import com.example.newbsh.R;
import com.example.newbsh.UI.home.hometype.news.shownews.ShowNewsActivity;
import com.example.newbsh.UI.home.hometype.news.shownews.comment.BlogComment;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;
import java.lang.ref.WeakReference;
import static com.android.volley.VolleyLog.TAG;
public class FindAdapter extends BaseAdapter {
private JSONArray jsonArray;
private Context context;
private LayoutInflater layoutInflater;
public FindAdapter(JSONArray jsonArray, Context context) {
this.jsonArray = jsonArray;
this.context = context;
this.layoutInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return jsonArray.length();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
holder=new ViewHolder();
convertView=layoutInflater.inflate(R.layout.cell_news2,parent,false);
holder.textViewTitle=convertView.findViewById(R.id.Title);
holder.textViewJianJie=convertView.findViewById(R.id.jianjie);
holder.textViewClicknumber=convertView.findViewById(R.id.click);
holder.textViewDatetime=convertView.findViewById(R.id.date);
holder.textViewnewscomment=convertView.findViewById(R.id.comment);
holder.textViewnewssupport=convertView.findViewById(R.id.support);
holder.photo=convertView.findViewById(R.id.userphoto);
JSONObject bean= null;
try {
bean = jsonArray.getJSONObject(position);
holder.textViewTitle.setText(bean.getString("title"));
holder.textViewJianJie.setText(bean.getString("doc"));
holder.textViewClicknumber.setText(bean.getString("click"));
holder.textViewDatetime.setText(bean.getString("date"));
String []urls=bean.getString("url").split(" ");
holder.textViewnewscomment.setText(bean.getString("comment"));
holder.textViewnewssupport.setText(bean.getString("support"));
RequestOptions options = new RequestOptions()
.error(R.drawable.error)
.placeholder(R.drawable.loading);
if(urls.length!=0) {
for(int i=0;i<urls.length;i++)
{
if(!urls[i].equals(" "))
{
Log.i("url",urls[i]);
Glide.with(convertView)
.load(urls[i])
.apply(options)
.into(holder.photo);
break;
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle=new Bundle();
try {
bundle.putString("title", jsonArray.getJSONObject(position).getString("title"));
bundle.putString("doc",jsonArray.getJSONObject(position).getString("doc"));
bundle.putString("click",jsonArray.getJSONObject(position).getString("click"));
bundle.putString("date",jsonArray.getJSONObject(position).getString("date"));
bundle.putString("url",jsonArray.getJSONObject(position).getString("url"));
bundle.putString("authorname",jsonArray.getJSONObject(position).getString("authorname"));
bundle.putString("authorurl",jsonArray.getJSONObject(position).getString("authorurl"));
} catch (JSONException e) {
e.printStackTrace();
}
Intent intent=new Intent(context,ShowNewsActivity.class);
intent.putExtras(bundle);
context.startActivity(intent);
}
});
return convertView;
}
private static class ViewHolder
{
public TextView textViewTitle,textViewJianJie,textViewDatetime,textViewClicknumber,textViewnewssupport,textViewnewscomment;
public ImageView photo;
}
}