gpt4 book ai didi

android - 从数据库中获取列表并在 Listview 中显示需要很长时间

转载 作者:搜寻专家 更新时间:2023-10-30 23:41:37 24 4
gpt4 key购买 nike

在我的应用程序中,我在我的数据库中存储了 8 个静态 dns 字段。这只是一个名称值对。单击按钮时,我将存储在数据库中的名称和 IP 地址获取到 arraylist 中,并使用基本适配器在 ListView 中显示它们。一切正常,但按下按钮后几乎需要 4-5 秒才能显示 listview 。这个时间是正常的还是应该更少??此外,我还从代码的其他部分在该列表中添加项目,因此每次我必须再次从数据库获取列表时单击按钮。如果这个时间更多,如果不是,我可以做些什么来减少这个时间??

这是点击按钮时运行的代码

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;
}

}

最佳答案

我建议您加载数据并使用 AsyncTask 异步填充 ListView

关于android - 从数据库中获取列表并在 Listview 中显示需要很长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34431665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com