I am fairly beginner and currently writing an app which contains a list of Restaurants. I am using a SQLite database to store the "Restaurant" objects and am using a CursorAdapter to display them into a ListView.
Currently the fields are a photo, the name of the restaurant and its location.
I want to have one more TextView which shows how many times that certain restaurant appears in the database. I'm having trouble figuring out how to do this inside my CursorAdapter class.
Below is my bindView method in the CursorAdapter class where the existing views get updated.
#Override
public void bindView(View view, Context context, Cursor cursor) {
// Find individual views that we want to modify in the list item layout
ImageView foodImageView = (ImageView) view.findViewById(R.id.food_image_view);
TextView restaurantNameTextView = (TextView) view.findViewById(R.id.restaurant_name_text_view);
TextView restaurantLocationTextView = (TextView) view.findViewById(R.id.restaurant_location_text_view);
// Find the columns of restaurant attributes that we're interested in
int foodPictureColumnIndex = cursor.getColumnIndex(RestaurantEntry.COLUMN_FOOD_PHOTO_URL);
int restaurantNameColumnIndex = cursor.getColumnIndex(RestaurantEntry.COLUMN_RESTAURANT_NAME);
int restaurantLocationColumnIndex = cursor.getColumnIndex(RestaurantEntry.COLUMN_RESTAURANT_LOCATION);
// Read the restaurant attributes from the Cursor for the current restaurant
String foodPhoto = cursor.getString(foodPictureColumnIndex);
String restaurantName = cursor.getString(restaurantNameColumnIndex);
String restaurantLocation = cursor.getString(restaurantLocationColumnIndex);
// Update the Views with the attributes for the current restaurant
Picasso.with(mContext).load(foodPhoto).into(foodImageView);
restaurantNameTextView.setText(restaurantName);
restaurantLocationTextView.setText(restaurantLocation);
}
Answered my own question; used a temporary ArrayList to track how many times a certain name appears in the database table
Related
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/
I have an app that features chat rooms. Every room has an ID that serves as a database reference, and messages are brought down from there and displayed in a RecyclerView. I know how to increase the number of messages that are downloaded onCreate/onStart by using Query.orderbykey().limittolast(), but how do I download and display additional items when the user scrolls to the top of the RecyclerView of the chat activity, a la Facebook messenger?
Edit: Here is my adapter's construct0r:
public ChatRecyclerViewAdapter(Context mContext, ArrayList<String> mMessage, ArrayList<String> mAuthor, String mRoomID, DatabaseReference reference) {
this.mContext = mContext;
this.mRoomID = mRoomID;
numberOfRecentMessages=20;
messageList = new ArrayList<>();
mDatabaseReference = reference.child(mRoomID+"_messages");
recentMessages = mDatabaseReference.orderByKey().endAt(100).limitToLast(numberOfRecentMessages);
recentMessages.addChildEventListener(mListener);
}
It sounds like you're looking for endAt(), which takes the key of the last item to return. Say you you're currently showing these keys:
key20
key21
key22
key23
key24
key25
key26
key27
key28
key29
Then you can get the previous items with:
ref.orderByKey().endAt("key20").limitToLast(11)
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.
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
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.