我在我的应用程序中使用 SherlockFragment 操作栏,并且在其中一个活动中我创建了一个自定义 ListView。由于上下文,它似乎不起作用,我不知道如何解决它。
这是自定义 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;
}
}