-1

大家好,我有问题没有调用getView()。请。在项目中我正在使用 ViewPager。所以也许是因为这个

这是我的 MainActivity

package com.uinleader.animewatcher;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {
    private List<View> pages;
    private View recent;
    private View top_seven;

    private ViewPager mPager;
    private final ArrayList<Parsed> info = new ArrayList<Parsed>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initUi();
        LVInit();

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(android.R.menu.main, menu);
        return true;
    }

    private void initUi() {
        LayoutInflater inflater = LayoutInflater.from(this);
        pages = new ArrayList<View>();

        recent = inflater.inflate(R.layout.recent, null);
        recent.setTag(getString(R.string.recent));

        top_seven = inflater.inflate(R.layout.top_seven, null);
        top_seven.setTag(getString(R.string.top));

        pages.add(recent);
        pages.add(top_seven);

        PAdapter adapter = new PAdapter(pages);
        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(adapter);
        mPager.setCurrentItem(0);


    }
    private void LVInit() {
        LayoutInflater inflater = LayoutInflater.from(this);
        Parsed one = new Parsed("One", "1", R.drawable.ic_launcher);
        Parsed two = new Parsed("Two", "2", R.drawable.ic_launcher);
        Parsed three = new Parsed("Three", "3", R.drawable.ic_launcher);

        View v = inflater.inflate(R.layout.top_seven, null);

        info.add(one);
        info.add(two);
        info.add(three);
        ListView lv = (ListView) v.findViewById(R.id.listView1);
        MArrayAdapter radapter = new MArrayAdapter(MainActivity.this, R.id.listView1, info);
        lv.setAdapter(radapter);
    }
}

这是适配器

package com.uinleader.animewatcher;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by uinleader on 28.09.13.
 */
public class MArrayAdapter extends ArrayAdapter<Parsed> {

    private final Activity activity;
    private final ArrayList<Parsed> items;
    public MArrayAdapter(Activity a, int textViewResourceId, ArrayList<Parsed> items) {
        super(a, textViewResourceId, items);
        activity = a;
        this.items = items;
        Log.e("ListViewDebug", "Inside Adapter");
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.e("ListViewDebug", "Start Function");
        View view = convertView;
        ViewHolder holder;
        if (view == null) {
            Log.e("ListViewDebug", "In First IF");
            LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.row_layout, parent, false);
            holder = new ViewHolder();


 holder.title = (TextView) view.findViewById(R.id.anime_title);
        holder.ep_num = (TextView) view.findViewById(R.id.ep_number);
        holder.ep_preview = (ImageView) view.findViewById(R.id.ep_preview);
        view.setTag(holder);
    }else {
        holder = (ViewHolder) view.getTag();
    }

    Parsed item = items.get(position);
    if(item!=null) {
        holder.title.setText(item.anime_title);
        holder.ep_num.setText("Episode: "+item.ep_num);
        holder.ep_preview.setImageResource(item.img);
    }

    return view;
}

@Override
public int getCount() {
    return items.size();
}

private static class ViewHolder {
    public TextView title;
    public TextView ep_num;
    public ImageView ep_preview;
}

}

这是我的数据课。和 XML 的

package com.uinleader.animewatcher;

/**
 * Created by uinleader on 28.09.13.
 */
public class Parsed {
    public String anime_title;
    public String ep_num;
    public int img;

    public Parsed (String anime_title, String ep_num, int img ){
        this.anime_title = anime_title;
        this.ep_num = ep_num;
        this.img = img;
    }
}

用于列表的 XML。

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

行的 XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    >




        <ImageView
            android:layout_width="200px"
            android:layout_height="200px"
            android:id="@+id/ep_preview"
            android:layout_column="0"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/anime_title"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/ep_preview" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ep_number"
        android:layout_below="@+id/anime_title"
        android:layout_toRightOf="@+id/ep_preview" />


</RelativeLayout>
4

2 回答 2

0

setContentView(R.layout.activity_main); 已经“膨胀”了活动正在使用的当前布局..我不知道为什么你在“init”方法中膨胀其他东西,这可能就是为什么没有调用任何东西,因为活动仍然显示在 R.layout 上设置的视图.activity_main。

于 2013-09-28T23:44:16.423 回答
0

ArrayAdapter的工作方式与BaseAdapter有点不同- 这是你正在实现你的 getView

我建议几个选项来修复:

  1. 改变你ArrayAdapterBaseAdapter

  2. 或在构造函数中使用 ArrayList 适配器附带的内置 ArrayList 编写以下内容:

    public MArrayAdapter(Activity a, int textViewResourceId, ArrayList<Parsed> items) {
        super(a, textViewResourceId, items); //<== call to super instantiates the items
        activity = a;
        //ArrayListAdapter has a built in arrayList - use it
        Log.e("ListViewDebug", "Inside Adapter");
    }
    

将您的 getView 更改为:

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.e("ListViewDebug", "Start Function");
        View view = convertView; // **note why do you allocate again? just write convertview everywhere  instead of view**
        ViewHolder holder;
        if (view == null) {
            Log.e("ListViewDebug", "In First IF");
                      //**I recommend instantiating the inflater once inside the constructor**
            LayoutInflater inflater =   
                     (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.row_layout, parent, false);
            holder = new ViewHolder();


        holder.title = (TextView) view.findViewById(R.id.anime_title);
        holder.ep_num = (TextView) view.findViewById(R.id.ep_number);
        holder.ep_preview = (ImageView) view.findViewById(R.id.ep_preview);
        view.setTag(holder);
    }else {
        holder = (ViewHolder) view.getTag();
    }

    Parsed item = (Parsed)getItem(position); //<==== this
    if(item!=null) {
        holder.title.setText(item.anime_title);
        holder.ep_num.setText("Episode: "+item.ep_num);
        holder.ep_preview.setImageResource(item.img);
    }

    return 

view;
}
于 2013-09-29T00:07:29.273 回答