How to update fields and refresh single row in ListView? - android

Hi I have say 100+ list items in the server and getting list items using pagination concept(Every time I am getting 10 list items at a time). Till then I am fine with my coding.
I have one class named A and has listView;
and my POJO is :
public class StreamBean{
private String description;
private String thumbnailUrl;
public String getDescription() {
return mDescription;
}
public void setDescription(String mDescription) {
this.mDescription = mDescription;
}
public String getThumbnailUrl() {
return thumbnailUrl;
}
public void setThumbnailUrl(String thumbnailUrl) {
this.thumbnailUrl = thumbnailUrl;
}
}
I am getting 10 items in the first service call and storing in List<StreamBean> and passing to adapter and showing the results.Till Now I am clear.
Now, I am updating my description in the new class B and Getting back when it is finished editing(In Editing class I am calling service call).
In Class A I am showing updated description using position as
View v = gridView.getChildAt(position-gridView.getFirstVisiblePosition());
if(v==null){
return;
}
ImageView assetThumbnail = (ImageView) v.findViewById(R.id.asset_thumbnail);
TextView description = (TextView) v.findViewById(R.id.tv_description);
if (streamBean.getImageBean() != null
&& !TextUtils.isEmpty(streamBean.getImageBean()
.getThumbnailUrl())){
Glide.with(this)
.load(streamBean.getImageBean()
.getThumbnailUrl())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(assetThumbnail);
}
if(!TextUtils.isEmpty(streamBean.getDescription())){
description.setText(streamBean.getDescription());
}
Till Now I am fine with my coding.
Now my problems are that:
1) When I am doing editing operation for the same list item, I am getting previous description. How can I get the updated description?
2) When I am getting list using pagination, how can I refresh the listItem and place updated StreamBean into existing one.
May be I am wrong with understanding or questions. Please help me with your answers. Thanks in Advance.

Related

How to make a dynamic filter? Android SDK Java

Help to realize the idea with the application. The application receives JSON from the site, and displays information on the screen (for example, games (image, name of the game, creators, year of release and description)). This is all parted, got the data through the retrofit and output them through recycleview. There is no problem with this, but I can’t think of a filter implementation. The filter must be dynamic, for example the creators and year of release. Activation opens where the CREATORS list goes down and the checkboxes with the studios name go down, and the YEAR of the ISSUE and the checkboxes with the year of release also go after it (only the creators and the year must take information from the data they received from the server via Jason). The idea is to have the first standard check box of the type all, which allows you to immediately output everything that is right at the start of the application, and then click the filter button and choose exactly what interests you. And there should be a button that all this update and returns with specific parameters. And I saw there is a button like a cross on the upper right (above on the actionbar), which possibly cancels everything and sets it back to its original position (all checkboxes only). I really hope for your advice and tips on how to implement this application. Thanks to all
Here is a good example of a filter (I need this one) https://pp.userapi.com/c851332/v851332451/e7308/hhiO3IOHPsg.jpg
in fact, a separate activity, which is dynamically filled with checkboxes on the results obtained from JSON (tobish the name, year, etc.)
POJO class value:
public class Value {
#SerializedName("title")
#Expose
private String title;
#SerializedName("year")
#Expose
private String year;
#SerializedName("genre")
#Expose
private List<String> genre;
#SerializedName("director")
#Expose
private String director;
#SerializedName("desription")
#Expose
private String desription;
#SerializedName("image")
#Expose
private String image;
public String getTitle() {
return title;
}
public String getYear() {
return year;
}
public List<String> getGenre() {
return genre;
}
public String getDirector() {
return director;
}
public String getDesription() {
return desription;
}
public String getImage() {
return image;
}
}
POJO class for list value:
public class Example {
#SerializedName("values")
#Expose
private List<Value> values = null;
public List<Value> getValues() {
return values;
}
}
MainActivity:
public class MainActivity extends Activity {
private static final String TAG = "MoviesApp";
RecyclerView recyclerView;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.filters:
Intent intent = new Intent(this, FiltersActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.rc_movies);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
JSONPlaceHolderApi service = NetworkService.getJSONApi();
Call <Example> call = service.getValues();
call.enqueue(new Callback<Example>() {
#Override
public void onResponse(Call<Example> call, Response<Example> response) {
Example data = response.body();
if(response.isSuccessful()) {
if(data != null) {
MoviesAdapter moviesAdapter = new MoviesAdapter(MainActivity.this, data);
recyclerView.setAdapter(moviesAdapter);
Log.i(TAG, "Response call");
}else{
Log.i(TAG, "Data is null");
}
}else{
Log.i(TAG, "Response does not successful");
}
}
#Override
public void onFailure(Call<Example> call, Throwable t) {
Log.i(TAG, "Failure call");
}
});
}
}
And i cant make FiltersActivity so that it works according to the condition
Sorry for bad english :C
...
Hi Daniil,
using a second activity for filter is not a bad idea in my opinion.
The problem is to update the list every time you apply a new filter because you have to store your filters values somewhere and then update the list after apply.
I suggest you to try using a shared class between activities (maybe a ViewModel) and if you wanna allows the generation of filters you can build the interface of the second activity in a dynamic way during your network call using the values you have received.
All filters value can be stored inside the viewmodel into an appropriate object during the network call (using a background operation to not freeze the UI).
When you call the second activity the shared class will be interrogated to obtain the filters and in the onCreate method you can generate the corresponding Views programmatically from the data you have received after the interrogation.
Also,to make sure the "apply filter" event will be intercepted by the first activity you can use an Event Bus Library, a LiveData Object or an Observer Pattern (Listener) according to the ones that fit your architecture best. When the first activity receives the event you can force and offline filter operation in your data and update the RecyclerView using a notifyDataSetChange().
Another architectural approach is to use a single activity and two fragments, but it require more code despite it's flexibility.This approach fits best with Viewmodels and Livedata and allows you to manage only one activity lifecycle (no worries about the kill of the non focused activities by the system).
Hope this will give you some hints/help.
Cheers

