0

我创建了一个扩展 BaseAdapter 的类。我没有任何 custom.xml 来膨胀它。很简单,我写了这段代码

 public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();//Getting Error here to create a method getLayoutInflater
        View row;
        row = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
        TextView title;
        title = (TextView) row.findViewById(android.R.id.text1);
        title.setText(Title[position]);
        return (row);
    }

在 getLayoutInflater() 上,它向我显示创建方法的错误。如果我在主要活动中创建此类,那么它可以正常工作。所以我必须做些什么来消除这个错误

为了创建ListView,我将适配器称为

myAdapter = new dataListAdapter(this,passengerList);
listView.setAdapter(myAdapter);

对于ListView项目的更新,我将适配器称为

 update.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {

                if(isSuccess){
                    dialog.cancel();
                    passengerList[position] = newString;
                    listView.setAdapter(new dataListAdapter(this,passengerList));
                }
            }
        });

我的 BaseAdapter 类

class PassengerListView extends BaseAdapter {
    String[] Title;
    Activity activity;

    public dataListAdapter(MainActivity mainActivity, String[] text) {
        Title = text;
        activity = mainActivity;
    }

    public String[] getAllValues() {
        return Title;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return Title.length;
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = activity.getLayoutInflater();//Getting Error here to create a method getLayoutInflater
        View row = null;
        if(convertView == null) {
            row = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
        }
        TextView title;
        title = (TextView) row.findViewById(android.R.id.text1);
        title.setText(Title[position]);
        return (row);
    }

所以在更新代码上它向我显示了这个错误:

构造函数 PassengerListView(new View.OnClickListener(){}, String[]) 未定义

4

4 回答 4

2

它是一个活动类的方法。

http://developer.android.com/reference/android/app/Activity.html#getLayoutInflater()

您需要将活动上下文传递给 asynctask 的构造函数并使用相同的

     new CustomAdapter(ActivityName.this); // pass the activity context

然后在你的适配器中

     LayoutInflater mInflater; 
     Context mContext;
     public CustomAdapter(Context context)
     {
        // use the activity context now
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     } 

在`getView

     row = mInflater.inflate(android.R.layout.customlayouttoinflate, parent, false);

您还需要使用视图支架。

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

要更新,您需要更新填充您的列表视图的列表并调用notidyDataSetChanged您的适配器。

编辑:

从我的评论

您正在listView.setAdapter(new dataListAdapter(this,passengerList))按钮单击侦听器中进行操作,并且使用this的是ActivityName.this.

我也不知道你为什么有

 new dataListAdapter(this,passengerList);

应该

 new PassengerListView (ActivityName.this,passengerList);

将您的构造函数更改为

public PassengerListView(MainActivity mainActivity, String[] text) {
    Title = text;
    activity = mainActivity;
}
于 2013-08-22T05:42:53.223 回答
1

BaseAdapter不是Activity因此,它不能得到 的方法Activity,你需要通过Context,在这种情况下是你Activity这样做的:

LayoutInflater inflater = your_activity.getLayoutInflater();//Getting Error here to create a method getLayoutInflater

希望这可以帮助

于 2013-08-22T05:42:42.353 回答
0

Raghunandan 给出的答案似乎是正确的,但只有你不需要每次都充气。你的代码可能是这样的

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = mContext.getLayoutInflater();//mContext being the private variable intialized by constructor.
    View row = convertView;
    if(convertView == null) {
        row = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
    }
    TextView title;
    title = (TextView) row.findViewById(android.R.id.text1);
    title.setText(Title[position]);
    return (row);
}
于 2013-08-22T05:54:52.720 回答
0

试试这个,我认为它对你有用....

     LayoutInflater mInflater;
     PassengerListView(Activity activity,String[] data)
     {
         this.activity=activity;
         this.data=data;
         mInflater = (LayoutInflater)activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);    
     }

以上是您的自定义适配器类的构造函数,该类具有两个参数,可根据您的要求进行更改...

下面是创建视图并返回它的代码...

public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null) {
            convertView = inflater.inflate(android.R.layout.simple_list_item_1, false);
        }
        TextView title;
        title = (TextView) row.findViewById(android.R.id.text1);
        title.setText(Title[position]);
        convertView.setTag(title);
        return convertView;
    }   
于 2013-08-22T06:21:47.907 回答