我建立短信应用程序,它列出了联系人PIC,姓名,邮件,对话的日期。 我使用的是自定义适配器列出them.Below是code
公共类ConvRowAdapter扩展ArrayAdapter< ConvItem> {
私人语境my_context;
//浏览查找高速缓存
私有静态类ViewHolder {
TextView的名称,身体,日期;
ImageView的IMG;
}
公共ConvRowAdapter(上下文的背景下,ArrayList的< ConvItem> ConvItems){
超(背景下,R.layout.convitem,ConvItems);
my_context =背景;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
//获取数据项这个职位
ConvItem ConvItem =的getItem(位置);
//检查现有的观点正在被重复使用,否则膨胀的看法
ViewHolder viewHolder; //视图查找高速缓存存储在标签
如果(convertView == NULL){
viewHolder =新ViewHolder();
LayoutInflater充气= LayoutInflater.from(的getContext());
convertView = inflater.inflate(R.layout.convitem,NULL);
viewHolder.img =(ImageView的)convertView.findViewById(R.id.iv_photo);
viewHolder.name =(TextView中)convertView.findViewById(R.id.tv_name);
viewHolder.date =(TextView中)convertView.findViewById(R.id.tv_date);
viewHolder.body =(TextView中)convertView.findViewById(R.id.tv_body);
convertView.setTag(viewHolder);
} 其他 {
viewHolder =(ViewHolder)convertView.getTag();
}
//使用数据对象填充数据到模板视图
如果(ConvItem.getPhotoUri()!= NULL)
{
位图bitmap=BitmapFactory.de$c$cStream(my_context.getContentResolver().openInputStream(ConvItem.getPhotoUri()));
viewHolder.img.setImageBitmap(位图); //图像被设定
}
其他
{
点阵位图= BitmapFactory.de codeResource(my_context.getResources(),R.drawable.contact_blue);
viewHolder.img.setImageBitmap(位);
}
如果(ConvItem.getDisplayName()== NULL)
viewHolder.name.setText(ConvItem.getAddress()); //电话号码设定
其他
viewHolder.name.setText(ConvItem.getDisplayName()); //如果电话号码的联系人显示联系人姓名存在
viewHolder.date.setText(ConvItem.getDate()); //设置日期
viewHolder.body.setText(ConvItem.getBody()); //设置消息的主体
//返回完成视图呈现在屏幕上
返回convertView;
}
}
这需要8-9秒的显示列表。 有没有办法来提高速度? 如果是的话,请张贴这样做的正确方法。
在此先感谢:)
编辑:
要检查是否延迟加载是通过图像我尝试删除所有照片设置报表的加载造成跑it.Still加载需要同时进行。 通过使用simplecursoradapter我可以显示人体,日期和消息中没有delay.But显示照片与他们我尝试使用自定义适配器。 这是一个返回所有短信到arraylist.Please功能检查的code效率:
公开的ArrayList< ConvItem> getSMS(){
ArrayList的< ConvItem> ConvItems =新的ArrayList< ConvItem>();
(?内容://彩信,短信/通话简单=真正的),乌里uriSMSURI = Uri.parse;
光标CUR = getActivity()。getContentResolver()查询(uriSMSURI,NULL,NULL,NULL,
日期降序);
cur.moveToFirst();
而(!cur.isAfterLast()){
ConvItem ConvItem =新ConvItem();
字符串的地址=空,身体= NULL,DNAME = NULL,RES = NULL,id_dummy = NULL;
长日期= NULL;
乌里uriphotoid = NULL;
//获取从规范-地址的地址
RES = cur.getString(cur.getColumnIndex(recipient_ids));
乌里广告= Uri.parse(内容:// MMS,SMS /规范-地址/);
光标curad = getActivity()getContentResolver()查询(广告中,NULL,NULL,NULL,NULL);
curad.moveToFirst();
而(!curad.isAfterLast())
{
id_dummy = curad.getString(curad.getColumnIndexOrThrow(_ ID));
如果(id_dummy.equals(RES))
地址= curad.getString(curad.getColumnIndexOrThrow(地址));
curad.moveToNext();
}
curad.close();
身体= cur.getString(cur.getColumnIndexOrThrow(片段));
日期= cur.getLong(cur.getColumnIndexOrThrow(日));
日期datenew =新的日期(日期);
。字符串FORMATTED_DATE =新的SimpleDateFormat(+DD / MM / YYYY)格式(datenew);
的thread_id = cur.getString(cur.getColumnIndexOrThrow(_ ID));
龙的ContactID = fetchContactIdFromPhoneNumber(地址);
// uriphotoid = getPhotoUri(的ContactID);
DNAME = getcontactname(地址);
ConvItem.setDisplayName(DNAME);
ConvItem.setThreadId(thread_id单);
ConvItem.setAddress(地址);
//ConvItem.setPhotoUri(uriphotoid);
ConvItem.setDate(FORMATTED_DATE);
ConvItem.setBody(体);
ConvItems.add(ConvItem);
cur.moveToNext();
}
cur.close();
返回ConvItems;
}
![vb中如何在listview和textbox里,使不断新添加的数据处于能看见的状态](https://p.xsw88.cn/allimgs/daicuo/20230907/6538.png)
这个功能对我的主要活动类onCreate方法运行。
我用loader.callbacks纠正延迟问题。
公共类InboxLoaderFragment扩展片段实现LoaderCallbacks<光标> {
私有静态最终诠释LOADER_ID = 1; //确定哪些装载机
LoaderManager流明;
SimpleCursorAdapter mAdapter;
ListView的LV;
私人LoaderManager.LoaderCallbacks<光标> mCallbacks;
公共InboxLoaderFragment(){}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
查看rootview = inflater.inflate(R.layout.inboxloaderfragment,集装箱,假);
LV =(ListView控件)rootview.findViewById(R.id.list);
//创建一个空的适配器,我们将用它来显示加载的数据。
的String [] uiBindFrom = {recipient_ids,recipient_ids,段};
INT [] uiBindTo = {R.id.iv_photo,R.id.tv_name,R.id.tv_body};
mAdapter =新SimpleCursorAdapter(getActivity(),R.layout.convitem,空,uiBindFrom,uiBindTo,0);
mAdapter.setViewBinder(新SimpleCursorAdapter.ViewBinder(){
@覆盖
公共布尔setViewValue(查看视图,光标指针,整数参数:columnIndex){
开关(view.getId())
{
案例R.id.iv_photo:
字符串资源= NULL,地址= NULL;
//获取从规范-地址的地址
RES = cursor.getString(cursor.getColumnIndex(recipient_ids));
地址= getadd(RES);
龙的ContactID = fetchContactIdFromPhoneNumber(地址);
乌里uriphotoid = getPhotoUri(的ContactID);
setcontactimage(uriphotoid,视图);
//设置照片使用URI
返回true;
案例R.id.tv_name:
字符串RES1 = NULL,地址1 = NULL;
RES1 = cursor.getString(cursor.getColumnIndex(recipient_ids));
地址1 = getadd(RES1); //获取从规范-地址的地址
字符串DNAME = getcontactname(地址1);
如果(DNAME!= NULL)//接触退出
((TextView中)查看).setText(DNAME); //接触存在
其他
((TextView中)查看).setText(地址1); //设置显示名称
返回true;
案例R.id.tv_body:
绳体= cursor.getString(cursor.getColumnIndexOrThrow(片段));
((TextView中)查看).setText(身体); //设置邮件正文
返回true;
默认:
返回false;
}
}
});
lv.setAdapter(mAdapter);
mCallbacks =这一点;
LM = getLoaderManager();
//启动加载器
lm.initLoader(LOADER_ID,空,mCallbacks);
返回rootview;
}
@覆盖
公共android.support.v4.content.Loader<光标> onCreateLoader(
INT为arg0,捆绑ARG1){
(?内容://彩信,短信/通话简单=真正的)开放的基本URI = Uri.parse;
返回新CursorLoader(getActivity(),基本URI,
NULL,NULL,NULL,日期DESC);
}
@覆盖
公共无效onLoadFinished(
android.support.v4.content.Loader<光标>为arg0,光标ARG1){
开关(arg0.getId()){
案例LOADER_ID:
mAdapter.swapCursor(ARG1);
打破;
}
//列表视图现在显示查询的数据
}
@覆盖
公共无效onLoaderReset(android.support.v4.content.Loader<光标>为arg0){
mAdapter.swapCursor(空);
}
}///主要活动
应用程序加载速度非常快now.But滚动laggy.How实现自定义simplecursoradapter或CursorAdapter的内部viewholder设计模式?
解决方案有没有办法来提高速度?
将您的照片加载到后台线程,可能通过使用图像管理库如毕加索。
除此之外,使用Traceview和其他工具,以确定具体在哪里,你的问题所在。
I am building an sms app which lists the contact pic,name,message,date of a conversation. I'm using a custom adapter to list them.Below is the code
public class ConvRowAdapter extends ArrayAdapter<ConvItem> {
private Context my_context;
// View lookup cache
private static class ViewHolder {
TextView name,body,date;
ImageView img;
}
public ConvRowAdapter(Context context, ArrayList<ConvItem> ConvItems) {
super(context, R.layout.convitem, ConvItems);
my_context=context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
ConvItem ConvItem = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.convitem, null);
viewHolder.img = (ImageView) convertView.findViewById(R.id.iv_photo);
viewHolder.name = (TextView) convertView.findViewById(R.id.tv_name);
viewHolder.date = (TextView) convertView.findViewById(R.id.tv_date);
viewHolder.body = (TextView) convertView.findViewById(R.id.tv_body);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// Populate the data into the template view using the data object
if(ConvItem.getPhotoUri()!=null)
{
Bitmap bitmap=BitmapFactory.decodeStream(my_context.getContentResolver().openInputStream(ConvItem.getPhotoUri()));
viewHolder.img.setImageBitmap(bitmap);//Image is set
}
else
{
Bitmap bitmap=BitmapFactory.decodeResource(my_context.getResources(),R.drawable.contact_blue);
viewHolder.img.setImageBitmap(bitmap);
}
if(ConvItem.getDisplayName()==null)
viewHolder.name.setText(ConvItem.getAddress());//Phone number set
else
viewHolder.name.setText(ConvItem.getDisplayName());//If phone number exists in contacts display contact name
viewHolder.date.setText(ConvItem.getDate());//set date
viewHolder.body.setText(ConvItem.getBody());//setting the body of the message
// Return the completed view to render on screen
return convertView;
}
}
It takes 8-9 seconds to display the list. Is there a way to improve the speed? If so please post the correct way to do so.
Thanks in advance :)
Edited:
To check if the delay in loading is caused by the loading of images i tried deleting all photo setting statements and ran it.Still the loading takes the same time. By using a simplecursoradapter i could display the body,date and message in no delay.But to display the photo with them i tried using the custom adapter. This is the function which returns all sms into an arraylist.Please check the efficiency of the code:
public ArrayList<ConvItem> getSMS(){
ArrayList<ConvItem> ConvItems = new ArrayList<ConvItem>();
Uri uriSMSURI = Uri.parse("content://mms-sms/conversations?simple=true");
Cursor cur = getActivity().getContentResolver().query(uriSMSURI, null, null, null,
"date desc");
cur.moveToFirst();
while (!cur.isAfterLast()) {
ConvItem ConvItem = new ConvItem();
String address = null, body = null, dname = null,res=null,id_dummy=null;
Long date=null;
Uri uriphotoid=null;
//to obtain address from canonical-addresses
res = cur.getString(cur.getColumnIndex("recipient_ids"));
Uri ad =Uri.parse("content://mms-sms/canonical-addresses/");
Cursor curad=getActivity().getContentResolver().query(ad, null,null, null, null);
curad.moveToFirst();
while(!curad.isAfterLast())
{
id_dummy=curad.getString(curad.getColumnIndexOrThrow("_id"));
if(id_dummy.equals(res))
address=curad.getString(curad.getColumnIndexOrThrow("address"));
curad.moveToNext();
}
curad.close();
body = cur.getString(cur.getColumnIndexOrThrow("snippet"));
date = cur.getLong(cur.getColumnIndexOrThrow("date"));
Date datenew=new Date(date);
String formatted_date=new SimpleDateFormat(" "+"dd/MM/yyyy").format(datenew);
thread_id = cur.getString(cur.getColumnIndexOrThrow("_id"));
Long ContactID = fetchContactIdFromPhoneNumber(address);
//uriphotoid = getPhotoUri(ContactID);
dname = getcontactname(address);
ConvItem.setDisplayName(dname);
ConvItem.setThreadId(thread_id);
ConvItem.setAddress(address);
//ConvItem.setPhotoUri(uriphotoid);
ConvItem.setDate(formatted_date);
ConvItem.setBody(body);
ConvItems.add(ConvItem);
cur.moveToNext();
}
cur.close();
return ConvItems;
}
This function is running on my onCreate method in the main activity class.
I used loader.callbacks to correct the delay issue.
public class InboxLoaderFragment extends Fragment implements LoaderCallbacks<Cursor>{
private static final int LOADER_ID = 1;//identify which loader
LoaderManager lm;
SimpleCursorAdapter mAdapter;
ListView lv;
private LoaderManager.LoaderCallbacks<Cursor> mCallbacks;
public InboxLoaderFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview=inflater.inflate(R.layout.inboxloaderfragment,container,false);
lv=(ListView)rootview.findViewById(R.id.list);
// Create an empty adapter we will use to display the loaded data.
String[] uiBindFrom = {"recipient_ids","recipient_ids","snippet"};
int[] uiBindTo = {R.id.iv_photo,R.id.tv_name,R.id.tv_body};
mAdapter = new SimpleCursorAdapter(getActivity(),R.layout.convitem,null,uiBindFrom,uiBindTo, 0);
mAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
switch(view.getId())
{
case R.id.iv_photo:
String res=null,address=null;
//to obtain address from canonical-addresses
res = cursor.getString(cursor.getColumnIndex("recipient_ids"));
address=getadd(res);
Long ContactID = fetchContactIdFromPhoneNumber(address);
Uri uriphotoid = getPhotoUri(ContactID);
setcontactimage(uriphotoid,view);
//set photo using uri
return true;
case R.id.tv_name:
String res1=null,address1=null;
res1 = cursor.getString(cursor.getColumnIndex("recipient_ids"));
address1=getadd(res1);//to obtain address from canonical-addresses
String dname = getcontactname(address1);
if(dname!=null)//contact exits
((TextView)view).setText(dname);//contact exists
else
((TextView)view).setText(address1);//set display name
return true;
case R.id.tv_body:
String body = cursor.getString(cursor.getColumnIndexOrThrow("snippet"));
((TextView)view).setText(body);//set message body
return true;
default:
return false;
}
}
});
lv.setAdapter(mAdapter);
mCallbacks=this;
lm = getLoaderManager();
//Initiating the loader
lm.initLoader(LOADER_ID, null,mCallbacks);
return rootview;
}
@Override
public android.support.v4.content.Loader<Cursor> onCreateLoader(
int arg0, Bundle arg1) {
Uri baseUri = Uri.parse("content://mms-sms/conversations?simple=true");
return new CursorLoader(getActivity(), baseUri,
null, null, null,"date desc");
}
@Override
public void onLoadFinished(
android.support.v4.content.Loader<Cursor> arg0, Cursor arg1) {
switch (arg0.getId()) {
case LOADER_ID:
mAdapter.swapCursor(arg1);
break;
}
// The listview now displays the queried data
}
@Override
public void onLoaderReset(android.support.v4.content.Loader<Cursor> arg0) {
mAdapter.swapCursor(null);
}
}///main activity
App loads very fast now.But scrolling is laggy.How to implement viewholder design pattern inside a custom simplecursoradapter or a cursoradapter?
解决方案Is there a way to improve the speed?
Move your photo loading to a background thread, perhaps by using an image management library like Picasso.
Beyond that, use Traceview and other tools to determine specifically where your problems lie.
相关推荐
最新文章