我试图在片段上获取自定义列表视图,但我只能通过活动来完成它。
这是我的代码:
MyActivity:
package com.teampainite.testreviews;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MyActivity extends Activity {
private List<ReviewAdapter> Review = new ArrayList<ReviewAdapter>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
populateReviews();
populateReviewList();
}
private void populateReviewList() {
Review.add(new ReviewAdapter("Okay", 4, "Levi", "This is an example"));
}
private void populateReviews() {
ArrayAdapter<ReviewAdapter> adapter = new ReviewList();
ListView list = (ListView) findViewById(R.id.list);
list.setFastScrollEnabled(true);
list.setAdapter(adapter);
}
private class ReviewList extends ArrayAdapter<ReviewAdapter> {
public ReviewList() {
super(MyActivity.this, R.layout.review_layout, Review);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View ReviewView = convertView;
if(ReviewView == null){
ReviewView = getLayoutInflater().inflate(R.layout.review_layout, parent, false);
}
//Find view
ReviewAdapter currentReview = Review.get(position);
TextView title = (TextView)ReviewView.findViewById(R.id.review_list_title);
RatingBar rating = (RatingBar)ReviewView.findViewById(R.id.review_rating);
TextView user = (TextView)ReviewView.findViewById(R.id.review_user);
TextView review = (TextView)ReviewView.findViewById(R.id.review_content);
title.setText(currentReview.getTitle());
rating.setRating(currentReview.getRating());
user.setText(currentReview.getUser());
review.setText(currentReview.getReview());
rating.setEnabled(false);
return ReviewView;
}
}
}
ReviewAdapter:
package com.teampainite.testreviews;
public class ReviewAdapter {
private String Title;
private float Rating;
private String User;
private String Review;
public ReviewAdapter(String Title, float Rating, String User, String Review){
super();
this.Title = Title;
this.Rating = Rating;
this.User = User;
this.Review = Review;
}
public String getTitle(){
return Title;
}
public float getRating(){
return Rating;
}
public String getUser(){
return User;
}
public String getReview(){
return Review;
}
}
有人可以帮我把它放在片段上吗?我想使用基于xml的listview,因为主片段上还有其他元素。没有其他问题可以解决这个问题,我已经找到了。
片段代码:
package com.teampainite;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import com.teampainite.adapter.ReviewAdapter;
import java.util.ArrayList;
import java.util.List;
public class RestarauntFragment extends Fragment {
private List<ReviewAdapter> Review = new ArrayList<ReviewAdapter>();
public RestarauntFragment() {
// Required empty public constructor
}
LayoutInflater inflater;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_restaraunt, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ArrayAdapter<ReviewAdapter> adapter = new ArrayAdapter(getActivity(), R.layout.review_list_item, Review);
ListView list = (ListView)view.findViewById(R.id.reviews_list);
list.setAdapter(adapter);
final ImageView favorite = (ImageView) view.findViewById(R.id.favorite);
favorite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
favorite.setImageDrawable(getActivity().getResources().getDrawable(R.drawable.ic_action_favorite_full));
}
});
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.restaraunt, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
showStoreListings();
break;
case R.id.show_menu:
showMenu();
break;
}
return super.onOptionsItemSelected(item);
}
private void showStoreListings() {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.hide(RestarauntFragment.this);
transaction.replace(android.R.id.widget_frame, new HomeFragment()).commit();
}
private void showMenu() {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.hide(RestarauntFragment.this);
transaction.replace(android.R.id.widget_frame, new MenuFragment()).commit();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
getActivity().setTitle("Applebees");
populateReviewList();
}
private void populateReviewList() {
Review.add(new ReviewAdapter("Okay", 1, "Levi", "This is an example")
}
private class ReviewList extends ArrayAdapter<ReviewAdapter> {
public ReviewList() {
super(getActivity(), R.layout.review_list_item, Review);
}
public View getView(LayoutInflater inflater, int position, View convertView, ViewGroup parent) {
View ReviewView = convertView;
if (ReviewView == null) {
ReviewView = inflater.inflate(R.layout.review_list_item, parent, false);
}
//Find view
ReviewAdapter currentReview = Review.get(position);
TextView title = (TextView) ReviewView.findViewById(R.id.review_list_title);
RatingBar rating = (RatingBar) ReviewView.findViewById(R.id.review_rating);
TextView user = (TextView) ReviewView.findViewById(R.id.review_user);
TextView review = (TextView) ReviewView.findViewById(R.id.review_content);
title.setText(currentReview.getTitle());
rating.setRating(currentReview.getRating());
user.setText(currentReview.getUser());
review.setText(currentReview.getReview());
rating.setEnabled(false);
return ReviewView;
}
}
}
没有错误,但是当我启动时,我得到了这个:
08-29 23:15:20.876 27575-27575/com.teampainite.you_r_next E/ArrayAdapter﹕ You must supply a resource ID for a TextView
08-29 23:15:20.876 27575-27575/com.teampainite.you_r_next D/AndroidRuntime﹕ Shutting down VM
08-29 23:15:20.876 27575-27575/com.teampainite.you_r_next W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41615930)
08-29 23:15:21.507 27575-27575/com.teampainite.you_r_next E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2159)
at android.widget.ListView.onMeasure(ListView.java:1130)
at android.view.View.measure(View.java:15513)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:645)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:425)
at android.view.View.measure(View.java:15513)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1217)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.widget.ScrollView.onMeasure(ScrollView.java:321)
at android.view.View.measure(View.java:15513)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:645)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:425)
at android.view.View.measure(View.java:15513)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15513)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:762)
at android.view.View.measure(View.java:15513)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15513)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15513)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
at android.view.View.measure(View.java:15513)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5191)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2159)
at android.widget.ListView.onMeasure(ListView.java:1130)
at android.view.View.measure(View.java:15513)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:645)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:425)
at android.view.View.measure(View.java:15513)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1217)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.widget.ScrollView.onMeasure(ScrollView.java:321)
at android.view.View.measure(View.java:15513)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:645)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:425)
at android.view.View.measure(View.java:15513)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15513)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:762)
at android.view.View.measure(View.java:15513)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15513)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15513)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
at android.view.View.measure(View.java:15513)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5191)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
没有其他问题可以解决这个问题,我已经找到了。
请检查Android ListView in fragment example 和Create listview in fragment android 例。
如果你用列表视图编写&#34;片段,有很多例子&#34;在Google上
检查一下: https://www.google.co.in/?gfe_rd=cr&ei=uU0BVJzYIsHN8geZ8IBI&gws_rd=ssl#q=fragment+with+list+views
答案 1 :(得分:0)
从logcat中可以清楚地看到你在Adapter 的getView()方法中犯了错误(即ReviewList)你提到了 3 TextView 在getView中...正确检查你的布局文件其中一个视图必须是 RelativeLayout ...这就是为什么你得到 ClassCastException 强>