布局inflater的无法访问语句

时间:2013-12-10 05:14:11

标签: android layout-inflater

我正在尝试使用 layoutInflater 将我的布局转换为java对象,但是当我尝试获取对LayoutInflater类的引用时,我收到错误“语句无法访问”

这是我的代码

package test.app;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

/**
 * Created by HADDAD on 12/8/13.
 */
public class TonyAdapter extends ArrayAdapter<String> {
            Context context;
    public TonyAdapter(Context c, String[] titles) {
        super(c, R.layout.sinlge_row,R.id.textView , titles);
           this.context=c;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return super.getView(position, convertView, parent);
        LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }
}

2 个答案:

答案 0 :(得分:3)

这是因为你在第一行返回。将您的代码更改为:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
    // ... more code
    return view;
}

答案 1 :(得分:3)

return之后您无法拥有代码。将其更改为:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(/** your layout.. */);
    return v;
}

或者只是通过super调用它:

public View getView(int position, View convertView, ViewGroup parent) {
    View v = super.getView(position, convertView, parent);
    // do something with v
    return v;
}

取决于您的使用案例。