在setAdapter上,它不会调用Adapter

时间:2015-09-07 19:57:17

标签: java android android-layout listview android-fragments

问题是即使我在listView上执行setAdapter也不会调用getView方法。请参阅下面的可能代码。我没有收到任何错误消息。它只是不显示评论者数据。请你建议我做错了什么。

MovieReviewerFragmemt类我的代码如下:

    @Override
    protected void onPostExecute (ArrayList movieDetailsArray){
        getMovieArrayList(movieDetailsArray);
        ListView listView = (ListView)                                    rootView.findViewById(R.id.movie_detail_reviewerlist);
        MovieAddlDetail detail = (MovieAddlDetail) movieDetailsArray.get(0);
        MovieReviewers[] list = detail.getReviewerArrayList();
        Log.v(LOG_TAG, "MovieReviewerFragment - CustomReviewerAdapter - calling adapter");
        movieDetailAdapter = new CustomReviewerAdapter(getActivity(), list);
        movieDetailAdapter.notifyDataSetChanged();
        listView.setAdapter(movieDetailAdapter);
    }

CustomerReviewAdapter:

public class CustomReviewerAdapter extends ArrayAdapter {
private Context context;
private LayoutInflater inflater;
private final String LOG_TAG1 = CustomReviewerAdapter.class.getSimpleName();
private MovieReviewers[] reviewers;

public CustomReviewerAdapter(Context context, MovieReviewers[] reviewers) {
    super(context, R.layout.fragment_detail, reviewers);
    Log.v(LOG_TAG1, "CustomReviewerAdapter - constructor");
    this.context = context;
    this.reviewers = reviewers;

    inflater = LayoutInflater.from(context);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Log.v(LOG_TAG1, "CustomReviewerAdapter - getView");
    if (convertView == null) {
        inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(R.layout.fragment_detail_reviewer, parent, false);
        Log.v(LOG_TAG1, "CustomReviewerAdapter - getView - convertview was null");

    }
    TextView textView1 = (TextView) convertView.findViewById(R.id.movie_detail_reviewerlist_author);
    TextView textView2 = (TextView) convertView.findViewById(R.id.movie_detail_reviewerlist_content);
    textView1.setText(reviewers[position].getAuthor());
    textView2.setText(reviewers[position].getContent());
    Log.v(LOG_TAG1, "CustomReviewerAdapter - getView - data set");
    return convertView;
}

}

Fragment Detail Reviewer:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id ="@+id/imageView_layout"
android:orientation = "vertical">

<TextView
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:id="@+id/movie_detail_reviewerlist_author"
    android:textAlignment="center"
    android:textStyle="bold"
    android:gravity="center_vertical"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:descendantFocusability="blocksDescendants"/>

<TextView
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:id="@+id/movie_detail_reviewerlist_content"
    android:textAlignment="center"
    android:textStyle="bold"
    android:gravity="center_vertical"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:descendantFocusability="blocksDescendants"/>

片段细节:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/movie_detail_list"
    android:editable="false" />

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/movie_detail_reviewerlist"
    android:editable="false" />

以下是启动片段的活动。第一个片段工作正常,正在显示。然而第二个片段是我没有显示任何东西的地方。是否正确启动两个片段?

详情活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);
    Bundle extras = getIntent().getExtras();
    intentData = extras.getStringArray("moviedetails");
    MovieDetailFragment movieDetailFragment = new MovieDetailFragment();
    movieDetailFragment.setMovieDetails(intentData);
    MovieReviewerFragment movieReviewerFragment = new MovieReviewerFragment();
    movieReviewerFragment.setMovieDetails(intentData);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container_1, movieDetailFragment)
                .add(R.id.container_2, movieReviewerFragment)
                .commit();
    }
}

2 个答案:

答案 0 :(得分:1)

你必须像这样调用LayoutInflater:

inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

答案 1 :(得分:0)

你必须切换线

movieDetailAdapter.notifyDataSetChanged();
listView.setAdapter(movieDetailAdapter);

listView.setAdapter(movieDetailAdapter);
movieDetailAdapter.notifyDataSetChanged();

首先设置适配器,然后发送通知。如果以相反的方式执行此操作,则没有ListView可能会显示更新的数据。

您可以使用

自动执行这些通知
movieDetailAdapter.setNotifyOnChange(true)