나는 채우기하고 ListView
데이터베이스에서 커서 결과 세트를 수신하는 사용자 정의 커서 어댑터와 함께. 20 개의 뷰만 표시하는 데 최대 10-12 초가 걸릴 수 있습니다!
여기입니다 CursorAdater의 커서가의 일련의 문자열과 기본 64 이미지 표현을 공급 클래스 텍스트 뷰 와 이미지 뷰 각 뷰는 :
public void bindView(View v, Context context, Cursor c) {
String diveSite = c.getString(c.getColumnIndexOrThrow(diveDataBase.KEY_DIVESITE));
String date = c.getString(c.getColumnIndexOrThrow(diveDataBase.KEY__DIVEDATE));
String diveNumber= c.getString(c.getColumnIndexOrThrow(diveDataBase.KEY__DIVENUMBER));
String diveImagePath = c.getString(c.getColumnIndex(diveDataBase.KEY_DIVEPICTURE));
String rating = c.getString(c.getColumnIndexOrThrow(diveDataBase.KEY_DIVERATING));
c.moveToLast();
noOfRows = Integer.parseInt(c.getString(c.getColumnIndex(diveDataBase.KEY__DIVENUMBER)));
/**
* Next set the dive site name
*/
TextView title_text = (TextView) v.findViewById(R.id.tv_DiveSiteListView);
if (title_text!= null) {
title_text.setText(diveSite);
}
/**
* Set Date of dive in smaller textView font
*/
TextView date_text = (TextView) v.findViewById(R.id.tv_diveDateList);
if (date_text!= null) {
date_text.setText(date);
}
/**
* Display the dive number in larger red font
*/
TextView dive_no = (TextView) v.findViewById(R.id.tv_diveNumberListView);
if (diveNumber!= null ) {
dive_no.setText(diveNumber+"/"+noOfRows);//+1 as rows start at zero
}
/*
* Display the rating of the dive
*/
RatingBar bar = (RatingBar) v.findViewById(R.id.ratingBarListView);
bar.setNumStars(5);
bar.setRating( Float.parseFloat(rating));
/**
* Display the image only of image not null
* First get image from Strimg pathname as a file, then convert to Bitmap and resize
*
*/
ImageView displayImage = (ImageView) v.findViewById(R.id.iv_list_image);
//set image here once taken form external string path, and resized bitmap conversion
imagePathFile = new File(diveImagePath);
try {
FileInputStream streamIn = new FileInputStream(imagePathFile);
Bitmap bitmap = BitmapFactory.decodeStream(streamIn);//retrun null if can't convert
if(bitmap!=null){
displayImage.setBackground(null);
resizedImage = reSizeImage(bitmap);
displayImage.setImageBitmap(resizedImage);
}else{
displayImage.setBackgroundResource(R.drawable.logdive3);
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}//end try catch
}//end newView
그리고 List-view 클래스에서 커서는 비동기 클래스 의 do-in-background 메소드에서 얻어지며 어댑터는 on-post-execute에 설정됩니다.
@Override
protected Cursor doInBackground(Void... params) {
// get the cursor from database
ViewListOfDives.data = new diveDataBase(ViewListOfDives.this);
ViewListOfDives.data.open();
// get cursor object holding all data, use a asynch inner class to load
cursor = data.getCursorData();
//ViewListOfDives.data.close();
return cursor;
}
//set the adapter to list view
@Override
protected void onPostExecute(Cursor cursor) {
//check if data available
if(cursor!=null && cursor.getCount()>0){
// get customised array adoater list
adapter = new ItemAdapter(ViewListOfDives.this, cursor);
}else{
//display o dives in data base message and finish this activity
displayDialog();
}
ViewListOfDives.this.setListAdapter(adapter);
ViewListOfDives.data.close();
super.onPostExecute(cursor);
// dispose dialog
if(pd.isShowing()){
pd.dismiss();
}
}
온라인 할당량을 조사해 왔으므로 성능 최적화 커서 어댑터에 대해 많은 것을 찾을 수 없으므로 모든 입력을 많이 주시면 감사하겠습니다 !!
편집 : 이미지 크기를 조정하는 데 사용하는 방법 포함 :
public Bitmap reSizeImage(Bitmap bitmapImage) {
// resize bitmap image passed and rerun new one
Bitmap resizedImage = null;
float factorH = h / (float) bitmapImage.getHeight();
float factorW = w / (float) bitmapImage.getWidth();
float factorToUse = (factorH> factorW)? factorW : factorH;
try {
resizedImage = Bitmap.createScaledBitmap(bitmapImage,
(int) (bitmapImage.getWidth() * factorToUse),
(int) (bitmapImage.getHeight() * factorToUse), false);
} catch (IllegalArgumentException e) {
Log.d(TAG, "Problem resizing Image @Line 510+");
e.printStackTrace();
}
Log.d(TAG,
"in resixed, value of resized image: "
+ resizedImage.toString());
return resizedImage;
}// end reSize
여기서 h와 w :
// for image resizing
static int w = 250;
static int h = 280;
편집 : 기본 64에서 비트 맵으로의 변환을 처리하기 위해 비동기 클래스를 작성한 다음 실행 후 이미지보기가 비트 맵으로 설정됩니다. 이 내부 클래스는 cursoradpter 클래스의 bindView 메서드에서 호출됩니다. 문제는 이제 마지막보기 만 이미지로 채워집니다 !!
//asynch class to load nase 64 image and convert to bitmap and set imageview
private class getBitmapImage extends AsyncTask<String, Void, Bitmap>{
@Override
protected Bitmap doInBackground(String... imagePath) {
// get image path and decode to bitmap
String diveImagePath = imagePath[0];
//String diveImagePath = c.getString(c.getColumnIndex(diveDataBase.KEY_DIVEPICTURE));
File imagePathFile = new File(diveImagePath);
try {
FileInputStream streamIn = new FileInputStream(imagePathFile);
bitmap = BitmapFactory.decodeStream(streamIn);//retrun null if cant convert
}catch (FileNotFoundException e) {
e.printStackTrace();
//Toast.makeText(context, "No Image Found!! Usimng default", Toast.LENGTH_LONG).show();
}//end try catch
return bitmap;
}//end do in background
@Override
protected void onPostExecute(Bitmap bitmap) {
if(bitmap!=null){
displayImage.setBackground(null);
resizedImage = reSizeImage(bitmap);
displayImage.setImageBitmap(resizedImage);
}else{
//Toast.makeText(context, "No Image Found!! Usimng default", Toast.LENGTH_LONG).show();
displayImage.setBackgroundResource(R.drawable.logdive3);
}
}//end onPOstExecute
}//end getBitmap asynch
이 내부 클래스는 CursorAdpter 클래스의 bindView 메서드에서 호출됩니다.
//get bitmap image from base 64 string, and set to image view using asynch class thread
new getBitmapImage().execute(diveImagePath);
먼저 화면에 텍스트를 넣으십시오. 로드 중임을 보여주는 기본 드로어 블일 수 있습니다.
파일에서 20 개의 비트 맵을로드하는 데는 최소한 몇 초가 걸립니다. 비동기 적으로로드해야합니다 (목록보기를 채우는 스레드에서). UI 스레드 만 imageview를 설정할 수 있지만 대부분의 작업 인 작업자 스레드에서 비트 맵을로드 할 수 있습니다.
또한 표시 할 수있는 것보다 높은 해상도로로드하지 않도록 샘플링을 사용하여 비트 맵을로드해야합니다. 그렇지 않으면 필요한 것보다 더 많은 데이터를 읽게됩니다.
큰 비트 맵을 효율적으로로드하는 방법 은 Android 튜토리얼 을 참조하십시오 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다