如何在使用BaseAdapter的ListView中添加标题?除了下面所使用的方法之外,还有没有其他方法。
我尝试过使用addHeaderView(header)
函数,但仍然无法成功。
需要一些帮助。谢谢!以下是代码:
public class ViewInterview extends Activity {
InterviewList interviewList = null;
TextView header;
ListView list = null;
static ArrayList<String> titlelist;
static ArrayList<String> markingslist;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_interview);
list = (ListView) findViewById(R.id.list_interview);
String TAG = "INTERVIEW LIST";
header = (TextView) findViewById(R.id.header);
header.setText("Test");
titlelist = new ArrayList<String>();
markingslist = new ArrayList<String>();
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader xreader = parser.getXMLReader();
InputSource is = new InputSource(getResources().openRawResource(R.raw.booklist));
InterviewHandler myInterviewHandler = new InterviewHandler();
xreader.setContentHandler(myInterviewHandler);
xreader.parse(new InputSource(is.getByteStream()));
} catch(Exception e) {
Log.i(TAG, "InterviewList parse exception " + e.getMessage());
}
//---get result from InterviewHandler tempInterviewList
interviewList = InterviewHandler.tempInterviewList;
String strMarkings = interviewList.getMarkings().get(0);
String[] arrMarkings = strMarkings.split("\\|\\|");
for(int i=0; i<arrMarkings.length; i++) {
titlelist.add("Title " + interviewList.getTitle().get(0));
markingslist.add(arrMarkings[i]);
}
//list.addHeaderView(header);
//setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, interviewList.interview));
list.setAdapter(new InterviewAdapter(this,titlelist,markingslist));
}
public static class InterviewAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<String> dataTitle;
private List<String> dataMarkings;
public InterviewAdapter(Context context, List<String> dataTitle, List<String> dataMarkings) {
mInflater = LayoutInflater.from(context);
dataTitle = this.dataTitle;
dataMarkings = this.dataMarkings;
}
public int getCount() {
return titlelist.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = mInflater.inflate(R.layout.layout_interview, null);
holder = new ViewHolder();
holder.title = (TextView)convertView.findViewById(R.id.title);
holder.markings = (TextView)convertView.findViewById(R.id.markings);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.title.setText(titlelist.get(position));
holder.markings.setText(markingslist.get(position));
return convertView;
}
static class ViewHolder {
TextView title;
TextView markings;
}
}
}