My app keeps crashing whenever my setadapter function gets called and I have no idea why, can anyone help? The emulator that I am running just says that the app has stopped unexpectedly.
This is my Main
public class MainActivity extends Activity {
private ListView listView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Ride ride_data[] = new Ride[]
{
new Ride("LA Trip", "la man"),
new Ride("SD Trip", "sd man"),
new Ride("SF Trip", "sf man")
};
RideAdapter adapter = new RideAdapter(this,
R.layout.listview_item_row,ride_data);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
}
}
and this is my Adapter
public class RideAdapter extends ArrayAdapter
{
Context context;
int layoutResourceId;
Ride data[] = null;
public RideAdapter(Context context, int layoutResourceId, Ride[] data)
{
super(context,layoutResourceId,data);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
RideHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RideHolder();
holder.rideName = (TextView)row.findViewById(R.id.txtRideName);
holder.personName = (TextView)row.findViewById(R.id.txtPersonName);
row.setTag(holder);
}
else
{
holder = (RideHolder)row.getTag();
}
Ride ride = data[position];
holder.rideName.setText(ride.rideName);
holder.personName.setText(ride.personName);
return row;
}
public static class RideHolder
{
TextView rideName;
TextView personName;
}
}
this is my ride class
public class Ride {
public String rideName;
public String personName;
public Ride()
{
}
public Ride(String rideName, String personName)
{
super();
this.rideName=rideName;
this.personName=personName;
}
}
and finally this is my list item view
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp"
>
android:id="@+id/txtRideName"
android:layout_width ="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
/>
android:id="@+id/txtPersonName"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
/>
any help would be greatly appreciated!
.
stackoverflow.comm
No comments:
Post a Comment