【问题标题】:Android Studio Unable to Display Photo Gallery on ListViewAndroid Studio 无法在 ListView 上显示照片库
【发布时间】:2016-03-02 07:25:49
【问题描述】:

我正在开发一个包含个人资料图片联系人数据和照片库的业务资料页面。我正在使用 Listview 从收到的 JSON 数据中检查 url。

问题是:listview 没有显示在 profile_layout 上。没有膨胀。任何人都可以帮忙吗?

非常感谢。

代码如下: 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);
}

这是适配器 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;
}}

这是 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;
}}

我在个人资料页面上使用此布局

<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>

这是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>

注意: 我正在使用 listview 仅检查 url。稍后我会改成GridView

【问题讨论】:

  • 抱歉,已解决。我必须在 GalleryAdapter.java 上使用 Gallery 而不是 Profiles ..

标签: android listview layout gallery


【解决方案1】:

您的列表视图的高度可能计算为“零”。不建议在scrollview里面放listview,因为listview不能再滚动了。

但是,如果您坚持这样做,请根据以下答案计算列表视图的总高度并在运行时更新其高度:How to measure total height of ListView 但首先将 listview 的 layout_height 设置为 wrap_content。

【讨论】:

  • 您好,感谢您的回复。在这种情况下,我看到列表视图不可滚动,我改为网格视图,它只包含 3 行图像的第一行。但是当我删除 ScrollView 时,我看不到内容的底部它不可滚动。
  • 能否请您发布您编写的新代码(GridView 的 xml)?
  • 也许你应该使用 id="@+id/gallery" 从 LinearLayout 中拉出 gridview
  • 它仍然只显示第一行。当我将 GridView 的 layout_height 设置为 400dp 时,GridView 显示所有 3 行图像。所以我认为问题是 gridview 不能根据其内容进行扩展。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-05
  • 1970-01-01
  • 1970-01-01
  • 2018-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多