列表视图访问数据库时无法平滑滚动

时间:2019-02-09 14:18:17

标签: android android-layout android-listview android-sqlite

我的android应用程序的搜索活动使用列表视图显示搜索结果,并且列表视图的每一项都是从资产文件夹中存储的SQLite数据库获得的。问题是列表视图滚动不流畅。请帮助我。

我的代码:

DatabaseOpenHelper类:

import android.content.Context;

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

public class DatabaseOpenHelper extends SQLiteAssetHelper {
    private static final String dbname="minip5.db";
    private static final int dbversion=1;

    public DatabaseOpenHelper(Context context){
        super(context,dbname,null,dbversion);
    }
}

DatabaseAccess类:

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;

public class DatabaseAccess {
    private SQLiteOpenHelper openHelper;
    private SQLiteDatabase db;
    private static DatabaseAccess instance;
    public Cursor c;
    //private constructor so that object creation from outside the class 
    is avoided
    private DatabaseAccess(Context context){
        this.openHelper=new DatabaseOpenHelper(context);
    }
    //to return single instance of database
    public static DatabaseAccess  getInstance(Context context){
        if (instance==null)
            instance=new DatabaseAccess(context);
        return instance;
    }

    //to open database
    public void open(){
        this.db=openHelper.getWritableDatabase();
    }
    public void close(){
        if (db!=null)
        this.db.close();
    }
    public ArrayList<searchadapter> st(){
        ArrayList<searchadapter> sea=new ArrayList<>();
        if(c.moveToFirst()){
            do {
                searchadapter a=new searchadapter(c.getString(0));
                sea.add(a);
            }while (c.moveToNext());
        }
        return sea;
    }
    public void run2(String name){
        c=db.rawQuery("select land.State, Avg_Land_Rate, 
        Annual_Avg_Temp_inC, No_of_Companies, annual_avg_rain_inCm, 
        No_of_Schools, No_of_Malls, No_of_Colleges, No_of_Pubs, 
        Literacy_rate_inPercent, Rank_in_Crime_Rate from land, company, 
        education, people, recreation, weather where 
        upper(land.state)='"+name+"' and land.State=company.State and " 
        +"land.State_code=education.State_code and 
        land.State_code=people.State_code 
        land.State_short_code=recreation.State_short_code "+"and 
        land.State_short_code=weather.State_short_code",new String[]{});
    }

searchadapter类:

public class searchadapter {
    private String st;
    public searchadapter(String st){
        this.st=st;
    }
    public String getSt(){
        return st;
    }
    public void setSt(String st){
        this.st=st;
    }
}

searchad类:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class searchad extends ArrayAdapter<searchadapter> {
    private Context mcontext;
    private int mresource;
    private int lastpos=-1;

    static class ViewHolder{
        TextView state;
    }

    public searchad(Context context, int resource, ArrayList<searchadapter> objects){
        super(context,resource,objects);
        mcontext=context;
        mresource=resource;
    }
    @Override
    public View getView(int pos, View convertView, ViewGroup parent){
        String state=getItem(pos).getSt();
        searchadapter sa=new searchadapter(state);

        final View result;

        searchad.ViewHolder holder;
        if (convertView==null){
            LayoutInflater inflater=LayoutInflater.from(mcontext);
            convertView=inflater.inflate(mresource,parent,false);
            holder=new searchad.ViewHolder();
            holder.state=(TextView)convertView.findViewById(R.id.txtst);
            result=convertView;

            convertView.setTag(holder);

        }
        else {
            holder=(searchad.ViewHolder)convertView.getTag();
            result=convertView;
        }
    }
}

searchint类:(搜索结果活动类)

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class searchint extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchint);
        search();
    }
    ListView listView;
    public void search(){
        listView=(ListView) findViewById(R.id.lsearch);
        listView.setClickable(true);
        DatabaseAccess d=DatabaseAccess.getInstance(getApplicationContext());
        d.open();
        ArrayList<searchadapter> a=d.st();
        d.close();
        searchad adapter1=new searchad(searchint.this,R.layout.search,a);
        listView.setAdapter(adapter1);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int 
            position, long id) {
                TextView tv=(TextView)view.findViewById(R.id.txtst);
                String st=tv.getText().toString();
                DatabaseAccess 
                d=DatabaseAccess.getInstance(getApplicationContext());
                d.open();
                d.run2(st.toUpperCase());
                Intent intent = new Intent(getApplicationContext(), 
                searchres.class);
                startActivity(intent);
            }
        });
    }
}

activity_searchint.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg2"
    tools:context=".searchint">

    <ListView
        android:id="@+id/lsearch"
        android:layout_width="395dp"
        android:layout_height="587dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="8dp" />
</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

我怀疑您的问题可能是由于可能提取的行数所致。

简而言之,提取的行数将是每个表中匹配的行数的逐步乘积。

也就是说,如果is状态是x并且有:-

  • 状态为x的10个陆行
  • 状态为x的4个公司行
  • 状态为x的3个教育行
  • 状态为x的3个人行
  • 3个状态分别为x和
  • 的娱乐行
  • 3个状态为x的天气行

结果将是3,240行,看似是相对少量的数据。

考虑也许更合理的有用/可用数据量,例如:-

  • 状态为x的30个陆行
  • 状态为x的20个公司行
  • 状态为x的10个教育行
  • 状态为x的10个人行
  • 状态为x和
  • 的10个娱乐行
  • 状态为x的10条天气行

然后即使真正有用的数据可以表示为90行,也将生成6,000,000行

测试示例

考虑以下用于模拟您的查询的内容:-

import readline
from colorama import Fore

main_user_input = input(Fore.LIGHTWHITE_EX + 'Ask' +  Fore.LIGHTGREEN_EX + '@' + Fore.LIGHTRED_EX + 'Question ' + Fore.LIGHTGREEN_EX + '->' + Fore.LIGHTWHITE_EX)

结果

运行上述命令后,产生的行从0到1000多行(为缅因州创建行的风险相对较低(PS请勿删除WHERE子句,尝试生成2,176,782,336行)(尝试在大约10分钟后崩溃)而且我必须重新启动PC)))。

通常有0行符合所有条件。但是,这是产生了90行的运行结果:-

首先排列(最后一个查询):-

enter image description here

一些结果数据,如您所见,一旦获得任何合理数量的数据,这些数据就很烦人:-

enter image description here