I looked at the following site: ListView Example
Which describes how to implement a search function in a listview which uses the default adapter and it works fine.
How can I modify it so I can use the same for a Custom Adapter for my listview?
Partial code is:
dataList = (ListView) findViewById(R.id.lvFiles);
tvQuote = (TextView) findViewById(R.id.tvDisplay);
tvQuote.setTypeface(Typeface.createFromAsset(MainActivity.this.getAssets(), "fonts/roboto.ttf"));
for (int y=0; y<strNamesOfAllah.length;y++) {
name = strNamesOfAllah[y];
meaning = strMeaning[y];
rowsArray.add(new SetRows(R.drawable.icon, name, meaning));
}
adapter = new SetRowsCustomAdapter(MainActivity.this, R.layout.customlist, rowsArray);
dataList.setAdapter(adapter);
dataList.setClickable(true);
You need to override getFilter inside of your adapter and return a new customFilter object that you create. See this answer: No results with custom ArrayAdapter Filter
Edit:
#Override
public Filter getFilter() {
if(customFilter == null){
customFilter = new CustomFilter();
}
return customFilter;
}
Related
I'm trying to perform a search on my recycler adapter when onQueryTextChange
as shown below.
newText = newText.toLowerCase();
List<HymnDataModel> search_list = new ArrayList<>();
for(HymnDataModel hymnDataModel : hymnDataList){
String hymn_title = hymnDataModel.getHymnTitle().toLowerCase();
String hymn_subTitle = hymnDataModel.getHymnSubTitle().toLowerCase();
if (hymn_title.contains(newText) || hymn_subTitle.contains(newText)){
search_list.add(hymnDataModel);
}
}
And i filter the Adapter using the setFilter.
adapterRV.setFilter(search_list);
This is the setFilter function in my Adapter
public void setFilter(List<HymnDataModel> search_list) {
mHymnsList = new ArrayList<>();
mHymnsList.addAll(search_list);
//notify to reload
notifyDataSetChanged();
}
The search works just fine, onQueryTextChange
, but after filtering the Adapter and displaying on the RecyclerView, when i click on the filtered/searched item on my recyclerview, it doesn't open that particular item, instead, it opens another item that's not on the filtered list.
Try this instead.Assign search_list to the existing array List.
public void setFilter(List<HymnDataModel> search_list) {
mHymnsList = search_list;
//notify to reload
notifyDataSetChanged();
}
You are appending Filtered data item's in your Arraylist that's why your adapter display item that's not on the filtered list. try this clear your Arraylist before adding Filtered item in your Arraylist
Try this
public void setFilter(List<HymnDataModel> search_list) {
mHymnsList.clear();
mHymnsList.addAll(search_list);
or
mHymnsList = search_list;
//notify to reload
notifyDataSetChanged();
}
Hello I am trying to call this function loadFiles() which is located in fragmentB and I want to call it from FragmentA, so I can refresh my GridView for my images and videos.
I get this error from the same method
FATAL EXCEPTION: main
java.lang.NullPointerException
my method in FragmentB:
public void loadFiles(){
gridView = (GridView) this.v.findViewById(R.id.grid);
File f = new File(home);
if (f.exists()){
String [] media = f.list();
ArrayList<String> files = new ArrayList<>();
for (int i=0; i<media.length;i++){
if (media[i].endsWith(".jpg")||
media[i].endsWith(".png")||
media[i].endsWith(".JPEG")||
media[i].endsWith(".3gp")||
media[i].endsWith(".mp4")||
media[i].endsWith(".mov")){
files.add(media[i]);
}
if (i==media.length-1){
GridAdapter adapter = new GridAdapter(this.v.getContext(), files, gridView);
adapter.notifyDataSetChanged();
gridView.setAdapter(adapter);
}
}
}
}
How I do call it in FragmentA:
FragmentB b = new FragmentB();
b.loadFiles();
I think the problem is because of this line but I have tried everything and there is no luck :(
GridAdapter adapter = new GridAdapter(this.v.getContext(), files, gridView);
I would suggest you to do like this:
public class Utils{
public static ArrayList<String> loadFiles(){
File f = new File(home);
if (f.exists()){
String [] media = f.list();
ArrayList<String> files = new ArrayList<>();
for (int i=0; i<media.length;i++){
if (media[i].endsWith(".jpg")||
media[i].endsWith(".png")||
media[i].endsWith(".JPEG")||
media[i].endsWith(".3gp")||
media[i].endsWith(".mp4")||
media[i].endsWith(".mov")){
files.add(media[i]);
}
}
return files;
}
}
}
Now in FragmentA and FragmentB etc. you can do
gridView = (GridView) this.v.findViewById(R.id.grid);
GridAdapter adapter = new GridAdapter(this.v.getContext(), Utils.loadFiles(), gridView);
gridView.setAdapter(adapter);
Often you will want one Fragment to communicate with another, for
example to change the content based on a user event. All
Fragment-to-Fragment communication is done through the associated
Activity. Two Fragments should never communicate directly.
See this link here, you will find all required details; also you can download zip file containing sample app.
If you want to call a fragment method is because that fragment exists, so you can find and call it with:
Fragment2 myFragment = (MyFragment2) getFragmentManager().findFragmentByTag("TAG_NAME");
myFragment.method();
If it doesn't exist and you have to instantiate it from Fragment1, then maybe your strategy is not correct and you should put your code in your activity.
private String[] listView2 = {"aa","bb","cc","dd","ee"};
listView1 = (ListView)findViewById(R.id.listView1);
listAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,listView2);
listView1.setAdapter(listAdapter);
Change listView2[1] to "zzz".
Following code isn't work.
listView2[1] = "zzz";
you also need to call
listAdapter.notifyDataSetChanged();
Or use one of the listAdpater methods for changing the data:
add(T), insert(T, int), remove(T), clear()
you can update adapter like this:
adapter.insert("zz", 1);
adapter.notifyDataSetChanged();
Try this.^_^
public void newListView(){
listAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,listView2);
listView1.setAdapter(listAdapter);
}
listView2[0] = "zz";
newListView();
Currently I'm using an ArrayAdapter which I just import. As the question states I want to get images into my listviews. I'm loading information into my list using JSONObjects and storing them into my ArrayLists. I know my current method removes html tages from the JSONObjects.
public class Home extends Activity {
ListView lView;
TextView tView;
ArrayAdapter lAdapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_layout);
lView = (ListView) findViewById(android.R.id.list);
//tView = (TextView) findViewById(android.R.id.);
loadList(lView);
}
public void loadList(ListView lView){
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall("URL", ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
ArrayList<String> titles = new ArrayList<String>();
ArrayList<String> body = new ArrayList<String>();
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray entries = jsonObj.getJSONArray("entries");
for (int i = 0; i < entries.length(); i++) {
JSONObject c = entries.getJSONObject(i);
String text = c.getString("introtext");
if(text == "null"){
text = "No text here" + "\n" + "\n";
}
else {
text = android.text.Html.fromHtml(text).toString();
}
String title= c.getString("title");
String full = title + "\n" + "\n" + text;
titles.add(full);
//body.add(text);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
lAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, titles);
lView.setAdapter(lAdapter);
}
}
Any suggestions would be great help
Thanks
First create a layout same what you want to show in a row, now create a class for a custom adapter like:
public class MyAdapter extends ArrayAdapter<T>{
}
Where T represents the entity class, which represents your data model.
Now under override method:
public View getView(final int position, View convertView, ViewGroup parent) {}
Inflate your layout created for the row, and set each view as you want to show.
Don't forgot to use lazy loading for the image to load over the imageview.
Hope it will help you.
You will need to create a custom adapter that has your own Layout defined. In this layout, you can include whatever you want (including an ImageView, that you can place your image).
There are many examples, but the important thing to know in your code, is you will supply your own adapter here, and set it up a bit differently:
lAdapter = new ArrayAdapter<String>(this, R.layout.my_custom_adapter, titles);
Try to use a Base Adapter
Inflate a custom Xml having a imageview using the base adapter
You can put any views(Image,textview etc ) depending on your
requirement
Finally use picasso or imageloader to populate images using the JSON
response to your base adapter
Hope it helps, Revert back to me if you need any more help, Happy coding
You'll have to create a custom adapter instead of ArrayAdapter, so that you can use your own layout for every row, and perform any 'image loading' there. Try creating a class that extends the BaseAdapter abstract class from the framework. Also, search the web or here (in stackoverflow) you'll find tons of tutorials on how to create a custom adapter.
Since downloading and loading images can be a bit of a pain if you aren't experienced with ListViews and custom adapters, I would suggest looking up two 3rd party libraries for this job:
Picasso or Universal-Image-Loader, each one having examples on how to use them alongside with custom adapters
My Function In SQLAdapter class is
public ArrayList<Airline> getairlinedetails(String bookingdate) {
Cursor curCalllog =db.rawQuery("SELECT * FROM "+ BOOK +
" WHERE " + date +
" BETWEEN '" + startdate + "' AND '" + enddate + "'", null);
if (curCalllog != null) {
if (curCalllog.moveToFirst()) {
do {
a=new Airline();
//a.setBookingdate(curCalllog.getString(1));
a.setPickupadd(curCalllog.getString(2));
a.setCity(curCalllog.getString(3));
a.setTrip(curCalllog.getString(4));
a.setFdate(curCalllog.getString(5));
a.setFtime(curCalllog.getString(6));
a.setCdate(curCalllog.getString(7));
a.setPtime(curCalllog.getString(8));
a.setSeats(curCalllog.getInt(9));
a.setAmount(curCalllog.getInt(10));
update.add(a);
} while (curCalllog.moveToNext());
}
}
return update;
}
M Fetching data between two dates and
I Want To show the fetched data into listview please help me how to do it I m new in android development.
You can use SimpleCursorAdapter for showing Databse contents in Listview. Make instance of SimpleCursorAdapter and pass Cursor object into it. Refer this link
If you want Customized Listview, you can customize SimpleCursorAdapter by extending this with your custom adapter class.
you can follow this example :
DataManipulator.java -helper class
//to retrieve data in a list
public List<String[]> selectAll()
{
List<String[]> list = new ArrayList<String[]>();
Cursor cursor = db.query(TABLE_NAME, new String[] { "id","name","number","skypeId","address" },
null, null, null, null, "name asc");
int x=0;
if (cursor.moveToFirst()) {
do {
String[] b1=new String[]{cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4)};
list.add(b1);
x=x+1;
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
cursor.close();
return list;
}
CheckData.java
// to show data in a list view
public class CheckData extends ListActivity {
TextView selection;
public int idToModify;
DataManipulator dm;
List<String[]> list = new ArrayList<String[]>();
List<String[]> names2 =null ;
String[] stg1;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.check);
dm = new DataManipulator(this);
names2 = dm.selectAll();
stg1=new String[names2.size()];
int x=0;
String stg;
for (String[] name : names2) {
stg = name[1]+" - "+name[2]+ " - "+name[3]+" - "+name[4];
stg1[x]=stg;
x++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_list_item_1,
stg1);
this.setListAdapter(adapter);
selection=(TextView)findViewById(R.id.selection);
}
public void onListItemClick(ListView parent, View v, int position, long id) {
selection.setText(stg1[position]);
}
}
follow this link for complete tutorial
I assume you are getting data from database and there by complete ArrayList with Airline objects.
Now to display data in ListView from ArrayList<Airline>, you have to define Custom adapter class by extending either BaseAdapter or ArrayAdapter.
Here you go: How to define custom adapter for ListView?
I Want To show the fetched data into listview please help me how to do
it I m new in android development
Simpliest way is to use ArrayAdapter with build-in ListView's row layout.
ListView list = (ListView) findViewById(R.id.yourList);
ArrayList<Airline> data = db.getairlinedetails("someString");
ArrayAdapter<Airline> adapter = new ArrayAdapter<Airline>(this,
android.R.layout.simple_list_item_1, data);
list.setAdapter(adapter);
But since this you need to also override toString() method in your Airline class.
Reason is that your ArrayAdapter will convert each child(provided list of airlines) to String and if you won't override toString() you will get default string representation of object but you probably need to show for instance name of airline so your method can looks like
#Override
public String toString() {
return this.name;
}
Note:
This is simple way. But if you want to get more control over ListView and create custom list i recommend you to create own subclass of ListAdapter for example BaseAdapter and define your own Adapter. Sorry but i won't write you implementation because it requires much more code but nice examples you can find here:
Customizing Android ListView Items with Custom ArrayAdapter
Android ListView, ListActivity and ListFragment - Tutorial
Android Custom ListView with Image and Text