我有一个简单的黑客新闻(https://news.ycombinator.com/)应用程序,它在列表视图中显示每篇文章。我已经成功地检索到新闻,甚至填充了Listview
,但是,据我所知,列表视图只是部分填充。我的适配器列表项如下所示:
其中标题,发布时间,用户,点数,评论数量和域名是单独的Textview
。但是,当我运行应用程序时,我得到了这个:
所以我的问题是,出了什么问题?
以下是此列表视图的片段:
package com.material.tdapps.hackernews.activity;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.material.tdapps.hackernews.JSON.JSONNewsParser;
import com.material.tdapps.hackernews.R;
import com.material.tdapps.hackernews.model.StoryFeed;
import com.material.tdapps.hackernews.model.StoryItem;
import java.net.MalformedURLException;
public class HomeFragment extends Fragment {
StoryFeed storyFeed;
Context context;
ListView listView;
NewsListAdapter newsListAdapter;
public HomeFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
context = this.getActivity();
storyFeed = new StoryFeed();
newsListAdapter = new NewsListAdapter(this);
listView = (ListView) rootView.findViewById(R.id.newsListView);
listView.setAdapter(newsListAdapter);
new AsyncLoadNewsFeed().execute();
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
private class AsyncLoadNewsFeed extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
JSONNewsParser newsParser = new JSONNewsParser();
storyFeed = newsParser.parseJSON("https://hacker-news.firebaseio.com/v0/topstories.json",0,30);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (storyFeed == null || storyFeed.getStoryCount() == 0){
StoryItem nullStoryItem = new StoryItem();
nullStoryItem.setTitle("ERROR: Null error!");
nullStoryItem.setNumberComments(0);
nullStoryItem.setScore(0);
nullStoryItem.setBodyText("NULL");
nullStoryItem.setTime(0);
nullStoryItem.setAuthor("NULL");
nullStoryItem.setUrl("http://www.google.com");
storyFeed.addStory(nullStoryItem);
}
newsListAdapter.notifyDataSetChanged();
}
}
class NewsListAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
public NewsListAdapter(HomeFragment homeFragment){
layoutInflater = (LayoutInflater) homeFragment.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return storyFeed.getStoryCount();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
class listViewHolder {
TextView titleTxtV;
TextView timeTxtV;
TextView scoreTxtV;
TextView authorTxtV;
TextView domTxtV;
TextView comTxtV;
ImageButton commB;
listViewHolder(View v) {
titleTxtV = (TextView) v.findViewById(R.id.titleText);
timeTxtV = (TextView) v.findViewById(R.id.timeText);
scoreTxtV = (TextView) v.findViewById(R.id.pointsText);
authorTxtV = (TextView) v.findViewById(R.id.authorText);
domTxtV = (TextView) v.findViewById(R.id.domainText);
comTxtV = (TextView)v.findViewById(R.id.commentsText);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItem = convertView;
listViewHolder holder;
if (listItem == null) {
listItem = layoutInflater.inflate(R.layout.news_item_layout, null);
holder = new listViewHolder(listItem);
listItem.setTag(holder);
} else {
holder = (listViewHolder) listItem.getTag();
}
holder.titleTxtV.setText(storyFeed.getStory(position).getTitle());
holder.timeTxtV.setText("Posted " + storyFeed.getStory(position).getTime());
holder.authorTxtV.setText("By " + storyFeed.getStory(position).getAuthor());
holder.scoreTxtV.setText(storyFeed.getStory(position).getScore() + " Points");
holder.comTxtV.setText(storyFeed.getStory(position).getNumberComments() + " Comments");
try {
holder.domTxtV.setText("(" + storyFeed.getStory(position).getURLDomain() + ")");
} catch (MalformedURLException e) {
e.printStackTrace();
}
return listItem;
}
}
}
我的适配器项目布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="90dp"
android:paddingLeft="4dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:background="#ecf0f1"
android:id="@+id/relLayout">
<ImageButton
android:layout_width="45dp"
android:layout_height="fill_parent"
android:id="@+id/imageButton"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="@drawable/ic_view_comments"
android:background="#fff39c12"
android:layout_alignParentBottom="true"
android:contentDescription="View Comments"
android:layout_alignParentTop="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Pluto's Outer Moons Orbit Chaotically, With Unpredictable Sunrises and Sunsets"
android:id="@+id/titleText"
android:maxLines="2"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/imageButton"
android:layout_toStartOf="@+id/imageButton"
android:ellipsize="end"
android:layout_above="@+id/timeText"
android:layout_alignParentEnd="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="1234 Points"
android:id="@+id/pointsText"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:paddingRight="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="1234 Comments"
android:id="@+id/commentsText"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/pointsText"
android:layout_toEndOf="@+id/pointsText"
android:paddingRight="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="(gizmodo.com)"
android:id="@+id/domainText"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/commentsText"
android:layout_toEndOf="@+id/commentsText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Posted 12 Hours Ago"
android:id="@+id/timeText"
android:layout_above="@+id/pointsText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:paddingRight="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="By user12345"
android:id="@+id/authorText"
android:layout_above="@+id/pointsText"
android:layout_toRightOf="@+id/timeText"
android:layout_toEndOf="@+id/timeText" />
</RelativeLayout>
答案 0 :(得分:1)
listItem = layoutInflater.inflate(R.layout.news_item_layout, parent, false);
您目前正在将行的根元素设置为null,这将导致您的layout_xx属性被忽略
http://possiblemobile.com/2013/05/layout-inflation-as-intended/