我创建了自己的自定义适配器以支持多种类型的using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinearSearch
{
class Program
{
static void Main(string[] args)
{
int var1 = 50;
int[] arr;
arr = new int[10]{10,20,30,40,50,60,70,80,90,100};
int retval = linearsearch(arr,var1);
if (retval >= 1)
{
Console.WriteLine(retval);
Console.Read();
}
else
{ Console.WriteLine("Not found"); Console.Read(); }
}
static int linearsearch(int[] arr, int var1)
{
int pos = 0;
int posfound = 0;
foreach (var item in arr)
{
pos = pos + 1;
if (item == var1)
{
posfound = pos;
if (posfound >= 1)
break;
}
}
return posfound;
}
}
}
项,但我不断获得空指针异常。我没有使用ListView
的东西吗?有人有任何见解吗?
这是我的适配器类:
convertView
以下是错误日志:
package com.lucasmikesoftware.messaround;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class MyAdapter extends ArrayAdapter<AdapterData> {
private int layoutId;
public MyAdapter(Context context, ArrayList<AdapterData> data, int layoutId) {
super(context, R.layout.events_list_item, data);
this.layoutId = layoutId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(getItem(position) instanceof ImageAdapterData){
return setImageListItem(position,parent);
} else if (getItem(position) instanceof VideoAdapterData){
return setVideoListItem(position,parent);
} else if (getItem(position) instanceof EventAdapterData){
return setEventListItem(position,parent);
} else{
return setNormalListItem(position,parent);
}
}
public View setNormalListItem(int position,ViewGroup parent){
LayoutInflater theInflater = LayoutInflater.from(getContext());
View theView = theInflater.inflate(layoutId,parent,false);
AdapterData adapterData = getItem(position);
TextView theTextView = (TextView) theView.findViewById(R.id.normal_list_item_title);
theTextView.setText(adapterData.getTitle());
TextView theFullTextTextView = (TextView) theView.findViewById(R.id.normal_list_item_full_text);
theFullTextTextView.setText(adapterData.getFullText());
return theView;
}
public View setImageListItem(int position,ViewGroup parent){
LayoutInflater theInflater = LayoutInflater.from(getContext());
View theView = theInflater.inflate(layoutId,parent,false);
ImageAdapterData adapterData = (ImageAdapterData) getItem(position);
TextView theTextView = (TextView) theView.findViewById(R.id.image_list_item_title);
theTextView.setText(adapterData.getTitle());
TextView theFullTextTextView = (TextView) theView.findViewById(R.id.image_list_item_full_text);
theFullTextTextView.setText(adapterData.getFullText());
ImageView theImageView = (ImageView) theView.findViewById(R.id.image_list_item_image);
Picasso.with(getContext()).load(adapterData.getImageUrl()).into(theImageView);
return theView;
}
public View setEventListItem(int position,ViewGroup parent){
LayoutInflater theInflater = LayoutInflater.from(getContext());
View theView = theInflater.inflate(layoutId,parent,false);
EventAdapterData adapterData = (EventAdapterData) getItem(position);
TextView theTextView = (TextView) theView.findViewById(R.id.text_view);
theTextView.setText(adapterData.getTitle());
ImageView theImageView = (ImageView) theView.findViewById(R.id.event_image_view);
Picasso.with(getContext()).load(adapterData.getImageUrl()).into(theImageView);
return theView;
}
public View setVideoListItem(int position,ViewGroup parent){
//TODO Finish this damn thing
return parent;
}
}