0

I'm developing a business profile page which contains profile image contact data and a photo gallery. I'm using Listview to check the urls from the received JSON data.

The problem is: the listview is not displayed on profile_layout. not inflated. Anyone can help?

Thanks a lot.

Here're the codes: ProfileActivity.java

 private void getGalleryJSON(String url) {
    listView = (ListView) findViewById(R.id.gallery_listview);
    galleryAdapter = new GalleryAdapter(this, R.layout.gallery_layout);
    listView.setAdapter(galleryAdapter);
    class GetGalleryJSON extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            String uri2 = params[0];
            BufferedReader bufferedReader = null;
            try {
                URL url = new URL(uri2);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                StringBuilder sb = new StringBuilder();
                bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String json;
                while((json = bufferedReader.readLine())!= null){
                    sb.append(json+"\n");
                }
                bufferedReader.close();
                con.disconnect();
                return sb.toString().trim();
            }catch(Exception e){
                return null;
            }
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            gallery_json = s;
            //TextView tv = (TextView)findViewById(R.id.gallery_content);
            //tv.setText(gallery_json);
            //=====================================
            //======================================
            // JSON OBJECT RECEIVED PERFECTLY
            //======================================
            //======================================
            try {
                gallery_jsonObject = new JSONObject(gallery_json);
                gallery_jsonArray = gallery_jsonObject.getJSONArray("result");
                int count = 0;
                String thumb;
                while (count < gallery_jsonArray.length()){
                    JSONObject JO = gallery_jsonArray.getJSONObject(count);
                    thumb = JO.getString("thumburl");

                    Gallery gallery = new Gallery(thumb);
                    galleryAdapter.add(gallery);
                    count++;
                }
            } catch (JSONException e1) {
                e1.printStackTrace();
            }
        }
    }

    GetGalleryJSON ggj = new GetGalleryJSON();
    ggj.execute(url);
}

This is the adapter GalleryAdapter.java

public class GalleryAdapter extends ArrayAdapter{

List list = new ArrayList();

public GalleryAdapter(Context context, int resource)
{
    super(context, resource);
}

public void add(Profiles object) {
    super.add(object);
    list.add(object);
}

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

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row;
    row = convertView;

    GalleryHolder galleryHolder;
    if(row == null){

        LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.gallery_layout, parent, false);
        galleryHolder = new GalleryHolder();
        galleryHolder.img_url = (TextView) row.findViewById(R.id.img_url);
        row.setTag(galleryHolder);
    }
    else {
        galleryHolder = (GalleryHolder) row.getTag();
    }

    Gallery gallery = (Gallery) this.getItem(position);
    galleryHolder.img_url.setText(gallery.getUrl());

            //=====================================
            //======================================
            // LATER I WILL USE THIS FOR IMAGEVIEW
            //======================================
            //=====        //Picasso.with(this.getContext()).load(gallery.getUrl()).into(galleryHolder.img_url);

    return row;
}

static class GalleryHolder{
    TextView img_url;
}}

And this is the Gallery.java

public class Gallery {
private String url;

public Gallery(String url)
{
    this.setUrl(url);
}
public void setUrl(String url){
    this.url = url;
}
public String getUrl(){
    return url;
}}

I use this layout on profile page

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent"

android:orientation="vertical"
>
<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fitsSystemWindows="true"
    android:orientation="horizontal"
    >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        screen-wide thumb

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:id="@+id/data">contact data
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="top"
            android:padding="10dp"
            android:id="@+id/content">

            <TextView
                android:id="@+id/txt_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:editable="false"
                android:text="Content" />

        </LinearLayout>
        <TextView
            android:id="@+id/gallery_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Gallery"
            android:textAlignment="center"
            />
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="top"
            android:padding="10dp"
            android:id="@+id/gallery">
            <ListView
                android:id="@+id/gallery_listview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:columnWidth="100dp"
                android:drawSelectorOnTop="true"
                android:gravity="center"
                android:numColumns="auto_fit"
                android:stretchMode="columnWidth"
                android:verticalSpacing="5dp"
                android:focusable="true"
                android:clickable="true"

                android:background="#FF0000"
                />
        </LinearLayout>

    </LinearLayout>


</ScrollView>

And this is the gallery_layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="85dp">
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/img_url"

     />
</RelativeLayout>

Note: I am using listview to check the urls only. I will change to GridView later

1 Answers1

0

Probably your listview's height is calculated 'zero'. It's not recommended to put a listview inside scrollview, because the listview can not be scrolled anymore.

But if you insist to do this, calculate the listview's total height & update its height in runtime according to this answer: How to measure total height of ListView But first set the listview's layout_height to wrap_content.

Community
  • 1
  • 1
Shayan_Aryan
  • 2,002
  • 1
  • 29
  • 31
  • Hi thanks to your response. With this case, I see the listview is not scrollable, i changed to gridview and it only contains the first row of 3 rows of images. but when I remove the ScrollView, I can't see the bottom part of the contents It's not scrollable. – Agung Naek Kodok Nov 28 '15 at 07:49
  • Can you please post the new code you've wrote (GridView's xml)? – Shayan_Aryan Nov 28 '15 at 07:55
  • maybe you should pull gridview out from LinearLayout with id="@+id/gallery" – Shayan_Aryan Nov 28 '15 at 08:07
  • It still shows the first row only. when I set the layout_height of the GridView to, let's say, 400dp, the GridView show all of 3 rows of images. so i think the problem is gridview is not expandable based on its content. Thanks. – Agung Naek Kodok Nov 28 '15 at 08:15
  • GridView & ListView are supposed to be like this, their height must be defined. I recommend you change the design (maybe using ViewPager solve this) and reserve the whole screen for the gridview – Shayan_Aryan Nov 28 '15 at 08:29