I need to pass on button click pass argument with url address to private void parseJSON(). Any attempts have failed. The code is attached. The URL is provided as an example and does not exist.
MainActivity.java
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements ExampleAdapter.OnItemClickListener {
public static final String EXTRA_URL = "imageUrl";
public static final String EXTRA_CREATOR = "creatorName";
public static final String EXTRA_LIKES = "likeCount";
private RecyclerView mRecyclerView;
private ExampleAdapter mExampleAdapter;
private ArrayList<ExampleItem> mExampleList;
private RequestQueue mRequestQueue;
EditText editText;
Button btn_poisk;
String poisk;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.edit_text);
btn_poisk=findViewById(R.id.btn_poisk);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mExampleList = new ArrayList<>();
mRequestQueue = Volley.newRequestQueue(this);
parseJSON();
}
private void parseJSON() {
btn_poisk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
poisk = new String(editText.getText().toString());
}
});
String url = "https://mixs.shop.ru/b.php?action=search&query=" poisk;
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("SEARCH");
for (int i = 0; i < jsonArray.length(); i ) {
JSONObject hit = jsonArray.getJSONObject(i);
String KTl = hit.getString("ktl");
String creatorName = hit.getString("spr_3_name");
String imageUrl = hit.getString("tb_image_id") ;
int likeCount = hit.getInt("tb_price_value");
mExampleList.add(new ExampleItem(imageUrl, KTl, creatorName, likeCount));
}
mExampleAdapter = new ExampleAdapter(MainActivity.this, mExampleList);
mRecyclerView.setAdapter(mExampleAdapter);
mExampleAdapter.setOnItemClickListener(MainActivity.this);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void one rrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mRequestQueue.add(request);
}
@Override
public void onItemClick(int position) {
Intent detailIntent = new Intent(this, DetailActivity.class);
ExampleItem clickedItem = mExampleList.get(position);
detailIntent.putExtra(EXTRA_URL, clickedItem.getImageUrl());
detailIntent.putExtra(EXTRA_CREATOR, clickedItem.getCreator());
detailIntent.putExtra(EXTRA_LIKES, clickedItem.getLikeCount());
startActivity(detailIntent);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@ id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Введите текст..."/>
<Button
android:id="@ id/btn_poisk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Поиск"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray" />
</LinearLayout>
</RelativeLayout>
And you can also tell me! How to first clear what the previous request found by pressing the button, and then execute a new one?
CodePudding user response:
The value of poisk
is not updated until the button is pressed but the API call is happening before that.
Try this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.edit_text);
btn_poisk = findViewById(R.id.btn_poisk);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mExampleList = new ArrayList < > ();
mRequestQueue = Volley.newRequestQueue(this);
btn_poisk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
poisk = new String(editText.getText().toString());
parseJSON();
}
});
}
private void parseJSON() {
String url = "https://mixs.shop.ru/b.php?action=search&query=" poisk;
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener < JSONObject > () {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("SEARCH");
for (int i = 0; i < jsonArray.length(); i ) {
JSONObject hit = jsonArray.getJSONObject(i);
String KTl = hit.getString("ktl");
String creatorName = hit.getString("spr_3_name");
String imageUrl = hit.getString("tb_image_id");
int likeCount = hit.getInt("tb_price_value");
mExampleList.add(new ExampleItem(imageUrl, KTl, creatorName, likeCount));
}
mExampleAdapter = new ExampleAdapter(MainActivity.this, mExampleList);
mRecyclerView.setAdapter(mExampleAdapter);
mExampleAdapter.setOnItemClickListener(MainActivity.this);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void one rrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mRequestQueue.add(request);
}