各行にいくつかの情報と画像を含むリストを作成しました。私の問題は、下にあるリスト項目を見に行くと、画像をロードする必要があり、ロードされるまでスタックすることです。BaseAdapter を使用してリストを作成しています。積み重ねずにリストを作成する他の方法はありますか? たとえば、ArrayAdapter または CursorAdapter を使用しようとすると、読み込みが速くなりますか? それとも、適切に機能させるために何か他のものを変更する必要がありますか? 私はコードを入れます...それが役立つことを願っています!
リストフラグメント
public class SearchCarListActivity extends ListFragment {
private Button profileBtn;
private Button toggleMapListBtn;
private Button filterBtn;
private SearchCarListAdapter adapter;
private Location loc;
private SearchActivity parent;
private ArrayList<Car> cars;
private Location location;
private SearchFragmentActivity sfa;
private ArrayList<String> distance;
public void setCars(ArrayList<Car> cars){
this.cars = cars;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
SearchFragmentActivity afa = (SearchFragmentActivity)getActivity();
afa.setCar((Car)adapter.getItem(position));
Car c = (Car)adapter.getItem(position);
afa.changeFragment(new SearchCarListExpandable());
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.list_search_cars, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
setUpViews();
parent = (SearchActivity)getActivity().getParent();
LocationManager lm = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE); // Faster, no GPS fix.
criteria.setAccuracy(Criteria.ACCURACY_FINE);
loc = lm.getLastKnownLocation(lm.getBestProvider(criteria, true));
sfa = (SearchFragmentActivity) getActivity();
cars = sfa.getCars();
removeDeleted();
setUpDistances();
adapter = new SearchCarListAdapter(this.getActivity(), cars, distance);
adapter.setCurrentLocation(loc);
setListAdapter(adapter);
}
private void removeDeleted(){
ArrayList<Car> c = new ArrayList<Car>();
for (Car car:cars){
if (car.getPricePerHour()!=0 || car.getPricePerKm()!=0){
c.add(car);
}
}
cars = c;
}
private void setUpDistances() {
// TODO Auto-generated method stub
distance = new ArrayList<String>();
Location l = sfa.getMyLocation();
String distan;
for (Car car:cars){
GeoPoint gp=car.getLocation();
if (car.getLatitude()==0.0){
distan = "Unknown";
}else{
float[] f = new float[3];
Location.distanceBetween(l.getLatitude(), l.getLongitude(), (gp.getLatitudeE6()/1E6), (gp.getLongitudeE6()/1E6), f);
Double dist = Double.valueOf(f[0]/1000);
Short s = dist.shortValue();
distan = (s.toString()+"Km");
}
distance.add(distan);
}
}
private void setUpViews() {
toggleMapListBtn = (Button)getView().findViewById(R.id.toggleMapListBtnInListView);
toggleMapListBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SearchCarListActivity.this.getActivity().finish();
}
});
filterBtn = (Button)getView().findViewById(R.id.filterListSearchBtn);
filterBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getActivity().getParent(), FilterSearchActivity.class);
TabGroupActivity parentActivity = (TabGroupActivity)getActivity().getParent();
parentActivity.startChildActivity("FilterSearchActivity", i);
}
});
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
adapter = new SearchCarListAdapter(this.getActivity(), cars, distance);
adapter.setCurrentLocation(loc);
setListAdapter(adapter);
}
}
ベースアダプター:
public class SearchCarListAdapter extends BaseAdapter {
private ArrayList<Car> cars;
private Context context;
private Location currentLocation;
private ArrayList<String> distances;
public SearchCarListAdapter(Context c, ArrayList<Car> cars, ArrayList<String> distances){
super();
this.context = c;
this.cars = cars;
this.distances = distances;
}
public int getCount() {
return cars.size();
}
public Object getItem(int position) {
return (null == cars) ? null : cars.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
SearchCarItemView clv;
if(null == convertView){
clv = (SearchCarItemView)View.inflate(context, R.layout.search_car_list_item, null);
} else {
clv = (SearchCarItemView)convertView;
}
clv.setLocation(this.currentLocation);
clv.setDistance(distances.get(position));
clv.setCar(cars.get(position));
return clv;
}
public Location getCurrentLocation() {
return currentLocation;
}
public void setCurrentLocation(Location currentLocation) {
this.currentLocation = currentLocation;
}
public String getNumberplate(int position) {
Car c = cars.get(position);
return c.getLicensePlate();
}
}
アイテム ビュー:
public class SearchCarItemView extends LinearLayout {
private TextView name;
private ImageView iv;
private TextView distance;
private Car car;
private Context context;
private Location currentLocation;
private String distan;
private TextView addr;
private List<Address> address = new ArrayList<Address>();
private TextView priceKm;
private TextView priceH;
private TextView reviews;
private int reviCount;
public SearchCarItemView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
// TODO Auto-generated constructor stub
}
@Override
protected void onFinishInflate(){
super.onFinishInflate();
name = (TextView)findViewById(com.tapazz.R.id.txtCarListItemName);
addr = (TextView)findViewById(R.id.txtCarListItemAddress);
iv = (ImageView)findViewById(com.tapazz.R.id.carListItemSmallImage);
distance = (TextView)findViewById(com.tapazz.R.id.txtCarListItemDistance);
priceKm = (TextView)findViewById(R.id.txtCarListItemPricePerKm);
priceH = (TextView)findViewById(R.id.txtCarListItemPricePerHour);
reviews = (TextView)findViewById(R.id.txtCarListItemReviews);
}
public Car getCar() {
return car;
}
public void setCar(final Car car){
iv.setImageBitmap(null);
this.car = car;
name.setText(car.getName());
getAddress();
if(address.size()>0){
String displayAddress = "";
for(int i =0; i<address.get(0).getMaxAddressLineIndex(); i++){
displayAddress += address.get(0).getAddressLine(i);
}
addr.setText(displayAddress);
}
Log.d("equals?", ((Boolean)car.getPhotoUrl().equals("")).toString());
if(car.getPhotoUrl()!=null && !car.getPhotoUrl().equals("")){
String thumbUrl = "http://tapazz.com/autopia/upload/thumbnail.php?image=cars/"+car.getPhotoUrl()+"&maxWidth=150&maxHeight=150";
String urlString = "http://tapazz.com/autopia/upload/thumbnail.php?image=cars/"+car.getPhotoUrl()+"&maxWidth=150&maxHeight=150";
DrawableManager.fetchDrawableOnThread(urlString, iv, null);
}else{
iv.setImageResource(R.drawable.take_photo_normal);
}
distance.setText(getDistance(car));
priceKm.setText(String.format("€%.2f/km",car.getPricePerKm()));
priceH.setText(String.format("€%.2f/H",car.getPricePerHour()));
Thread thread = new Thread(){
public void run(){
Connection conn = Connect.getConnection();
try {
Statement stmt = conn.createStatement();
String sql = "SELECT COUNT(Remarks) FROM BOOKING WHERE Car= "+car.getId()+" AND Remarks IS NOT NULL AND Remarks <> ''";
Log.d("SQL Review:", sql);
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()){
reviCount = rs.getInt("COUNT(Remarks)");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
}
}
};
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
reviews.setText("Reviews: "+reviCount);
}
public void getAddress(){
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
try {
address = geocoder.getFromLocation(car.getLatitude(), car.getLongitude(), 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setDistance (String distan){
this.distan = distan;
}
private String getDistance(Car car) {
String dis = "Distance: "+distan;
//get distance
return dis;
}
public void setLocation(Location currentLocation) {
this.currentLocation =currentLocation;
}
}