アプリで SherlockFragment アクションバーを使用し、アクティビティの 1 つでカスタム ListView を作成しました。コンテキストが原因で機能しないようで、回避方法がわかりません。
Custom 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;
}
}