gpt4 book ai didi

java - 如何使用卡片向 RecyclerView 添加 searchBar

转载 作者:太空宇宙 更新时间:2023-11-04 12:38:39 25 4
gpt4 key购买 nike

我正在从 JSON 数组中获取数据,并将其显示在 RecyclerView 上。我想实现一个搜索栏,以搜索和显示 RecyclerView 中的元素。

MyRecyclerAdapter.java

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.CustomViewHolder> {
private List<FeedItem> feedItemList;
private Context mContext;

public MyRecyclerAdapter(Context context, List<FeedItem> feedItemList) {
this.feedItemList = feedItemList;
this.mContext = context;
}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, null);

CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}

@Override
public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
FeedItem feedItem = feedItemList.get(i);



//Setting text view title
customViewHolder.textView.setText(Html.fromHtml(feedItem.getTitle()));
customViewHolder.contactTV.setText(Html.fromHtml(feedItem.getContact()));
customViewHolder.emailTV.setText(Html.fromHtml(feedItem.getEmail()));
customViewHolder.eventname.setText(Html.fromHtml(feedItem.getEventname()));
customViewHolder.collegename.setText(Html.fromHtml(feedItem.getCollegename()));

//Handle click event on both title and image click
customViewHolder.textView.setOnClickListener(clickListener);

customViewHolder.textView.setTag(customViewHolder);

// Set the view to fade in
//setFadeAnimation(customViewHolder.itemView);

}

/*private void setFadeAnimation(View view) {
AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(800);
view.startAnimation(anim);
}*/

View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
CustomViewHolder holder = (CustomViewHolder) view.getTag();
int position = holder.getPosition();

FeedItem feedItem = feedItemList.get(position);
Toast.makeText(mContext, feedItem.getTitle(), Toast.LENGTH_SHORT).show();
}
};

@Override
public int getItemCount() {
return (null != feedItemList ? feedItemList.size() : 0);
}
public class CustomViewHolder extends RecyclerView.ViewHolder
{
protected TextView textView ,contactTV, emailTV, collegename, eventname;
protected ImageView callIcon, mailIcon, eventIcon;

public CustomViewHolder(View view)
{
super(view);
this.textView = (TextView) view.findViewById(R.id.title);
this.contactTV = (TextView) view.findViewById(R.id.contactTV);
this.emailTV = (TextView) view.findViewById(R.id.emailTV);
this.eventname = (TextView) view.findViewById(R.id.eventname);
this.collegename = (TextView) view.findViewById(R.id.collegename);

this.callIcon = (ImageView) view.findViewById(R.id.callIcon);
this.mailIcon = (ImageView) view.findViewById(R.id.mailIcon);
this.eventIcon = (ImageView) view.findViewById(R.id.eventIcon);
}
}
}

AdminActivity.java

public class AdminActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

private static final String TAG = "userList";
private List<FeedItem> feedsList;
private RecyclerView mRecyclerView;
private MyRecyclerAdapter adapter;
private ProgressBar progressBar;
private TextView ParticipantsCounts;
int count=0;
private int number;

private SwipeRefreshLayout swipeRefreshLayout;

private final String url="http://bmcctroika.hol.es/get-data.php";;

private int offSet = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
// Initialize recycler view
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

progressBar = (ProgressBar) findViewById(R.id.progress_bar);
progressBar.setVisibility(View.VISIBLE);

ParticipantsCounts= (TextView) findViewById(R.id.ParticipantsCount);

// Downloading data from below url
new AsyncHttpTask().execute(url);
new AsyncHttpRecordTask().execute(url);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshitems();
}
});
}

private void refreshitems()
{
new AsyncHttpTask().execute(url);
new AsyncHttpRecordTask().execute(url);
}

@Override
public void onRefresh()
{
new AsyncHttpTask().execute(url);
new AsyncHttpRecordTask().execute(url);
}

public class AsyncHttpTask extends AsyncTask<String, Void, Integer> {

@Override
protected void onPreExecute() {
setProgressBarIndeterminateVisibility(true);
}

@Override
protected Integer doInBackground(String... params) {
Integer result = 0;
HttpURLConnection urlConnection;
try {
URL url = new URL(params[0]);
urlConnection = (HttpURLConnection) url.openConnection();
int statusCode = urlConnection.getResponseCode();

// 200 represents HTTP OK
if (statusCode == 200)
{
BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = r.readLine()) != null)
{
response.append(line);
}
parseResult(response.toString());
result = 1; // Successful

} else {
result = 0; //"Failed to fetch data!";
}
} catch (Exception e) {
Log.d(TAG, e.getLocalizedMessage());
}
return result; //"Failed to fetch data!";
}

@Override
protected void onPostExecute(Integer result)
{
// Download complete.
progressBar.setVisibility(View.GONE);
if (result == 1)
{
adapter = new MyRecyclerAdapter(AdminActivity.this, feedsList);
mRecyclerView.setAdapter(adapter);
} else
{
Toast.makeText(AdminActivity.this, "Failed to fetch data!", Toast.LENGTH_SHORT).show();
}
swipeRefreshLayout.setRefreshing(false);
}
}

private void parseResult(String result)
{
try
{
JSONObject response = new JSONObject(result);
JSONArray posts = response.optJSONArray("result");
feedsList = new ArrayList<>();
for (int i = 0; i < posts.length(); i++)
{
JSONObject post = posts.optJSONObject(i);
FeedItem item = new FeedItem();
item.setTitle(post.optString("fullname"));
item.setContact(post.optString("contactno"));
item.setEmail(post.optString("emailaddress"));
item.setEventname(post.optString("eventname"));
item.setCollegename(post.optString("collegename"));
feedsList.add(item);
}
//ParticipantsCounts.setText(String.valueOf(posts.length()));
}
catch (JSONException e)
{
e.printStackTrace();
}
}
public class AsyncHttpRecordTask extends AsyncTask<String, Void, Integer> {


@Override
protected Integer doInBackground(String... params) {
Integer result = 0;
HttpURLConnection urlConnection;
try {
URL url = new URL(params[0]);
urlConnection = (HttpURLConnection) url.openConnection();
int statusCode = urlConnection.getResponseCode();

// 200 represents HTTP OK
if (statusCode == 200)
{
BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = r.readLine()) != null)
{
response.append(line);
}
parseResultnum(response.toString());
result = 1; // Successful

} else {
result = 0; //"Failed to fetch data!";
}
} catch (Exception e) {
Log.d(TAG, e.getLocalizedMessage());
}
return result; //"Failed to fetch data!";
}
}

private void parseResultnum(String result)
{
try
{
JSONObject response = new JSONObject(result);
JSONArray posts = response.optJSONArray("result");
ParticipantsCounts.setText("No. of Participants : "+String.valueOf(posts.length()));
}
catch (JSONException e)
{
e.printStackTrace();
}
}
}

最佳答案

您可以使用 actionView ( example ) 轻松地将 SearchView 添加到 ActionBar 作为菜单项。然后,基于 SearchView.OnQueryTextListener,您将能够将搜索查询应用到您的适配器。到底是什么阻碍了你走这条路?

关于java - 如何使用卡片向 RecyclerView 添加 searchBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37058550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com