我正在开发一个包含个人资料图片联系人数据和照片库的商家资料页面。我使用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只检查网址。我稍后会改为GridView
答案 0 :(得分:0)
可能您的列表视图的高度计算为零&#39;。不建议将listview放在scrollview中,因为listview不能再滚动了。
但是如果你坚持这样做,那么计算一下listview的总高度&amp;根据以下答案更新运行时的高度:How to measure total height of ListView 但首先将listview的layout_height设置为wrap_content。