Reputation: 65
I'm trying to do a search function on my database. I first tried to retrieve all data from my database and put it into a listview and then performed a search on that list. The problem is that my app doesn't put those retrieved data from my SQL database to the listview. I tried running my PHP code separately and it works, so I think it's a problem in my java code in android.
companylist.php
<?php
include 'db_connect.php';
$sql = "SELECT * FROM company";
$result = $con->query($sql);
if ($result->num_rows >0) {
while($row[] = $result->fetch_assoc()) {
$tem = $row;
$json = json_encode($tem);
}
} else {
echo "No Results Found.";
}
echo $json;
$con->close();
?>
Search.java
package com.example.activitymanagement;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class Search extends Fragment {
ListView SubjectListView;
ProgressBar progressBarSubject;
String ServerURL = "http://10.0.2.2/member/companylist.php";
EditText editText ;
List<String> listString = new ArrayList<String>();
ArrayAdapter<String> arrayAdapter ;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//returning our layout file
//change R.layout.yourlayoutfilename for each of your fragments
//return inflater.inflate(R.layout.fragment_search, container, false);
View rootView = inflater.inflate(R.layout.fragment_search, container, false);
SubjectListView = (ListView)rootView.findViewById(R.id.listview1);
progressBarSubject = (ProgressBar)rootView.findViewById(R.id.progressBar);
editText = (EditText)rootView.findViewById(R.id.edittext1);
new GetHttpResponse(getActivity()).execute();
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Search.this.arrayAdapter.getFilter().filter(charSequence);
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
}
});
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments different titles
getActivity().setTitle("Search");
}
private class GetHttpResponse extends AsyncTask<Void, Void, Void>
{
public Context context;
String ResultHolder;
public GetHttpResponse(Context context)
{
this.context = context;
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0)
{
HttpServicesClass httpServiceObject = new HttpServicesClass(ServerURL);
try
{
httpServiceObject.ExecutePostRequest();
if(httpServiceObject.getResponseCode() == 200)
{
ResultHolder = httpServiceObject.getResponse();
if(ResultHolder != null)
{
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(ResultHolder);
JSONObject jsonObject;
for(int i=0; i<jsonArray.length(); i++)
{
jsonObject = jsonArray.getJSONObject(i);
listString.add(jsonObject.getString("subjects").toString()) ;
}
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else
{
Toast.makeText(context, httpServiceObject.getErrorMessage(), Toast.LENGTH_SHORT).show();
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
progressBarSubject.setVisibility(View.GONE);
SubjectListView.setVisibility(View.VISIBLE);
arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_2, android.R.id.text1, listString);
SubjectListView.setAdapter(arrayAdapter);
}
}
}
fragment_search.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/searchlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/edittext1"
android:hint="Search Here"
android:gravity="center"/>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="visible"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview1"
android:layout_below="@id/edittext1" />
</RelativeLayout>
Upvotes: 1
Views: 382
Reputation: 1520
According your companylist.php
file response you are getting wrong parameter inside response, instead of getting name
you are getting subjects
, please check below code
try {
JSONArray jsonArray=new JSONArray(ResultHolder);
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
listString.add(jsonObject.getString("name")) ;
}
} catch (JSONException e) {
e.printStackTrace();
}
Upvotes: 3