'Cannot use search view to filter list view displaying data pulled from SQL server
I am very new to app development and i'm trying to create an app connect to an sql server. The app pulls data from the server and displays it in a list view. I am now trying to add a search view that filter the list view but i am receiving error saying that i need to create a new constructor. i am struggling to connect it to my existing routine. I would appreciate any help in fixing this.
public class MainActivity extends AppCompatActivity {
ArrayList<ClassListItems> itemsArrayList;
MyAppAdapter myAppAdapter;
ListView listView;
SearchView searchView;
boolean success = false;
ConnectionClass connectionClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.ListView);
connectionClass = new ConnectionClass();
itemsArrayList = new ArrayList<ClassListItems>();
SyncData orderData = new SyncData();
orderData.execute("");
}
private class SyncData extends AsyncTask<String, String, String>
{
String msg = "Error";
ProgressDialog progress;
@Override
protected void onPreExecute()
{
progress = ProgressDialog.show(MainActivity.this,"Synchronising",
"Listview Loading! Please Wait...", true);
}
@Override
protected String doInBackground(String... strings)
{
try {
Connection conn = connectionClass.CONN();
if (conn == null)
{
success = false;
}
else{
String query = "Select Combined FROM Employee_Information";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
if (rs != null)
{
while (rs.next())
{
try {
itemsArrayList.add(new ClassListItems(rs.getString("Combined")));
} catch (Exception ex){
ex.printStackTrace();
}
}
msg = "Found";
success = true;
} else{
msg = "No data found!";
success = false;
}
}
} catch (Exception e)
{
e.printStackTrace();
Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
msg = writer.toString();
success = false;
}
return msg;
}
@Override
protected void onPostExecute(String msg)
{
progress.dismiss();;
Toast.makeText(MainActivity.this, msg + "", Toast.LENGTH_LONG).show();
if (success == false)
{
}
else{
try{
myAppAdapter = new MyAppAdapter(itemsArrayList, MainActivity.this);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(myAppAdapter);
} catch (Exception ex)
{
}
}
}
}
public class MyAppAdapter extends BaseAdapter
{
public class ViewHolder
{
TextView textName;
}
public List<ClassListItems> parkingList;
public Context context;
ArrayList<ClassListItems> arrayList;
private MyAppAdapter(List<ClassListItems> apps, Context context)
{
this.parkingList = apps;
this.context = context;
arrayList = new ArrayList<ClassListItems>();
arrayList.addAll(parkingList);
}
@Override
public int getCount() {
return parkingList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ViewHolder viewHolder= null;
if(rowView == null)
{
LayoutInflater inflater = getLayoutInflater();
rowView = inflater.inflate(R.layout.list_content, parent, false);
viewHolder = new ViewHolder();
viewHolder.textName = (TextView) rowView.findViewById(R.id.textName);
rowView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textName.setText(parkingList.get(position).getName()+"");
return rowView;
}
private void initSearchWidgets()
{
SearchView searchView = (SearchView) findViewById(R.id.searchView);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText)
{
ArrayList<ClassListItems> filteredNames = new ArrayList<ClassListItems>();
MyAppAdapter adapter = new MyAppAdapter(getApplicationContext(), 0, filteredNames);
listView.setAdapter(adapter);
return false;
}
});
}
}
private void setUpList ()
{
listView = (ListView) findViewById(R.id.searchView);
MyAppAdapter adapter;
adapter = new MyAppAdapter(getApplicationContext(), 0, itemsArrayList);
listView.setAdapter(adapter);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