Parse Nested Query Android

Ok been stuck on this for literally weeks, sorry for long question.
Using Parse, making a workout app for Android.
Database tables and columns that are relevant to my problem are:
Exercises
ID| name | description | Musclegroup
Workouts
ID| workoutName| userID
WorkoutExercises (basically a join table between the two)
ID| workoutID (pointer) | exerciseID (pointer)
So a user will be able to create a workout and add in the exercises they want.
So far I've already done:
User can go to category, browse the muscle group, view the exercises in that group
signup/login/logout, update profile
list the current workouts (not the exercises in them) -
I have just entered some exercises into a workout on the db as have gotten stuck on querying current exercises in that workout before worrying about inserting new ones.
The problem:
I'm trying to do a nested query to get the exercise name so once a user clicks Eg. Back Workout it should bring up a list Eg. Deadlift, Rows, Chin ups
So basically in SQL I want to:
Select name
from Exercises
where ID in (select exerciseID from workoutexercises where WorkoutID=xxxx)
Things i'm struggling on:
Making a nested query for a pointer, from what I have read on the net I need to use query.include("exerciseID"); which will tell Parse to include a full Exercise item that I can then use to query? Correct me if wrong? Code below - have I done it correctly?
I've learnt from and been using methods from: http://www.michaelevans.org/blog/2013/08/14/tutorial-building-an-android-to-do-list-app-using-parse/ where query data is put into a custom adapter that lists the data. It uses getters
and setters to save/retrieve String/int values, do I still use the getters for getting a string from within a pointer?
EG.
public String getName()
{
return getString("name");
}
As in once i'm "through" that pointer and in the Exercise table im assuming i'm still just getting the String name value as oppose to getting a ParseObject?
Now so far I have been able to get the custom adapter to put 2 horizontal bars across the screen that shows it knows i've put 3 items in workoutExercises but just not bringing up the text from Exercise name that I need from the nested query
Have a look at my screenshots to see what I mean.
Thank you very much for the help in advance.
Query so far:
public void getcurrentExercisesInWorkout() {
//set progress bar
setProgressBarIndeterminateVisibility(true);
ParseQuery<WorkoutExercises> query = ParseQuery.getQuery("WorkoutExercises");
query.include("exerciseId");
query.whereEqualTo("workoutId", mWorkoutId);
query.findInBackground(new FindCallback<WorkoutExercises>() {
#Override
public void done(List<WorkoutExercises> workoutExercises, ParseException error) {
if (workoutExercises != null) {
mWorkoutExercisesAdapter.clear();
mWorkoutExercisesAdapter.addAll(workoutExercises);
} else {
Log.d("error", error.getMessage());
}
}
});
//stop progress bar
setProgressBarIndeterminateVisibility(false);
}
Custom list Adapter:
//constructor - get current state of parsed object and list of objects retrieved from workout
public WorkoutExercisesAdapter(Context context, List<WorkoutExercises> objects) {
super(context, R.layout.row_item, objects);
this.mContext = context;
this.mWorkoutExercises = objects;
}
public View getView(int position, View convertView, ViewGroup parent)
{
//put each item into the listview
if(convertView == null)
{
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
convertView = mLayoutInflater.inflate(R.layout.row_item,null);
}
WorkoutExercises workoutExercises = mWorkoutExercises.get(position);
TextView nameView = (TextView) convertView.findViewById(R.id.row_name);
//this just calls a String getter - which isn't working - need it to get the Exercise name from within the pointer
nameView.setText(workoutExercises.getWorkoutExercise());
return convertView;
}
WorkoutExercises(stores the getters)
#ParseClassName("WorkoutExercises")
public class WorkoutExercises extends ParseObject {
public String exName;
public WorkoutExercises()
{
}
public String getWorkoutExercise()
{
return getString("name");
}
}
Running Android Studio in Debug mode I can literally see the data I am trying to put into the text field (see screenshot - how can I grab that value? See screenshot below
NOW WORKING - THE RESULT!
First, alter the WorkoutExercises object:
#ParseClassName("WorkoutExercises")
public class WorkoutExercises extends ParseObject {
//public String exName;
public static final String workoutID = "workoutID"
public static final String exerciseID = "exerciseID"
public Exercises getWorkoutExercise() {
return (Exercises)getParseObject(exerciseID);
}
public WorkoutExercises()
{
// not sure if it is allowed to define your own constructor?
// just a note
}
// WorkoutExercises does not have a 'name' col
//public String getWorkoutExercise()
//{
// return getString("name");
//}
}
I assume that Exercises at least contains something like:
#ParseClassName("Exercises")
public class Exercises extends ParseObject {
public static final String name = "name"
public String getName()
{
return getString(name);
}
}
Now, with the query on WorkoutExercises including workoutID, the Exercises object will be populated in the fetched query. This means you can do the following to get the name of the exercises object:
// workoutExercises returned from a query including workoutID
Exercises exercise = workoutExercises.getWorkoutExercise();
String name = exercise.getName();
// if Exercises has getters for description and Musclegroup
String description = exercise.getDescription();
String musclegroup= exercise.getMusclegroup();
Hope this sheds some light on the problem

How to open a Intent with data according to the item clicked on a ListView?

I have a ListView, actually generated by parsing a XML file downloaded from Internet.
This XML file contains data about a person: IdNumber, Name, Age, PhotoURL, Birthday, Phone numbers, Email account, etc.
I get all the XML data when generating the ListView, but on each row I show some values of the person (not all), just name, age, photo (from the PhotoURL) and email.
I would like to get the "IdNumber" to parse it to the Activity that shows all the info, this activity should read the "IdNumber", get all the data of only that person and show it.
How can I parse a value that I'm not using on my ListView?
Thanks in advance,
Herni
For each row in the listView create an Object with all the fields you've originally parsed.
You will create a model that like this:
public class Person
{
String idNumber,Name,Email;
public void SetIdNumber(String p_value)
{
this.idNumber = p_value;
}
public void SetName(String p_value)
{
this.Name= p_value;
}
public void SetEmail(String p_value)
{
this.Email= p_value;
}
//Get Methods
public String GetIdNumber()
{
return idNumber;
}
public String GetName()
{
return Name;
}
public String GetEmail()
{
return Email;
}
When you parse your xml you will create an ArrayList list; and fill after the 'list' you will use this everywhere you want.
For example when you click your first item of your listview you could ;
Person p = list.get(position that clicked your listview);
Now you will use your Person model. This is simple object oriant man.

Fill an ArrayList asynchronously to be displayed in a listview

When the user clicks on a button, a new ListFragment is displayed. This fragment will contain the list of the albums with their associated Artist's name.
I've created my own class AlbumItem (String name, String artist) with name being the Album's title and artist the corresponding artist name :
public class AlbumItem {
private String AlbumName;
private String AlbumArtist;
public AlbumItem(){
}
public AlbumItem(String name, String artist){
this.AlbumName = name;
this.AlbumArtist = artist;
}
public String getAlbumName() {
return AlbumName;
}
public void setAlbumName(String AlbumName) {
this.AlbumName = AlbumName;
}
public String getAlbumArtist() {
return AlbumArtist;
}
public void setAlbumArtist(String AlbumArtist) {
this.AlbumArtist = AlbumArtist;
}
}
Then I wrote my custom adapter which associates the Album's name and Artist's name with the correct TextView in my ListView's row.
So then I can declare an ArrayList of AlbumItem and fill it like this :
ArrayList<AlbumItem> arrayList;
arrayList.add(new AlbumItem ("Album's title", "Artist");
Now I have few questions :
1) Am I going the appropriate way ? I've always learnt to fill listviews like that and I'm very comfortable using this technique (Custom Item class + ArrayList + CustomAdapter), but I'm doing a Music player and I'd like to query the list of Albums and update the listview asynchronously so that the UI is not blocked. I don't know if it is possible to do it by loading data in a ArrayList the way I do it.
2) How to fill up this ArrayList asynchronously ? With LoaderManager/CursorLoader or Asyntask or something else ? (I'm targeting Android 4.0)
Thanks for your advice.
CursorLoader is a convenient way of loading your list, especially if you get the data from a database or another local data source. If your data comes from the network, it may be easier to get the data with an asynctask and load it in the list adapter. If you are familiar with one of these, use that.

How can I read xml data from internet in Android ListView?

How can i read the categories education and work in a android ListView? Now on clicking any category its childs Google,IBM,etc. are shown in a new screen. How can it be implemented?
You have to follow the following steps to get your work done
Step-1 You need to parse the xml. see http://xmlpull.org/v1/src/java/samples/MyXmlPullApp.java
Step-2 Create a bean class for Category. means all the category related data will be stored in that bean class
Step-3 Create an arraylist of that bean class
Step-4 Create a list view using that arraylist ( the list view will display only name of categories)
Step-5 Implement on itemclik listener on the list view. get the position. retrieve the data pass to the intent call second activity
Step-6 In the second activity create a listview and display the data whatever is passed by first activity
NetworkBean
public class NetWorkBean {
String name;
int id;
NetWorkBean(String name, int id){
this.name = name;
this.id = id;
}
public String getNetWorkName(){
return name;
}
public int getNetWorkId(){
return id;
}
}
CategoryBean
import java.util.ArrayList;
public class CategoryBean {
String name;
ArrayList<NetWorkBean> networkList;
public CategoryBean(String name, ArrayList<NetWorkBean> networkList){
this.name = name;
this.networkList = networkList;
}
public String getNetWorkName(){
return name;
}
public ArrayList<NetWorkBean> getNetWorkList(){
return networkList;
}
}
you can see this
http://www.androidpeople.com/android-xml-parsing-tutorial-%E2%80%93-using-domparser
After parsing the data set it to the list view.Then in the list onclick event implement
what you need.You can call an another activity and sending the corresponding address in a
bundle then retrieving that on the activity you can show in webview

Categories

Resources