在我的应用程序中,我在我的数据库中存储了8个静态dns字段。这只是一个名称值对。然后在按钮上单击我将存储在数据库中的名称和IP地址输入到arraylist并使用基本适配器在listview中显示它们。一切正常,但按下按钮后几乎需要4-5秒才能显示列表视图。 这个时间是正常还是应该少?此外,我还在代码的其他部分添加该列表中的项目,因此每次我必须再次从数据库获取列表时按钮单击。 如果这个时间更多,如果不是我可以做些什么来缩短这个时间?
这是在按钮点击
上运行的代码public void LoadPrimaryDnsList(){
dnsName=dataBaseHelper.getDnsName();
ipAddress=dataBaseHelper.getIpAddress();
id=dataBaseHelper.getId();
dnsListAdapter = new DnsListAdapter(getActivity(), dnsName, ipAddress,id);
dnsListAdapter.notifyDataSetChanged();
android.support.v7.app.AlertDialog.Builder builder;
final android.support.v7.app.AlertDialog alertDialog;
LayoutInflater inflater = getActivity().getLayoutInflater();
View layout = inflater.inflate(R.layout.browse_dns_dialog, (ViewGroup) getActivity().findViewById(R.id.lin_dialog));
listView = (ListView) layout.findViewById(R.id.dns_list);
builder = new android.support.v7.app.AlertDialog.Builder(getActivity());
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
listView.setAdapter(dnsListAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String ip = ipAddress.get(position);
EDITDNS1.setText(ip);
alertDialog.dismiss();
}
});
}
这是适配器类
public class DnsListAdapter extends BaseAdapter {
Context context;
LayoutInflater layoutInflater;
ArrayList<String> dnslist=new ArrayList<>();
ArrayList<String> iplist=new ArrayList<>();
ArrayList<String> ids=new ArrayList<>();
public DnsListAdapter(Context context, ArrayList<String> dns, ArrayList<String> ip,ArrayList<String> id) {
this.context = context;
dnslist=dns;
iplist=ip;
ids=id;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return dnslist.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.dialog_row, parent, false);
holder.tv_name = (TextView) convertView.findViewById(R.id.dnsName);
holder.tv_ip= (TextView) convertView.findViewById(R.id.ip_address);
holder.iv_cross= (ImageView) convertView.findViewById(R.id.iv_cross);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv_name.setText(dnslist.get(position));
if (holder.tv_name.getText().toString().equalsIgnoreCase("Google 1")||holder.tv_name.getText().toString().equalsIgnoreCase("Google 2")||holder.tv_name.getText().toString().equalsIgnoreCase("Level3 1")
||holder.tv_name.getText().toString().equalsIgnoreCase("Level3 2")||holder.tv_name.getText().toString().equalsIgnoreCase("DNS Watch 1")||holder.tv_name.getText().toString().equalsIgnoreCase("DNS Watch 2")
||holder.tv_name.getText().toString().equalsIgnoreCase("OpenDNS 1")||holder.tv_name.getText().toString().equalsIgnoreCase("OpenDNS 2"))
{
holder.iv_cross.setVisibility(View.INVISIBLE);
}
holder.tv_ip.setText(iplist.get(position));
Fonts.setHelveticaFont(getActivity(), holder.tv_name);
Fonts.setHelveticaFont(getActivity(), holder.tv_ip);
holder.iv_cross.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dataBaseHelper.deleteRow(id.get(position));
dnsName.remove(position);
ipAddress.remove(position);
id.remove(position);
dnsListAdapter.notifyDataSetChanged();
}
});
return convertView;
}
}
答案 0 :(得分:1)
我建议您加载数据并使用ListView
异步填充AsyncTask
。
答案 1 :(得分:0)
如果你想要速度,你应该预加载所有的东西。
@Override
public void onCreate(Bundle s) {
dnsName=dataBaseHelper.getDnsName();
ipAddress=dataBaseHelper.getIpAddress();
id=dataBaseHelper.getId();
dnsListAdapter = new DnsListAdapter(getActivity(), dnsName, ipAddress,id);
dnsListAdapter.notifyDataSetChanged();
android.support.v7.app.AlertDialog.Builder builder;
final android.support.v7.app.AlertDialog alertDialog;
LayoutInflater inflater = getActivity().getLayoutInflater();
View layout = inflater.inflate(R.layout.browse_dns_dialog, (ViewGroup) getActivity().findViewById(R.id.lin_dialog));
listView = (ListView) layout.findViewById(R.id.dns_list);
builder = new android.support.v7.app.AlertDialog.Builder(getActivity());
builder.setView(layout);
alertDialog = builder.create();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String ip = ipAddress.get(position);
EDITDNS1.setText(ip);
alertDialog.dismiss();
}
});
}
public void LoadPrimaryDnsList(){
alertDialog.show();
listView.setAdapter(dnsListAdapter);
}
除此之外,如果您想了解代码的速度,您应该开始添加一些时间日志http://developer.android.com/reference/android/util/TimingLogger.html
为了使您的代码更具可读性(并且更易于扩展/增强),我将开始了解Single Responsibility Principle并将其应用于您的方法。