gpt4 book ai didi

android - 无法解析方法 getLayoutInflater

转载 作者:行者123 更新时间:2023-11-29 14:18:20 26 4
gpt4 key购买 nike

我想创建一个带有 2 个 TextView 的简单 ListView。原始类:https://github.com/thest1/LazyList/blob/master/src/com/fedorvlasov/lazylist/LazyAdapter.java

但我有一个大问题:我在 Fragment 上工作,而不是在 Activity 上工作。所以我已经调整了代码。

现在我在 ListViewAdapter 类中有一个错误(它不是我的主要类):

LayoutInflater inflater =  context.getLayoutInflater();

“无法解析方法‘getLayoutInflater()’

import android.app.Activity;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.pack.pack.R;

public class ListViewAdapter extends BaseAdapter
{
Fragment context;
String title[];
String description[];

public ListViewAdapter(Fragment context, String[] title, String[] description) {
super();
this.context = context;
this.title = title;
this.description = description;
}

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

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

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

private class ViewHolder {
TextView txtViewTitle;
TextView txtViewDescription;
}

public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();

if (convertView == null)
{
convertView = inflater.inflate(R.layout.simplerow, null);
holder = new ViewHolder();
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
holder.txtViewDescription = (TextView) convertView.findViewById(R.id.textView2);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}

holder.txtViewTitle.setText(title[position]);
holder.txtViewDescription.setText(description[position]);

return convertView;
}

}

我读到它在 BaseAdapter 中不起作用。我们怎样才能做到?谢谢。

编辑:我的 listView 类

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class commu extends Fragment {

public static final String TAG = "commu";

ListView lview;
ListViewAdapter lviewAdapter;

private final static String month[] = {"January","February","March","April","May",
"June","July","August","September","October","November","December"};

private final static String number[] = {"Month - 1", "Month - 2","Month - 3",
"Month - 4","Month - 5","Month - 6",
"Month - 7","Month - 8","Month - 9",
"Month - 10","Month - 11","Month - 12"};


@Nullable
//@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


View rootView = inflater.inflate(R.layout.commu, container, false);

lview = (ListView) rootView.findViewById(R.id.listView);
lviewAdapter = new ListViewAdapter(this, month, number);

//System.out.println("adapter => "+lviewAdapter.getCount());

lview.setAdapter(lviewAdapter);

//lview.setOnItemClickListener(this);


return rootView;
}

/*public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
// TODO Auto-generated method stub
//Toast.makeText(this,"Title => "+month[position]+"=> n Description"+number[position], Toast.LENGTH_SHORT).show();
}*/

@Override
public void onResume()
{
super.onResume();
//wv6.onResume();
}
@Override
public void onPause()
{
super.onPause();
//wv6.onPause();
}


}

最佳答案

你可以使用

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

关于android - 无法解析方法 getLayoutInflater,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31301470/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com