How to create above respective Views into my android application? - java

I'm creating an application similar to the link given here, where it shows three images transition from one state to another when click.
1) Stage 1: When a series of listview of video files are stored in a video directory. How do i create this particular view into the ListView?
2) Stage 2: When a video file is being click it does not immediately play the video instead it shows a dialog box showing the detail of the file.
3) Stage 3: The user could either Exit, Select Play video or show roadmap details...
Could someone help me i'm kinna new in android/java here, i'm totally lost on how to start creating the above views like how do i populate the Listview with existing video files found in my video directory?

You realize that your basically asking someone to make the application for you? In any case, I shall try to give you some help to get started.
Stage 1: Do you know how to create a ListView? Here's an example:
listView = (ListView) findViewById(R.id.list_view);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, elements); //elements is a List<String>
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//What to do when an item is clicked
}
});
You can customize the look of the list items by creating a XML-file containing a single TextView and then use that when creating the ArrayAdapter (like R.layout.list_item).
If you want a completely custom View, like in the app in the link, you can create your own adapter and then implementing the View getView(int position, View convertView, ViewGroup parent) function, where you return the View you want to be displayed.
Example:
View row = convertView;
if (row == null) {
LayoutInflater mInflater = LayoutInflater.from(getContext());
row = mInflater.inflate(R.layout.bookings_list_item, parent, false);
}
return row;
You can create the elements list by counting the files in the video directory. I don't really know how to do this, but it shouldn't be too hard. Perhaps someone else can provide you with an answer if you don't find out yourself.
Stage 2: Implement this in the OnItemClickListener in the example above by showing a dialog.
Stage 3: Implement what the Dialog will do when it's buttons are pressed. Exit: dismiss popup. Play video: Launch an intent to a video player. I'm not sure about how to show roadmap details, but you can always use Google Maps in your app (there's a sample here).
Now, I hope you can get something useful from this. I hope I have provided you with enough details to be able to get started with researching and a little coding. :)

Related

How to make clickable and open custom alert dialog in android listview?

I'm trying to build my own project list view where i use multiple image view with detail in single row but i can't be make it clickable and use my costume alert dialog layout how can i do it can any one help me. I try many of tutorial example of YouTube and also stack overflow site but those tutorial are not use for me and i'm stuck here i'm also beginner of android java programming.
This is my project github link can anyone help me to solve this project problem
[Link]https://github.com/rotaractnepalapp/SelectedApp/blob/master/app/src/main/java/np/com/rotaractnepalapp/rotaract/ClubReview.java
Implement like this
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CustomDialog dialog = new CustomDialog();
dialog.show(getSupportFragmentManager,"example dialog");
}
});

Updating an AlertDialog's ListView contents while running

I have an instance of an AlertDialog that I am using as a file selection dialog. It includes a hierarchical browsing function - if a directory is selected from the list, it should show the list of files in that directory. It also includes a 'up level' button, that returns to the previous folder. I need a way to update the contents of the AlertDialog object's built-in ListView while the dialog is displaying without reloading the dialog object from its builder. I am aware that Adapters exist, but I need a way to load data from a defined instance variable, not an external XML resource. I am overriding the onResume method to avoid dialog closure on button press, and this is where I need to run the list update.
This is the code I have now for the selection button's OnClick listener inside the onResume method.
alertDialog.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
if(position >= 0)
{
String[] list = getCurrentFileList();
if(list[position].equals(NO_ITEMS_TEXT)){
return;
}
// If the selected file is a directory, recursively update the file list and redisplay.
if(getCurrentFileRefList()[position].isDirectory()){
src = getCurrentFileRefList()[position];
parseFileList();
//todo update ListView from loaded file list
}else { // If the selected item is a file, give the value to the handler and dismiss the dialog.
handler.handleEvent(DialogActionEventHandler.ResultID.SUBMITTED, getCurrentFileRefList()[position]);
alertDialog.dismiss();
}
}
}
});
The parseFileList(); method is used to get the current list of files from the selected source file.
Any help would be appreciated!
You should notify the UI to update the list by calling notifyDatasetChanged
https://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged()
Once you update the data in the adapter, you must call Adapter.notifyDatasetChanged.
I eventually solved the issue with a workaround - here's how.
Since the notifyDatasetChanged() method did not push updates to the dialog correctly without creating an entire adapter class for the initial creation stage - something which I considered far too time-consuming and inefficient for my purposes - I worked around the issue by relaunching the dialog every time an update was needed, and tracked the current directory's position in the master tree relative to the source directory by using an ArrayList with a pointer that updated every time the user switched folders. This process also required making the onCreate() method relaunch-aware, and enabling the onResume() method to be called manually. Once these were done, the resultant section of code looked like this:
alertDialog.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position >= 0)
{
String[] list = getCurrentFileList();
if(list[position].equals(NO_ITEMS_TEXT)){
return;
}
// If the selected file is a directory, recursively update the file list and redisplay.
if(getCurrentFileRefList()[position].isDirectory()){
src = getCurrentFileRefList()[position];
hierarchyID ++;
onCreateDialog(null);
alertDialog.dismiss();
}else { // If the selected item is a file, give the value to the handler and dismiss the dialog.
handler.handleEvent(DialogActionEventHandler.ResultID.SUBMITTED, getCurrentFileRefList()[position]);
alertDialog.dismiss();
}
}
}
});
This discards the old dialog, updates the hierarchy tree tracking array and associated pointer, and relaunches the dialog with the new path. It is significantly more complex than I hoped, but it works well. I hope someone finds this useful!

