У меня проблема с Android Studio. Я ввел информацию через PhpMyAdmin. Мой IP-код по какой-то причине не работает.
ApiClient. java
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
class ApiClient {
private static final String BASE_URL = "http://MY IP/medicine/";
private static Retrofit retrofit;
static Retrofit getApiClient(){
if (retrofit==null){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
Адаптер. java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
private List<Drugs> drugs;
public Context context;
Adapter(List<Drugs> drugs, Context context) {
this.drugs = drugs;
this.context = context;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.drugs.setText(drugs.get(position).getDrug());
holder.description.setText(drugs.get(position).getDes());
}
@Override
public int getItemCount() {
return drugs.size();
}
static class MyViewHolder extends RecyclerView.ViewHolder{
TextView drugs,description;
MyViewHolder(View itemView) {
super(itemView);
drugs = itemView.findViewById(R.id.drugs);
description = itemView.findViewById(R.id.description);
}
}
}
MainActivity. java
import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.SearchView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
public RecyclerView.LayoutManager layoutManager;
private List<Drugs> drugs;
private Adapter adapter;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progress);
recyclerView = findViewById(R.id.recyclerView);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
fetchDrugs("medicine", "");
}
public void fetchDrugs(String type, String key){
ApiInterface apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<List<Drugs>> call = apiInterface.getDrugs(type, key);
call.enqueue(new Callback<List<Drugs>>() {
@Override
public void onResponse(@NonNull Call<List<Drugs>> call, @NonNull Response<List<Drugs>> response) {
progressBar.setVisibility(View.GONE);
drugs = response.body();
adapter = new Adapter(drugs, MainActivity.this);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onFailure(@NonNull Call<List<Drugs>> call, @NonNull Throwable t) {
progressBar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "Error\n"+t.toString(), Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
assert searchManager != null;
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
fetchDrugs("medicine", query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
fetchDrugs("medicine", newText);
return false;
}
});
return true;
}
}
AndroidManifest. xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.aptekauz">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:usesCleartextTraffic="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="UnusedAttribute">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.SEARCH"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
</application>
</manifest>
здесь is PHP код: connect. php
<?php
define('host','localhost');
define('name', 'root');
define('pass', 'root');
define('dbase', 'users');
$conn = mysqli_connect(host, name, pass, dbase) or die('Unable to connect');
?>
getcontacts. php
<?php
require_once 'connect.php';
$type = $_GET['item_type'];
if (isset($_GET['key'])) {
$key = $_GET["key"];
if ($type == 'users') {
$query = "SELECT * FROM users WHERE name LIKE '%$key%'";
$result = mysqli_query($conn, $query);
$response = array();
while( $row = mysqli_fetch_assoc($result) ){
array_push($response,
array(
'id'=>$row['id'],
'name'=>$row['name'],
'description'=>$row['description'])
);
}
echo json_encode($response);
}
} else {
if ($type == 'users') {
$query = "SELECT * FROM users";
$result = mysqli_query($conn, $query);
$response = array();
while( $row = mysqli_fetch_assoc($result) ){
array_push($response,
array(
'id'=>$row['id'],
'name'=>$row['name'],
'description'=>$row['description'])
);
}
echo json_encode($response);
}
}
mysqli_close($conn);
?>
Если Я пишу что-то для searchView, он показывает так:
Спасибо)