我正在尝试将自定义适配器添加到片段中的列表视图中。它运行正常,但它没有显示碎片中的任何内容。我尝试了很多方法,但我无法让它发挥作用。任何帮助都会很棒。提前致谢。这是我的代码: 的 fragment.xml之
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/UniFeeList"
android:layout_centerHorizontal="true"
android:clickable="true"
>
</ListView>
<android.support.design.widget.FloatingActionButton
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_input_add"
android:id="@+id/addQuestion"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="10dip"
/>
</RelativeLayout>
Fragment.Java
public class UniFeedFragment extends Fragment {
ListView uniFeedListview;
FloatingActionButton askQuestion;
String stdEmail, UniEmail;
TextView tv;
Post post;
String date,time;
ListView UniFeedList;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup view = (ViewGroup) inflater.inflate(R.layout.unifeedfragment, container, false);
//Getting Time
DateFormat df = new SimpleDateFormat("HH:mm");
time = df.format(Calendar.getInstance().getTime());
//Getting Date
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
Date today = new Date();
date = dateFormatter.format(today);
//Getting data from news feed search bar
UniFeed uniFeed = (UniFeed) getActivity();
stdEmail = uniFeed.stdEmail;
UniEmail = uniFeed.uni.getEmail();
//Setting Listview
UniFeedList = (ListView) view.findViewById(R.id.UniFeeList);
final ArrayList<Post> postArray = new ArrayList<Post>();
PostListAdapter PostList = new PostListAdapter(this.getActivity(), R.layout.activity_uni_feed ,postArray);
UniFeedList.setAdapter(PostList);
return view;
}
PostListAdapter.java
public class PostListAdapter extends BaseAdapter {
Context context = null;
ArrayList<Post> postData = new ArrayList<>();
LayoutInflater inflater;
public PostListAdapter(Context context, int activity_uni_feed, ArrayList<Post> postData){
this.context = context;
this.postData = postData;
}
@Override
public int getCount() {
return postData.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView= inflater.inflate(R.layout.unifeedpost, parent, false);
return convertView;
} }
unifeedPost.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/stdProfileImage"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name"
android:id="@+id/stdName"
android:layout_alignTop="@id/stdProfileImage"
android:layout_toRightOf="@+id/stdProfileImage"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Date and Time"
android:id="@+id/time"
android:layout_below="@+id/stdName"
android:layout_toRightOf="@+id/stdProfileImage"
android:layout_toEndOf="@+id/stdProfileImage" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/postContent"
android:layout_below="@id/time"
android:text="Test Post"
android:layout_marginTop="10dip"
android:layout_toRightOf="@id/stdProfileImage"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/postContent"
android:layout_toRightOf="@id/stdProfileImage"
android:layout_marginTop="10dip"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Like"
android:layout_weight="0.5"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comment"
android:layout_weight="0.5"
/>
</LinearLayout>