Synchronizing RecyclerView's getItemCount() with onCreateViewHolder

This problem has been beaten a million times, but unfortunately, no answer on SO has helped solve my issue.
Fundamentally, my question is that, in a RecyclerView with a CardView in a Fragment, when and where should the following tasks be performed:
Fetching data from a server to the local database on the user's device
Processing the so-fetched local data
Instantiating and setting the RecyclerView's adapter
Calling the RecyclerView's notifyfDataSetChanged()
The RecyclerView in my implementation presently calls back only getItemCount(). onCreateViewHolder() and onBindViewHolder() are never getting called back.
(I have seen this answer and a gazillion more.)
Here is the sequence of how its being done:
An AppCompatActivity instantiates a Fragment
The Fragment's layout contains a RelativeLayout with a toolbar and an include tag for a ViewPager. The ViewPager layout contains another include tag that sets a layout with a RecyclerView in a LinearLayout. The layout XML with the CardView is set in the RecyclerView's adapter:
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
Log.wtf(LOG_TAG, "Inside onCreateViewHolder()");
View myView;
LayoutInflater inflater;
inflater = LayoutInflater.from(parent.getContext());
myItemView = inflater.inflate(R.layout.my_row_layout, parent, false);
MyViewHolder myViewHolderItem = new MyViewHolder(myItemView);
return myViewHolderItem;
}
There's no ScrollView in any layout
The Fragment's onCreate() calls a method to fetch data from a server
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Some cache processing
args = getArguments();
myDBHelper = MyDBHelper.getInstance(getActivity());
Intent intent = getActivity().getIntent();
// Search comes from search intent
if (intent.getBooleanExtra(MyContract.MY_SEARCH_REQUEST, false))
{
...
getDataFromCloud("", true);
getDataFromLocalDB(false, item);
} // of if search intent
// Plain intent
else
{
getDataFromCloud("item", false);
getDataFromLocalDB(true, item);
}
} // of onCreate()
This data gets populated into a local database on the user's device
Another method in the Fragment's onCreate()filters the local data into an ArrayList
This ArrayList is presented in the RecyclerView - CardView combo finally to the user.
notifyDataSetChanged() is called in the Fragment's onActivityCreated(). But the fact is, its not making a difference anywhere it is called from. Also, the adapter is instantiated in the onPostExecute() of the AsyncTask that creates the ArrayList for the CardView from the local database.
Here's the log that results on running the app:
01-03 08:47:34.546 13489-14033/com.my A/MyDBHelper class: Create View-1 query: CREATE VIEW
01-03 08:47:34.562 13489-13489/com.my A/MyFragment: getItemCount: 0
01-03 08:47:34.584 13489-14033/com.my A/MyDBHelper class: Create View-2 query: CREATE TABLE
01-03 08:47:34.638 13489-13489/com.my A/MyFragment: getItemCount: 0
01-03 08:47:34.722 13489-13489/com.my A/MyFragment: getItemCount: 2
01-03 08:47:34.743 13489-13489/com.my A/MyFragment: getItemCount: 2
In the above log, the View-1 query fetches server data and the View-2 query processes the so-fetched local data.
I have log statements in the onCreateViewHolder() and onBindViewHolder(). As evident above, these methods are just not being invoked, despite a positive return from getItemCount(), which is the size of the ArrayList. The app just shows the toolbar, and an empty white card, despite there being two records or items to be shown.
Been staring at these logs for the past couple of days with no progress or clues. Many thanks in advance for your expert advice or pointers (and for saving what's left of my sanity)!
Finally, it was Jared Burrows's advice that drove the last nail in the coffin. BoyOboy, the only way to get it right is fight! (...almost always!)
Though I cannot pin-point what was going wrong, here's what I re-did:
Created a fresh new project with only the RecyclerView - CardView combo in a Fragment. Well worth the time and effort, in terms of the clarity it brought in.
Read-up this very good resource.
Switched back to the ListView implementation of my app, first got the CardView right, into the ListView
With a fresh new understanding of RecyclerView, systematically replaced all the ListView code with the RecyclerView counterpart
In the app, the custom RecyclerAdapter and ViewHolder live in their own, independent classes now
Was stuck for another couple of hours with the "RecyclerView: No Adapter attached; skipping layout” error; nothing was showing up in the Fragment. That's when Jared Burrows's advice pitched in.
The RecyclerView is now looking all hunky dory, waiting to throw up the next challenge: animation!

Android ViewPager/PagerAdapter with Data Binding and instantiateItem not executing active view

I am using Android data binding in a MVVM framework. I have a ViewPager setup with a corresponding PagerAdapter. Some of the pages may contain videos. The issue that I am having is that the instantiateItem method in the PagerAdapter always executes the next view in the PagerAdapter instead of the current view. For example say page 1 has no video, but page 2 does. When the user views page 1, the video in page 2 starts playing. Here is the instantiateItem method:
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Service.LAYOUT_INFLATER_SERVICE);
ViewDataBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.main_layout, null, false);
Post post = posts.get(position);
myViewModel = new PostModel(post);
binding.setVariable(BR.model, myViewModel);
container.addView(binding.getRoot());
return binding.getRoot();
}
How can I make the instantiateItem execute the current view instead of the next view? Is the issue related to setOffscreenPageLimit?
You can't because it's main idea of ViewPager - it preload next N pages in order to make swiping smooth. Also actually you can setup it in order to see part of next view.
I think you should use OnPageChangeListener, and manager start/stop of playing of your video when it needed here.
Btw, some advices - you can use that library in order to not write that boilerplate code at all. Here my example of usage.

