I've been working on an Android project, which uses three different activities.
They're basically all Listviews
. The ListView
isn't hardcoded in the project, it is being fetched from a DB.
I'm having trouble trying to connect the Search function to the listview
. So far I've seen lots of tutorials teaching to create an ArrayAdapter
to communicate with a hard-coded Listview
, but I can't seem to figure out how to connect it to search from a SQL-based listview
.
I found a somewhat similar question from HERE, tried to create an extra ArrayAdapter
, but got nowhere.
Result is , when I click on the search bar, and try to type any letters to see an actual result, it doesn't do anything.
Here's my MainActivity
package com.example.ragnar.sortiment;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SearchView;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class SortimentActivity extends AppCompatActivity {
List<Sortiment> sortimentMap = new ArrayList<>();
SortimentAdapter sortimentAdapter;
Context thisContext;
EditText edtSearch;
ListView myListView;
ArrayAdapter<String> searchAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sortiment);
Integer selectedStoreId = getIntent().getExtras().getInt("store_id");
edtSearch = (EditText)findViewById(R.id.edtSearch);
myListView = (ListView) findViewById(R.id.storesListView);
searchAdapter = new ArrayAdapter<String>(this,R.layout.list_item);
thisContext = this;
edtSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
SortimentActivity.this.searchAdapter.getFilter().filter(charSequence);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
GetSortimentForStore retrieveData = new GetSortimentForStore();
retrieveData.execute(selectedStoreId);
}
private class GetSortimentForStore extends AsyncTask<Integer,String,String> {
String msg = "";
// JDBC Driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
// Example 10.20.30.40:3306
static final String DB_URL = "jdbc:mysql://" +
DbStrings.DATABASE_URL + "/" +
DbStrings.DATABASE_NAME;
@Override
protected void onPreExecute() {
//progressTextView.setText("Ühendan andmebaasi...");
}
@Override
protected String doInBackground(Integer... selectedStores) {
Connection conn = null;
Statement stmt = null;
try {
Integer selectedStore = selectedStores[0];
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, DbStrings.USERNAME, DbStrings.PASSWORD);
stmt = conn.createStatement();
String sql = "select id, store_id, name, ean, description from myproject.sortiments WHERE store_id = " + selectedStore;
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
Sortiment newSortiment = new Sortiment();
newSortiment.id = rs.getInt("id");
newSortiment.name = rs.getString("name");
newSortiment.store_id = rs.getInt("store_id");
newSortiment.ean = rs.getString("ean");
newSortiment.description = rs.getString("description");
sortimentMap.add(newSortiment);
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException connError) {
msg = "An exception was thrown for JDBC.";
connError.printStackTrace();
} catch (ClassNotFoundException e) {
msg = "A class not found exception was thrown.";
e.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String msg) {
sortimentAdapter = new SortimentAdapter(thisContext, sortimentMap);
myListView.setAdapter(sortimentAdapter);
}
}
}
The Adapter for ListViews
package com.example.user.sortment;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import java.util.List;
/**
* Created by User on 11.05.2018
*/
public class SortmentAdapter extends BaseAdapter implements Filterable {
LayoutInflater mInflator;
List<Sortiment> map;
public void filterResults(CharSequence filter){
}
public SortimentAdapter(Context c, List<Sortiment> inputMap) {
mInflator = (LayoutInflater)
c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
map = inputMap;
}
@Override
public int getCount() {
return map.size();
}
@Override
public Object getItem(int position) {
return map.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = mInflator.inflate(R.layout.item_layout,null);
TextView nameTextView = (TextView) v.findViewById(R.id.nameTextView);
TextView priceTextView = (TextView) v.findViewById(R.id.priceTextView);
Sortiment viewObject = map.get(position);
nameTextView.setText(viewObject.name);
//priceTextView.setText(String.format("%.0f", prices.get(position)));
priceTextView.setText(viewObject.ean.toString());
return v;
}
@Override
public Filter getFilter() {
return null;
}
}
And AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.sortment">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".StoreActivity" />
<activity android:name=".SortmentActivity"
android:windowSoftInputMode="stateHidden">
</activity>
</application>
</manifest>
And my .xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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.user.sortment.MainActivity">
<EditText
android:id="@+id/edtSearch"
android:hint="Search.."
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/storesListView"
android:layout_width="361dp"
android:layout_height="447dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressTextView"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
app:layout_constraintHorizontal_bias="0.571"
tools:layout_editor_absoluteY="48dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.077"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.081" />
<TextView
android:id="@+id/franchiseTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TOODE"
android:textSize="14sp"
app:layout_constraintBottom_toTopOf="@+id/storesListView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.05"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.8" />
<TextView
android:id="@+id/EANtextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="EAN"
app:layout_constraintBottom_toTopOf="@+id/storesListView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.841"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.8" />
</android.support.constraint.ConstraintLayout>