Android Studio: Searchview for images, how? - java

I have a Gridview with Images on it, and a search bar on the top of my page. I know how to use the search bar to filter text, but how can I filter images? Is there a way to somehow "tag" the images?
Thanks!

You must create a data model for your images and use an id to filter them.
public class MyDataModel {
int resourceId;
String imageName
public MyDataModel(String name, resourceId);
// constructors getters, setters, etc
}
And in your activity you can create a list with your data:
List<MyDataModel> data = new List();
data.add(new MyDataModel("name", R.drawable.name))
//import all your data
And then on your SearchView query listener add to filter to Adapter;
adapter.getFilter().filter(//name);

Create a list of names of images and for each item of the adapter you tag the names of the images. Now filter the adapter using the tags of the images. :)

Related

Click on tag and view only picture with the same tag

I wouldlike to when I click on tag of one of my picture to view only pictures with the same tag.
In this picture an example of what I want :
Photo
private void initRecyclerView(){
mImageUrls.add("https://c1.staticflickr.com/5/4636/25316407448_de5fbf183d_o.jpg");
mNames.add("Havasu Falls");
mtags.add("#Sun");
mImageUrls.add("https://i.redd.it/0h2gm1ix6p501.jpg");
mNames.add("Mahahual");
mtags.add("#Sun");
mImageUrls.add("https://i.redd.it/k98uzl68eh501.jpg");
mNames.add("Frozen Lake");
mtags.add("#Winter");
RecyclerView recyclerView = findViewById(R.id.recycler_public);
MyRecyclerViewAdapter MyRecyclerViewAdapter = new MyRecyclerViewAdapter(this, mNames, mImageUrls, mtags);
StaggeredGridLayoutManager RecyclerGridLayoutManager = new StaggeredGridLayoutManager(1, LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(RecyclerGridLayoutManager);
recyclerView.setAdapter(MyRecyclerViewAdapter);
}
You must...
Create a model/pojo class to store details like url, name and tag.
Create two list of your pojo class. (ArrayList) one for all data and one for filtered data.
When user clicks on tag...filter your data with that clicked tag and display that list in a recyclerView.
In short use 'Filterable' for recyclerview see this https://www.androidhive.info/2017/11/android-recyclerview-with-search-filter-functionality/

Access string-array resource programmatically and add value to it

I have an empty string-array resource in my strings.xml file like below:
<string-array name="categories"/>
I'm getting array of data from an API back which I need to populate onto a listview. Its ArrayAdapter takes in an #ArrayResource int for its resource like below:
public void showListViewDialog(…, #ArrayRes int arrayResource) {
...
ListView listView = alertDialogView.findViewById(R.id.listViewCategories);
ArrayAdapter arrayAdapter = ArrayAdapter.createFromResource(context,
arrayResource, R.layout.custom_list_item);
listView.setAdapter(arrayAdapter);
}
In my Java class, this is how I am retrieving the string-array resource:
List<String> categoriesToAdd= Arrays.asList(getResources().getStringArray(R.array.categories));
How can I then add the data back to it? I've tried the following but it does not work:
List<String> listCategories = new ArrayList<>();
listCategories.add(categoriesToAdd);
It is not possible to alter the string-array resource at run-time. So you cannot update it to show data in a ListView.
You don't need a string-array resource to create an ArrayAdapter. Instead, you can simply pass an ArrayList to its constructor to create it.
For example:
List<String> data = new ArrayList<>();
// sample data
data.add("Data 1");
data.add("Data 2");
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this /*context*/,
android.R.layout.simple_list_item_1, android.R.id.text1, data);
ListView listView = findViewById(R.id.listView);
listView.setAdapter(adapter);
Now if you want to add data to the list, use:
adapter.add("Data 3");
AFIK, Its not possible to change any resource files at runtime which bundled in APK. What you can do, just fetch your data from API and use it and if you want to save it for later use then save it in SharedPreferences file

Java/Android Arraylist giving addresses