android listview pagination implementation

Hi am working on an android application. And am using a listview in some of my activities.
The problem is all of my listviews displayed are much longer so that the user needs to scroll the whole list to go for the last item.
Am trying to implement a pagination for this, like at first say only 20 items need to displayed on the listview. And at the end of my listview i need a titlebar which have next & previous buttons and on clicking on next button the listview will load the next records from 21st to 40 and so on.
Am using java rest webservice to load the listview.
Can anyone give me a good suggestion for solving my problem.?
Solution 1:
You can load all the data at once if its not TOO MUCH, store it locally & then you can navigate in that locally stored data. Define some variables like StartPoint & EndPoint & get the desired data from that stored data. Increment decrement the values of StartPoint & EndPoint by using the PreviouButton & NextButton.
Solution 2:
Get only the desired data from your data source for example 10 records each time when a Navigation button is clicked.
I suggest than you load list data in a custom Adapter class that extends BaseAdapter class. Like #oriolpons suggested, you should add a footer view, and when you click on button next call some method that is fetching next for example 20 rows, and then add them in your adapter object and call notifyDataSetChanged().
For example
private OnClickListener mListener = new OnClickListener() {
public void onClick(View v) {
ArrayList<YourObject> al = getSomeData(int startRow, int endRow);
MyCustomAdapter adapter = new MyCustomAdapter();
for(YourObject a : al)
adapter.add(a);
getListView.setAdapter(adapter);
notifyDataSetChanged();
}
};
Hope this helps.
The easiest solution is to add a footer view to the listview. And on the item click listener you can see if it is the last position (load more items), or not
//add the footer before adding the adapter, else the footer will not load!
View footerView = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listfooter, null, false);
this.getListView().addFooterView(footerView);
#Tijo . Refer this site http://www.androidhive.info/2012/03/android-listview-with-load-more-button/. You can have a button which would call the execute method of Async task and that will load the remaining list for you.

Categories

Resources