2012-12-18 28 views
0

我創建了一個列表,每行都有幾個信息,並且還有一個圖像。我的問題是,當我去看下面的列表項目時,圖像必須被加載並且它被堆疊直到它們被加載。我正在使用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); 

} 
    } 

BaseAdapter:

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(); 

} 

} 

ItemView控件:

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; 

} 

} 
+0

ArrayAdapter是一個BaseAdapter這需要一個T []作爲dataset.your代碼看起來不錯,我 – Blackbelt

回答

0

你需要異步加載圖像,可以考慮在一個線程池的環境中使用的AsyncTask;您不應該在UI線程中處理加載圖像,也請記住,如果您沒有正確處理大圖片,可能會發生OOM異常。

看一看在這裏,它會引導你,你應該做的: http://android-developers.blogspot.dk/2010/07/multithreading-for-performance.html

在他的AsyncTask類,他在處理下載的照片,你應該替換一段代碼從加載圖片你的SD卡

0

我發現了問題! 事情是,我是sql問題,並在視圖中從url下載圖像,每次我在列表中下載它必須加載,所以它堆棧,直到它加載。現在我首先加載片段中的所有信息,並將其傳遞給適配器,現在速度更快(它已經不完美了,但它進展順利)。 我嘗試更改爲ArrayAdapter,在我的案例中我沒有看到任何區別。我必須嘗試與cursoradapter,看看是否可以做得更好。 感謝所有的幫助!