我在我的应用程序中使用SherlockFragment Actionbars,并且在我创建自定义ListView的其中一个活动中。它似乎因为Context而无法工作,我不知道如何绕过它。
以下是我自定义ListView的代码:
package net.agnesium.lommebudsjett.view;
import java.util.List;
import com.actionbarsherlock.app.SherlockFragment;
import net.agnesium.lommebudsjett.R;
import net.agnesium.lommebudsjett.view.InnkjopActivity.ListViewItem;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class ListeKontroll extends BaseAdapter
{
LayoutInflater inflater;
List<ListViewItem> items;
public ListeKontroll(Context context, List<ListViewItem> items) {
super();
this.items = items;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ListViewItem item = items.get(position);
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.liste_rad, null);
TextView imgThumbnail = (TextView) vi.findViewById(R.id.imgThumbnail);
TextView txtTitle = (TextView) vi.findViewById(R.id.txtTitle);
TextView txtSubTitle = (TextView) vi.findViewById(R.id.txtSubTitle);
imgThumbnail.setText(item.navn);
txtTitle.setText(item.pris);
txtSubTitle.setText(item.kategori);
return vi;
}
}
列出ListView的类:
package net.agnesium.lommebudsjett.view;
import java.util.ArrayList;
import java.util.List;
import net.agnesium.lommebudsjett.R;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.actionbarsherlock.app.SherlockFragment;
public class InnkjopActivity extends SherlockFragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View V = inflater.inflate(R.layout.activity_innkjop, container, false);
super.onCreate(savedInstanceState);
ListView kvittering = (ListView) V.findViewById(R.id.lstKvitt);
List<ListViewItem> items = new ArrayList<ListViewItem>();
items.add(new ListViewItem()
{{
navn = "Produkt";
pris = 100;
kategori = "Kategori";
}});
ListeKontroll adapter = new ListeKontroll(this.getSherlockActivity(), items);
kvittering.setAdapter(adapter);
return V;
}
class ListViewItem{
public int pris;
public String navn;
public String kategori;
}
}
答案 0 :(得分:0)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.activity_innkjop, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
ListView kvittering = (ListView) getView().findViewById(R.id.lstKvitt);
List<ListViewItem> items = new ArrayList<ListViewItem>();
items.add(new ListViewItem()
{{
navn = "Produkt";
pris = 100;
kategori = "Kategori";
}});
ListeKontroll adapter = new ListeKontroll(getActivity(), items);
kvittering.setAdapter(adapter);
super.onActivityCreated(savedInstanceState);
}