我目前正在开发一款与股票短信应用非常相似的短信应用。
但问题是我的应用程序加载速度比库存(内置)应用程序慢得多。
这是我的应用程序代码: -
public class ThreadData extends ListActivity
{
private static final Uri SMS_URI = Uri.parse("content://sms");
ArrayList<String> bodies = new ArrayList<String>();
MySMSAdapter adapter;
String name;
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.threadlist);
Bundle extras = getIntent().getExtras();
if(extras != null)
{
name = extras.getString("address");
}
Cursor cursor = getContentResolver().query(SMS_URI, null, null, null, "date ASC");
startManagingCursor(cursor);
while (cursor.moveToNext())
{
String Id = cursor.getString(cursor.getColumnIndex("_id"));
String address = cursor.getString(cursor.getColumnIndex("address"));
String body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
String personName = getPersonName(Id, address);
if(personName.equals(name))
{
bodies.add(body);
}
}
//adapter = new MySMSAdapter(this, bodies);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.sms_inbox, R.id.row, bodies);
ListView listView = getListView();
listView.setAdapter(adapter);
}
public String getPersonName(String id, String address)
{
if(id==null)
{
if(address != null)
{
return PhoneNumberUtils.formatNumber(address);
}
else
{
return null;
}
}
Cursor c = this.getContentResolver().query(Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, address),
new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);
if(c != null)
{
try
{
if(c.getCount() > 0)
{
c.moveToFirst();
return c.getString(0);
}
}
finally
{
c.close();
}
}
if(address != null)
{
return PhoneNumberUtils.formatNumber(address);
}
return null;
}
}
我的自定义适配器: -
public class MySMSAdapter extends BaseAdapter
{
private Activity activity;
private ArrayList<String> data;
LayoutInflater inflater;
public MySMSAdapter(Activity a, ArrayList<String> arrayList)
{
activity = a;
data = arrayList;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount()
{
return data.size(); //To change body of implemented methods use File | Settings | File Templates.
}
public Object getItem(int i)
{
return data.get(i); //To change body of implemented methods use File | Settings | File Templates.
}
public long getItemId(int i)
{
return i;
}
public static class ViewHolder
{
public TextView textView;
}
public View getView(final int i, View view, ViewGroup viewGroup)
{
View row = view;
final ViewHolder holder;
if(view==null)
{
row = inflater.inflate(R.layout.sms_inbox, viewGroup, false);
holder = new ViewHolder();
holder.textView = (TextView)row.findViewById(R.id.row);
row.setTag(holder);
}
else
{
holder = (ViewHolder)row.getTag();
}
holder.textView.setText(data.get(i));
return row;
}
}
在我的课程中,我尝试使用两个选项(自定义适配器以及ArrayAdapter)加载List但是在这两种情况下我的应用程序加载速度非常慢。如何加载它更快?我可能更喜欢我的自定义适配器的解决方案,因为将来我可能需要在我的布局中添加一些东西,比如照片和其他东西。
感谢。请帮助!!
答案 0 :(得分:3)
您可以使用http://developer.android.com/guide/developing/tools/traceview.html分析器来本地化性能问题。我的猜测问题是你正在同步进行磁盘操作(Cursor cursor = getContentResolver()。query(...))。尝试使用AssyncTask进行此同步,即。