I am trying to display a list in android using guidance from vogella's tutorial for sqlite in android :
this is part of my ProjectListDataSource class (This gets all data from the sqlite database):
public List<ProjectList> getAllProjects() {
List<ProjectList> projects = new ArrayList<ProjectList>();
Cursor cursor = database.query(ProjectListHelper.TABLE_PROJECT_LIST,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
projects.add(cursorToProjectList(cursor));
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return projects;
}
private ProjectList cursorToProjectList(Cursor cursor) {
ProjectList projList = new ProjectList();
projList.setId(cursor.getLong(0));
projList.setProjName(cursor.getString(1));
projList.setProjComment(cursor.getString(2));
projList.setProjDateTime(cursor.getString(3));
return projList;
}
And this is my activity class :
public class ProjectListActivity extends ListActivity implements
OnClickListener {
private static final String TAG = "ProjectListActivity";
private ProjectListDataSource datasource;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_projectlist);
Log.d(TAG, "On Creat'd");
init();
}
private void init() {
// Getting data from database and adding to ListView
datasource = new ProjectListDataSource(this);
datasource.open();
List<ProjectList> values = datasource.getAllProjects();
ArrayAdapter<ProjectList> adapter = new ArrayAdapter<ProjectList>(this,
android.R.layout.simple_list_item_activated_1, values);
setListAdapter(adapter);
}
But on doing that I am getting unexpected result like this (see image) :
http://i.imgur.com/tQMooi8.png
But the database has records like this (see image):
http://i.imgur.com/HfY2azs.png
Can anyone please explain and give a solution as to why I cant get the list view to show the records as in the database...
Thanks,
Viney
Basic adapters, like ArrayAdapter or CursorAdapter, will map only one value to a single view. Here, a ProjectList object to a TextView with the id of android.R.id.simple_list_item_activated_1
You need a single layout(for a single view within the ListView) with multiple views to which you will map id, name, date, comment, etc. You need to extend one of the adapters. Preferably BaseAdapter or CursorAdapter.
There are several issues here, depending on what you want to display.
The reason you're seeing the object string reference in your list is because you're using a plain ArrayAdapter, which simply calls toString() on the objects in the array. If you override toString() in ProjectList, you can display what you want (though that's normally not the best way to solve this problem).
Another option which would allow you to keep using a plain ArrayAdapter would be to create an array of strings from the ProjectList objects of the data you want to display. A bit wasteful, but that's another option.
What you normally want to do is extend ArrayAdapter and override getView(). In getView() you assign the data you want to display in the view.
If you want to display all the data from your ProjectList objects in a single list item, you'll also need to create a custom layout to represent the row.

java android load items to listview from database?

Okay so i have a database, and i want it so you can favorite items.
Would i make it so you can save items then it will load it into a list view?
this is my load thing
//Calls the database, gets a list of names.
// if listofnames.size()==0 keep name, otherwise
// change name to first name.
ArrayList<String> nameList = new ArrayList<String>();
favList = db.getName();
if(favList.size()>0){
name.setText(favList.get(0));
But that just sets a text i want it to add items..
You may use ArrayAdapter or SimpleAdapter or BaseAdapter - through which you may bind dataSource (List<T>) to the ListView.
You can do this as follows
public class MyClass extends ListActivity{
public void onCreate(Bundle bundle){
//get the names from database
setListAdapter(new ArrayAdapter<E>(this,R.layout.xml_filename,your_list);
}
}
Remember in this case your xml file should be the TextView (I.e the items what list view should contain). You cannot pass an xml file with a ListView directly.
If you have still some problem, then post your code which can be solved.
You can follow the given link for more clarification.
http://developer.android.com/resources/tutorials/views/hello-listview.html

Android ListView Filtering Without Repeatedly Instantiating ArrayAdapters

I have a ListView filled with twitter tweets. I made a custom Tweet class so I could store a special type number. So, I have the Tweet class and custom ArrayAdapter:
class Tweet {
private int typeNumber; // My unique type number which does not come from Twitter
private String message;
// ... other tweet data
}
class TweetsAdapter extends ArrayAdapter<Tweet> {
private ArrayList<Tweet> tweets;
TweetsAdapter(Context context, int textViewResourceId, ArrayList<Tweet> tweets) {
super(context, textViewResourceId, tweets);
this.tweets = tweets;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Inflate my own view and set my own tweet row information
}
}
There is a nav bar which has 4 Buttons representing the 4 special type numbers.
I want to display a filtered tweet ListView when a person clicks on one of the type number buttons without destroying the data in the ArrayAdapter.
One approach I came up with is to break up my main tweets ArrayList into sub-ArrayLists and set a new ArrayAdapter with the corresponding sub-ArrayList on the ListView every time you click one of the Buttons. That seems inefficient to instantiate a new ArrayAdapter every time.
Is there a better way to display a filtered ListView while maintaining all the tweet data on the ArrayAdapter(I also tried looking at the filter class and it seems to destroy the data on the adapter to "filter")?
It seems like your approach of making 4 ArrayLists is fine- just make an array of 4 ArrayAdapters. You could even go one step further and make 4 ListViews and simply set the visibility to GONE or VISIBLE depending on which one is selected.

Categories