I am developing an android project in which I have to load from group of String arrays(say title,description,id) to the listview item TextView.
I did something similar with a database using a cursor like this
String[] from = new String[]{"medicine","healthsystem"};
int[] to = new int[] {R.id.textlist1,R.id.textlist2};
// Now creating an array adapter and set it to display using my row
SimpleCursorAdapter notes =new SimpleCursorAdapter(this,R.layout.notes_row, c, from, to);
I listed all the targets in "from" and all the origin in "to".
Now my problem is I don't have a database so cant use a cursor.
I have 3 arrays of strings which i want to load into textviews(title,description,id) of each item
How to do this
Please kindly help me out
thank you :)
If you don't have a cursor, why are you using a SimpleCursorAdapter?
Read this article about creating a SimpleListView using SimpleAdapter for alternate ideas.
There are lots of examples of populating lists from various data sources in the ApiDemos project that comes with the Android SDK - browse through those, and you should find one that fits what you're trying to do.
First of all you need to create a Class that holds that information, something like:
public class StringHolder{
String titte;
String description;
int id;
}
Then you create the layout of your row that says where you want your title, your description and your image.
Then you create an Adapter. The adapter will hold your data and will say for each position in the list what information should be loaded.
At last you need to use your adapter in an activity.
For more information you can see a tutorial here: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
Related
I am trying to learn about developing android TV apps and looking at sample code on github and on some tutorial links. I have grasped some basics around android TV development.
My problem is non of the tutorials properly explain how the browse fragment is populated with data from an online json source and how it is updated.
Can anyone please direct me to a link or source that can be used as a decent tutorial for a beginner?
I don't think there are any tutorials specifically for LeanBack, but you can probably find plenty of general android tutorials on how to use Retrofit to fetch json formatted data from a public API.
As for populating your BrowseFragment, something like this should do it:
//Create a rows adapter for your fragment
ArrayObjectAdapter mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
setAdapter(mRowsAdapter);
//Create a row and populate it
ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(someItemPresenter);
listRowAdapter.setItems(someItems, someDiffCallback);
ListRow row = new ListRow(listRowAdapter);
//Add row
mRowsAdapter.add(row);
someItems should be a List of your fetched items. someItemPresenter should be a class that extends Presenter and handles items of the type contained in the someItems list. someDiffCallback should be a DiffCallback.
I am programming an app for Android. I uploaded it to GitHub: app
I have a ViewPager (MainActivity.java) controlling two Fragments. On the first Fragment (FirstFragment.java) you can add People (People.java) which appears on the RecyclerView (also on FirstFragment.java). When you click one of the list items on the RecyclerView its details (name and id) appear on the second fragment (SecondFragment.java). The SecondFragment.java also contains a button you can delete the selected People with.
To store the People objects I used a List of People and managed it with the methods in PeopleLab.java. The program was working fine: I could add/remove People objects to the list and it appeared on the RecyclerView fine.
After that, I decided to replace the List with a database. It only meant creating the database (the 3 files in database folder) and editing the already existing and two new methods in PeopleLab.java. The other files remained untouched.
The database is working as expected (checked it with sqlite3), I can add/remove People like before and the queries work. My only problem is that the changes don't appear on the RecyclerView. But if I close and reopen the app, the changes appear.
It's like the RecyclerView doesn't care about the database in runtime, only do when the app starts (or closes, not sure).
Do you have any idea what could cause the problem? My only guess is I miss something about how Android apps handle databases.
P.S.: sorry for my English.
You do call notifyDataSetChanged() on the adapter but you don't provide any new data for that adapter.
In your FirstFragment :
private void updateUI() {
PeopleLab peopleLab = PeopleLab.get(getActivity());
List<People> peoples = peopleLab.getPeople();
if(mAdapter == null) {
mAdapter = new PeopleAdapter(peoples);
mRecyclerView.setAdapter(mAdapter);
} else {
// You actually have to change your dataset
mAdapter.changeDataSet(peoples);
}
}
And in your Adapter :
public void changeDataSet(List<People> people) {
this.mPeoples = people;
notifyDataSetChanged();
}
This some brutal way to do it though.
It would be better to notify your adapter on insertion / removal calling notifyItemInserted(int itemPosition) or notifyItemRemoved(int itemPosition). (And refreshing your dataset, by the way)
It will not work automatically.You can either use to notify the adapter the underlying data has been changed so that adapter can fetch and reload the data.It can be done using adapter.notifyDataSetChanged()
Or use can use CursorLoader to achieve the same
i have a lot of recycler views, they all use the same adapter, i used to update them with arraylists of objects that id make as i went like this
ADAPTER
private List<CardWriter> cardMakerList;
public CardAdapter(List<CardWriter> cardMakerList){
this.cardMakerList = cardMakerList;
// this.mContext = context;
}
ACTIVITY
public List<CardWriter> cardMakerList = new ArrayList<>();
public CardAdapter cardAdapter;
recyclerView.setAdapter(cardAdapter);
CardWriter cardWriter = new
CardWriter(getResources().getDrawable(R.drawable.happy),"HAPPY","happy ");
cardMakerList.add(cardWriter);
THE PROBLEM
Ive now switched to using databases for each recycler view (using greenDao see this question) so my adapter now uses the below code (addNewCard refers to the generated java class made by greendao)
public List<addNewCard> leaseList = new ArrayList<>();
but now to get my second database(2) its the exact same properties but greendao makes a different file so i should use this in my adapter
public List<addSimpleCard> leaseList = new ArrayList<>();
but this would mean a new adapter for every database and im going to need a lot of them. All the databases im making are just lots of the same object with different properties (an image and 2 strings) but each database needs to be managed individually.
my last question (in the link above) asks how i can make all the same databases with a different title which i think would also solve my problem.
WHAT IVE TRIED
I've tried just creating an static reference to the array list and passing it the new one eg:
public static CardAdapterDB cardAdapterDB;
cardAdapterDB = new CardAdapterDB(leaseList);
this works but trying to change it for a new database
cardAdapterDB = new CardAdapterDB(simpleleaseList);
gives me an error
CardAdapterDB(java.util.List<greendao.addNewCard>) in CardAdapterDB cannot be applied to (java.util.List<greendao.addSimpleCard>)
addSimpleCard is the name of my other java file generated by greendao so this makes perfect sense but how can i get around this without creating loads of new adapters and switching them all the time?
since asking this ive tried multiple ways to get this to work but have yet to find something suitable
I am creating a feature inside my Android app that will allow users to see their 6 last used apps in a gridview with only the application image. So far I have tried this:
//Load recent used apps
ActivityManager result = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
ArrayList<RecentTaskInfo> apps = (ArrayList<RecentTaskInfo>) result.getRecentTasks(10, ActivityManager.RECENT_WITH_EXCLUDED);
ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,
android.R.layout.simple_list_item_1, apps);
recentappsGridView.setAdapter(adapter);
But this only shows a lot of text in each row and column. How can I fix this/make it happen? Please note that I am already using a ListView within the same activity with a method for its click events.
Create a subclass of ArrayAdapter. Override getView(). Set up your cells to be images, not TextView. Use resolveActivityInfo() on the Intent you get from baseIntent in the RecentTaskInfo to get an ActivityInfo object. Use icon on ActivityInfo to populate your cell.
I haven't tried that recipe, but it should get you closer.
I have a listview with 2 textviews inside of it that are filled by a database using the simple cursor adapter. I want to add an image inside each item in the list with the corresponding image from the database. I have searched around about this topic but have yet to find a solution. any help would be greatly appreciated
String[] from = new String[]{InventoryDbAdapter.KEY_TITLE, InventoryDbAdapter.KEY_DESCRIPTION};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.text1,R.id.text2};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter inventory =
new SimpleCursorAdapter(this, R.layout.row, InventoryCursor, from, to);
setListAdapter(inventory
thanks
Really? Because a quick Google search turned up this blog entry which I think is the same one I used for my Android applications.