Android 项目
package com.example.camera.pojo;
import java.util.Date;
public class Policy {
private Long id;
private String name;
private String type;
private String category;
private String range;
private String document;
private String form;
private String organ;
private Date viadata;
private Date pubdata;
private Date perdata;
private String field;
private String theme;
private String keyword;
private String superior;
private String precursor;
private String succeed;
private String state;
private String text;
private String pdf;
private String redundancy;
private String rank;
private String policykey;
private String newrank;
private String year;
private String newkey;
private String secondtheme;
private Integer allsum;
// Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getRange() {
return range;
}
public void setRange(String range) {
this.range = range;
}
public String getDocument() {
return document;
}
public void setDocument(String document) {
this.document = document;
}
public String getForm() {
return form;
}
public void setForm(String form) {
this.form = form;
}
public String getOrgan() {
return organ;
}
public void setOrgan(String organ) {
this.organ = organ;
}
public Date getViadata() {
return viadata;
}
public void setViadata(Date viadata) {
this.viadata = viadata;
}
public Date getPubdata() {
return pubdata;
}
public void setPubdata(Date pubdata) {
this.pubdata = pubdata;
}
public Date getPerdata() {
return perdata;
}
public void setPerdata(Date perdata) {
this.perdata = perdata;
}
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
public String getTheme() {
return theme;
}
public void setTheme(String theme) {
this.theme = theme;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public String getSuperior() {
return superior;
}
public void setSuperior(String superior) {
this.superior = superior;
}
public String getPrecursor() {
return precursor;
}
public void setPrecursor(String precursor) {
this.precursor = precursor;
}
public String getSucceed() {
return succeed;
}
public void setSucceed(String succeed) {
this.succeed = succeed;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getPdf() {
return pdf;
}
public void setPdf(String pdf) {
this.pdf = pdf;
}
public String getRedundancy() {
return redundancy;
}
public void setRedundancy(String redundancy) {
this.redundancy = redundancy;
}
public String getRank() {
return rank;
}
public void setRank(String rank) {
this.rank = rank;
}
public String getPolicykey() {
return policykey;
}
public void setPolicykey(String policykey) {
this.policykey = policykey;
}
public String getNewrank() {
return newrank;
}
public void setNewrank(String newrank) {
this.newrank = newrank;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getNewkey() {
return newkey;
}
public void setNewkey(String newkey) {
this.newkey = newkey;
}
public String getSecondtheme() {
return secondtheme;
}
public void setSecondtheme(String secondtheme) {
this.secondtheme = secondtheme;
}
public Integer getAllsum() {
return allsum;
}
public void setAllsum(Integer allsum) {
this.allsum = allsum;
}
}
package com.example.camera.pojo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.example.camera.R;
import com.example.camera.pojo.Policy;
import java.util.List;
public class PolicyAdapter extends ArrayAdapter<Policy> {
private Context context;
private List<Policy> policies;
public PolicyAdapter(Context context, int resource, List<Policy> objects) {
super(context, resource, objects);
this.context = context;
this.policies = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.policy_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.nameTextView = convertView.findViewById(R.id.textView_policyName);
viewHolder.timeTextView = convertView.findViewById(R.id.textView_policyTime);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Policy policy = policies.get(position);
viewHolder.nameTextView.setText(policy.getName());
viewHolder.timeTextView.setText(policy.getPubdata().toString()); // 格式化日期
return convertView;
}
static class ViewHolder {
TextView nameTextView;
TextView timeTextView;
}
}
package com.example.camera;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BlankFragment1 extends Fragment {
private View root;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (root == null) {
root = inflater.inflate(R.layout.fragment_blank1, container, false);
}
return root;
}
}
package com.example.camera;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
public class HtmlDisplayActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_html);
// 获取Intent传递过来的HTML内容
String Text = getIntent().getStringExtra("Text");
Log.d("HtmlDisplayActivity", "Received HTML Content: " + Text);
// 初始化WebView
WebView webView = findViewById(R.id.webView);
// 设置WebView客户端,允许打开新的窗口(比如链接)
webView.setWebViewClient(new WebViewClient());
// 配置WebView设置
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true); // 如果HTML中有JavaScript代码,需要启用
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
// 加载HTML内容
webView.loadDataWithBaseURL(null, Text, "text/html", "utf-8", null);
}
}
package com.example.camera;
import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.camera.placeholder.PlaceholderContent;
/**
* A fragment representing a list of Items.
*/
public class ItemFragment extends Fragment {
// TODO: Customize parameters
private int mColumnCount = 1;
// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
// TODO: Customize parameter initialization
@SuppressWarnings("unused")
public static ItemFragment newInstance(int columnCount) {
ItemFragment fragment = new ItemFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public ItemFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list, container, false);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
recyclerView.setAdapter(new MyItemRecyclerViewAdapter(PlaceholderContent.ITEMS));
}
return view;
}
}
package com.example.camera;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import com.example.camera.pojo.Policy;
import com.example.camera.pojo.PolicyAdapter;
import com.example.camera.tools.Okhttp3helper;
import com.google.android.material.button.MaterialButton;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Okhttp3helper OkHttpHelper =new Okhttp3helper();
private MaterialButton buttonSearch;
private EditText editText;
private ListView listView;
private PolicyAdapter adapter;
private List<Policy> policies;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
buttonSearch=findViewById(R.id.buttonSearch);
editText=findViewById(R.id.editText);
listView = findViewById(R.id.list);
policies = new ArrayList<>(); // 假设你已经有了政策数据列表
// 初始化policies列表,可以从网络、数据库等地方获取数据
adapter = new PolicyAdapter(this, R.layout.policy_item, policies);
listView.setAdapter(adapter);
// Button button=findViewById(R.id.btn1);
// Button button_2=findViewById(R.id.btn2);
// button.setOnClickListener(this);
// button_2.setOnClickListener(this);
fetchPolicies();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// 假设你的数据模型类为Policy,且其中有一个字段存储HTML内容
Policy policy = (Policy) parent.getItemAtPosition(position);
String Text = policy.getText(); // 获取HTML内容
Log.d("MainActivity", "Sending HTML Content: " + Text);
// 使用Intent传递HTML内容到新的Activity
Intent intent = new Intent(MainActivity.this, HtmlDisplayActivity.class);
intent.putExtra("Text", Text);
MainActivity.this.startActivity(intent);
}
});
buttonSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String query = editText.getText().toString().trim();
if (!query.isEmpty()) {
queryPolicies(query);
} else {
showToast("请输入查询内容");
}
}
});
}
private void queryPolicies(String query) {
OkHttpHelper.queryPolicy(query, new Okhttp3helper.QueryCallback() {
@Override
public void onPolicyReceived(List<Policy> policies) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// 更新数据并通知Adapter数据集变更
MainActivity.this.policies.clear();
MainActivity.this.policies.addAll(policies);
if (adapter != null) {
adapter.notifyDataSetChanged();
}
}
});
}
@Override
public void onPolicyFailure(String errorMessage) {
runOnUiThread(new Runnable() {
@Override
public void run() {
showToast("网络请求失败");
}
});
}
});
}
private void fetchPolicies() {
OkHttpHelper.getPolicy(new Okhttp3helper.DataListCallback() {
public void onPlanReceived(List<Policy> policies) {
// 确保在主线程更新UI
runOnUiThread(new Runnable() {
@Override
public void run() {
// 更新数据并通知Adapter数据集变更
MainActivity.this.policies.clear();
MainActivity.this.policies.addAll(policies);
if (adapter != null) {
adapter.notifyDataSetChanged();
}
}
});
}
@Override
public void onPlanFailure(String errorMessage) {
runOnUiThread(new Runnable() {
@Override
public void run() {
showToast("网络请求失败");
}
});
}
});
}
private void showToast(String message) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT; // 持续时间,可选Toast.LENGTH_LONG
Toast toast = Toast.makeText(context, message, duration);
toast.show();
}
}
package com.example.camera;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.camera.placeholder.PlaceholderContent.PlaceholderItem;
import com.example.camera.databinding.FragmentItemBinding;
import java.util.List;
/**
* {@link RecyclerView.Adapter} that can display a {@link PlaceholderItem}.
* TODO: Replace the implementation with code for your data type.
*/
public class MyItemRecyclerViewAdapter extends RecyclerView.Adapter<MyItemRecyclerViewAdapter.ViewHolder> {
private final List<PlaceholderItem> mValues;
public MyItemRecyclerViewAdapter(List<PlaceholderItem> items) {
mValues = items;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(FragmentItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mIdView.setText(mValues.get(position).id);
holder.mContentView.setText(mValues.get(position).content);
}
@Override
public int getItemCount() {
return mValues.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final TextView mIdView;
public final TextView mContentView;
public PlaceholderItem mItem;
public ViewHolder(FragmentItemBinding binding) {
super(binding.getRoot());
mIdView = binding.itemNumber;
mContentView = binding.content;
}
@Override
public String toString() {
return super.toString() + " '" + mContentView.getText() + "'";
}
}
}
标签:return,String,6.12,void,private,import,public From: https://www.cnblogs.com/wcy1111/p/18253705