SherlockFragment中带有自定义适配器的ListView,构造函数未定义(无法通过此过程)
作为一名新的java/android程序员,我请求您的帮助。 我试图通过我的自定义适配器传递“this”,以便在SherlockFragment中设置ListView。不幸的是,在onCreateView中编译之前,在未定义的构造函数中设置适配器(new LeatherAdapter(this))时,我遇到了一个错误。有人能一步一步地解释一下解决我问题的方法吗SherlockFragment中带有自定义适配器的ListView,构造函数未定义(无法通过此过程),listview,android-listview,android-fragments,actionbarsherlock,android-custom-view,Listview,Android Listview,Android Fragments,Actionbarsherlock,Android Custom View,作为一名新的java/android程序员,我请求您的帮助。 我试图通过我的自定义适配器传递“this”,以便在SherlockFragment中设置ListView。不幸的是,在onCreateView中编译之前,在未定义的构造函数中设置适配器(new LeatherAdapter(this))时,我遇到了一个错误。有人能一步一步地解释一下解决我问题的方法吗 import java.util.ArrayList; import android.content.Context; import
import java.util.ArrayList;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragment;
public class LeatherTab extends SherlockFragment {
private ListView leather_listview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.leather_fragment, container, false); //Fragment Layout inflated
leather_listview = (ListView) view.findViewById(R.id.leather_list); // List is initialized
leather_listview.setAdapter(new LeatherAdapter(this)); //Custom list adapter passes Context
return view;
}
}
class SingleRow {
String title;
String description;
int image;
SingleRow(String title, String description, int image) {
this.title=title;
this.description=description;
this.image=image;
}
}
class LeatherAdapter extends BaseAdapter {
ArrayList<SingleRow> list;
Context context;
public LeatherAdapter(Context c) {
context = c;
list = new ArrayList<SingleRow>();
Resources res = c.getResources();
String[] titles = res.getStringArray(R.array.leather_list_titles);
String[] descriptions = res.getStringArray(R.array.leather_list_description);
int[] images = {R.drawable.belt, R.drawable.wallet, R.drawable.coincase};
for (int i=0;i<3;i++)
{
new SingleRow(titles[i], descriptions[i], images[i]);
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row, viewGroup, false);
TextView title = (TextView) row.findViewById(R.id.leather_title);
TextView description = (TextView) row.findViewById(R.id.leather_description);
ImageView image = (ImageView) row.findViewById(R.id.leather_icon);
SingleRow temp = list.get(position);
title.setText(temp.title);
description.setText(temp.description);
image.setImageResource(temp.image);
return row;//returns the rootView of single_row.xml
}
}
import java.util.ArrayList;
导入android.content.Context;
导入android.content.res.Resources;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.ImageView;
导入android.widget.ListAdapter;
导入android.widget.ListView;
导入android.widget.TextView;
导入com.actionbarsherlock.app.SherlockFragment;
公共类LeatherTab扩展了SherlockFragment{
私有ListView\u ListView;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图=充气器。充气(R.layout.leather_碎片,容器,false);//碎片布局充气
leather_listview=(listview)view.findViewById(R.id.leather_list);//列表已初始化
leather_listview.setAdapter(新LeatherAdapter(this));//自定义列表适配器传递上下文
返回视图;
}
}
类单列{
字符串标题;
字符串描述;
int图像;
单行(字符串标题、字符串描述、整型图像){
这个.title=title;
这个。描述=描述;
这个。图像=图像;
}
}
类LeatherAdapter扩展了BaseAdapter{
数组列表;
语境;
公共皮革适配器(上下文c){
上下文=c;
列表=新的ArrayList();
Resources res=c.getResources();
String[]titles=res.getStringArray(R.array.leather\u list\u titles);
String[]descriptions=res.getStringArray(R.array.leather\u list\u description);
int[]images={R.drawable.belt,R.drawable.wallet,R.drawable.coincase};
对于(int i=0;i改变此值
leather_listview.setAdapter(new LeatherAdapter(this));
到
公共最终活动getActivity()
返回此片段当前关联的活动。
leather_listview.setAdapter(new LeatherAdapter(getActivity());