不调用getView

时间:2013-09-28 23:11:39

标签: java android xml listview adapter

大家好我有问题,没有调用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 for list。

<?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>

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

ArrayAdapter 的工作方式与 BaseAdapter 略有不同 - 这是您实现getView的原因

我建议修改几个选项:

  1. 将您的ArrayAdapter更改为BaseAdapter

  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");
    }
    
  3. 将你的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;
    }