sri sri - 6 months ago 20
Android Question

How to set all values in listview android

I have created litstview. All values are reterived from the json response but only the last values were displayed in the list.what to do for displaying the all values in list view.`

SixFragment.movieList = new ArrayList<Movie1>();
for (int i1 = 0; i1 < jsonArray3.length(); i1++) {
try {
JSONObject jsonObject = jsonArray3.getJSONObject(i1);
review_rating = jsonObject.optString("review_rating").toString();
username_rate = jsonObject.optString("username").toString();
review_title = jsonObject.optString("review_title").toString();
review_desc = jsonObject.optString("review_desc").toString();
sleep = jsonObject.optString("Sleep").toString();
location = jsonObject.optString("Location").toString();
service = jsonObject.optString("Service").toString();
rooms = jsonObject.optString("Rooms").toString();
cleanliness = jsonObject.optString("Cleanliness").toString();
userimage1 = jsonObject.optString("user_image").toString();

Movie1 movie = new Movie1();


movie.setRate_userimage(userimage1);
movie.setreview_rating(review_rating);
movie.setusername_rate(username_rate);
movie.setreview_title(review_title);
movie.setreview_desc(review_desc);
movie.setsleep(sleep);
movie.setlocation(location);
movie.setservice(service);
movie.setraterooms(rooms);
movie.setcleanliness(cleanliness);

SixFragment.movieList.add(movie);

} catch (JSONException e) {
e.printStackTrace();
}

}


This is my fragment page:

public class SixFragment extends Fragment{
public static String userimage;
public static String review_rating;
public static String username_rate;
public static String review_title;
public static String review_desc;
public static String sleep;
public static String location;
public static String service;
public static String rooms;
public static String cleanliness;
public static ArrayList stringArray;
public static ArrayList<Movie1> movieList= new ArrayList<Movie1>();;

ListView listView;
Reviewadapter reviewadapter;

public SixFragment() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_six, container, false);
ListView list=(ListView)v.findViewById(R.id.six_listView);



return v;
}

}


This logcat error`

E/AndroidRuntime: FATAL EXCEPTION: main


Process: com.airstar.abservetech.airstar, PID: 27390
java.lang.NullPointerException
at com.airstar.abservetech.adapter.Reviewadapter.getCount(Reviewadapter.java:160)
at android.widget.ListView.setAdapter(ListView.java:488)
at com.airstar.abservetech.airstar.SixFragment.onCreateView(SixFragment.java:75)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)


`

Review adapter

public class Reviewadapter extends BaseAdapter {
private final List<Movie1> movieItems;
public SixFragment context;

OneFragment Fragment;
Bitmap bitmap;
public Reviewadapter(SixFragment context, List<Movie1> movieItems) {
this.context = context;
this.movieItems = movieItems;
this.Fragment=Fragment;
}

public View getView(int position, View convertView, ViewGroup parent) {

if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.fragment_sixlist,parent,false);

CircleImageView profile_image = (CircleImageView) convertView.findViewById(R.id.profile_image);
TextView rate_username = (TextView) convertView.findViewById(R.id.rate_username);
RatingBar ratingBar = (RatingBar) convertView.findViewById(R.id.ratingBar);
TextView rate_title = (TextView) convertView.findViewById(R.id.rate_title);
TextView rate_description = (TextView) convertView.findViewById(R.id.rate_description);
RatingBar ratingBar_sleep = (RatingBar) convertView.findViewById(R.id.ratingBar_sleep);
RatingBar ratingBar_location = (RatingBar) convertView.findViewById(R.id.ratingBar_location);
RatingBar ratingBar_service = (RatingBar) convertView.findViewById(R.id.ratingBar_service);
RatingBar ratingBar_clearness = (RatingBar) convertView.findViewById(R.id.ratingBar_clearness);
RatingBar ratingBar_rooms = (RatingBar) convertView.findViewById(R.id.ratingBar_rooms);

final Movie1 m = movieItems.get(position);


// Glide.with(context)
// .load(m.getRate_userimage())
// .diskCacheStrategy(DiskCacheStrategy.ALL)
// .into(profile_image);
URL url = null;
String image=m.getRate_userimage();
try {
url = new URL(image);
InputStream is = null;
// Bitmap bitMap = BitmapFactory.decodeStream(is);
//create imageview dynamically

Glide.with(context).load(String.valueOf(url)).into(profile_image);

} catch (MalformedURLException e) {
e.printStackTrace();
}
//Glide.with(context).load(String.valueOf(url)).into(profile_image);

if (m.getusername_rate()!=null)
{
if (!m.getusername_rate().equals("null"))
{
rate_username.setText(m.getusername_rate());
}}

if (m.getreview_rating()!=null)
{
if (!m.getreview_rating().equals("null"))
{
ratingBar.setRating(Float.parseFloat(m.getreview_rating()));

}}


if (m.getsleep()!=null)
{
if (!m.getsleep().equals("null"))
{
ratingBar_sleep.setRating(Float.parseFloat(m.getsleep()));

}}


}


return convertView;
}

@Override
public int getCount() {
return movieItems.size();
}

@Override
public Object getItem(int location) {
return movieItems.get(location);
}

@Override
public long getItemId(int position) {
return position;
}
}

Answer

In Your Fragment make your list static:

public class SixFragment extends Fragment{

      // Your code
//------------DO THIS------------------///

public static List<Movie1> movieList = new ArrayList<Movie1>();


public SixFragment() {
    // Required empty public constructor
}

//Your code

}

Rest of the code in your fragment will be as it is.

Change your JSON loop like this :

SixFragment.movieList = new ArrayList<Movie1>();
    for (int i1 = 0; i1 < jsonArray3.length(); i1++) {
        try {
            JSONObject jsonObject = jsonArray3.getJSONObject(i1);
            review_rating = jsonObject.optString("review_rating").toString();
            username_rate = jsonObject.optString("username").toString();
            review_title = jsonObject.optString("review_title").toString();
            review_desc = jsonObject.optString("review_desc").toString();
            sleep = jsonObject.optString("Sleep").toString();
            location = jsonObject.optString("Location").toString();
            service = jsonObject.optString("Service").toString();
            rooms = jsonObject.optString("Rooms").toString();
            cleanliness = jsonObject.optString("Cleanliness").toString();
            userimage1 = jsonObject.optString("user_image").toString();

            Movie1 movie = new Movie1();


            movie.setRate_userimage(userimage1);
            movie.setreview_rating(review_rating);
            movie.setusername_rate(username_rate);
            movie.setreview_title(review_title);
            movie.setreview_desc(review_desc);
            movie.setsleep(sleep);
            movie.setlocation(location);
            movie.setservice(service);
            movie.setraterooms(rooms);
            movie.setcleanliness(cleanliness);

            SixFragment.movieList.add(movie);

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }