SimpleCursorAdapter没有填充ListView

时间:2017-04-30 21:06:02

标签: android listview android-sqlite

我正在尝试使用SimpleCursorAdapter从SQLiteDatabase填充ListView。没有错误,我知道数据库中有数据,但ListView上没有任何内容。

任何人都可以帮忙找出问题所在吗?

1)ListView的布局(activity_profilelist.xml)     

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_profilelist"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation= "vertical"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    tools:context="com.corvus.corvusenterprises.digimonapp.Profilelist">

    <TextView
        android:id="@+id/textView_names"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.1"
        android:gravity="center"
        android:textAppearance="@android:style/TextAppearance.Theme"
        android:textSize="25sp"
        android:text="Digimon"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation= "horizontal">

        <ListView
            android:id="@+id/ListView_names"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="100" />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_weight="0.1">

        <Button
            android:id="@+id/button_update"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:text="Update List">
        </Button>
    </LinearLayout>
</LinearLayout>

2)ListView(activity_list_example.xml)中各个条目的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
    <TextView
        android:id="@+id/Digimon_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="28sp" />
    <TextView
        android:id="@+id/Digimon_favourites"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="28sp" />
</LinearLayout>

3)ListView(ProfileList.java)的Java

package com.corvus.corvusenterprises.digimonapp;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import static com.corvus.corvusenterprises.digimonapp.R.id.button_update;

public class Profilelist extends Digimon2 {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profilelist);

        DBHelper dbHelper = DBHelper.getInstance(MyApp.getContext());
        Cursor cursor = dbHelper.defaultMainMenu();

        String[]columns = new String[] {"name","favourite","path"};

        ListView theListView = (ListView) findViewById(R.id.listView_names);

        int[]to = new int[] {R.id.Digimon_name,R.id.Digimon_favourites};

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.activity_list_example,cursor,columns,to,0);

        theListView.setAdapter(adapter);
    }
}

4)数据库助手的Java(DBHelper.java)

package com.corvus.corvusenterprises.digimonapp;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
import android.content.ContentValues;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;

import org.jsoup.Jsoup;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import static android.graphics.Bitmap.Config.ARGB_8888;
import static android.graphics.Bitmap.createBitmap;


/**
 * Created by Jack on 28-Mar-17.
 */

public class DBHelper extends SQLiteOpenHelper {
    private static final int version = 1;

    private static final String DATABASE_NAME = "DigimonDatabase.db";
    private static final String DIGIMON_TABLE_NAME = "Digimon";
    private static final String DIGIMON_COLUMN_NAME = "name";
    private static final String DIGIMON_COLUMN_DUB_NAME = "dub_name";
    private static final String DIGIMON_COLUMN_LEVEL = "level";
    private static final String DIGIMON_COLUMN_ATTRIBUTE = "attribute";
    private static final String DIGIMON_COLUMN_DESCRIPTION = "description";
    private static final String DIGIMON_COLUMN_FAVOURITE = "favourite";
    private static final String EVOLUTIONS_TABLE_NAME = "Evolutions";
    private static final String EVOLUTIONS_COLUMN_FROM = "\"from\"";
    private static final String EVOLUTIONS_COLUMN_TO = "\"to\"";
    private static final String EVOLUTIONS_COLUMN_CONDITIONS = "conditions";
    private static final String ATTACKS_TABLE_NAME = "Attacks";
    private static final String ATTACKS_COLUMN_NAME = "name";
    private static final String ATTACKS_COLUMN_DUB_NAME = "dub_name";
    private static final String ATTACKS_COLUMN_DIGIMON = "digimon";
    private static final String ATTACKS_COLUMN_DESCRIPTION = "description";
    private static final String PICTURES_TABLE_NAME = "Pictures";
    private static final String PICTURES_COLUMN_DIGIMON = "digimon";
    private static final String PICTURES_COLUMN_PATH = "path";

    private static String[]digimonArray = {/*19MB worth of Digimon names removed for length constraints.*/};
    private static boolean[]presentDigimon = new boolean[digimonArray.length];
    private static DBHelper instance;

    private DBHelper(Context context)
    {
        super(context, DATABASE_NAME, null, version);
    }

    public static DBHelper getInstance(Context ctx)
    {
        if(instance == null)
            instance = new DBHelper(ctx);

        return instance;
    }


    @Override
    public void onCreate(SQLiteDatabase db)
    {

        //Create and define the tables
        db.execSQL("CREATE TABLE "+DIGIMON_TABLE_NAME+" ("+DIGIMON_COLUMN_NAME+" TEXT PRIMARY KEY, "+DIGIMON_COLUMN_DUB_NAME+" TEXT NULL, "+DIGIMON_COLUMN_LEVEL+" TEXT NULL, "+DIGIMON_COLUMN_ATTRIBUTE+" TEXT NULL, "+DIGIMON_COLUMN_DESCRIPTION+" TEXT NULL, "+DIGIMON_COLUMN_FAVOURITE+" TEXT DEFAULT 'FALSE')");
        db.execSQL("CREATE TABLE "+EVOLUTIONS_TABLE_NAME+" ("+EVOLUTIONS_COLUMN_FROM+" TEXT, "+EVOLUTIONS_COLUMN_TO+" TEXT, "+EVOLUTIONS_COLUMN_CONDITIONS+" TEXT NULL, primary key("+EVOLUTIONS_COLUMN_FROM+", "+EVOLUTIONS_COLUMN_TO+"))");
        db.execSQL("CREATE TABLE "+ATTACKS_TABLE_NAME+" ("+ATTACKS_COLUMN_NAME+" TEXT, "+ATTACKS_COLUMN_DUB_NAME+" TEXT NULL, "+ATTACKS_COLUMN_DIGIMON+" TEXT, "+ATTACKS_COLUMN_DESCRIPTION+" TEXT NULL, primary key("+ATTACKS_COLUMN_NAME+", "+ATTACKS_COLUMN_DIGIMON+"))");
        db.execSQL("CREATE TABLE "+PICTURES_TABLE_NAME+" ("+PICTURES_COLUMN_DIGIMON+" TEXT PRIMARY KEY, "+PICTURES_COLUMN_PATH+" TEXT)");

        //Fill in some default Digimon
        insertDigimon("Guilmon", "Guilmon", "Child/Rookie", "Virus", "Dinosaur", "TRUE");
        insertDigimon("Growmon", "Growlmon", "Adult/Champion", "Virus", "Bigger Dinosaur", "FALSE");
        insertDigimon("Terriermon", "terriermon", "Child/Rookie", "Vaccine", "Dogbunny", "FALSE");
        insertDigimon("Galgomon", "Gargomon", "Adult/Champion", "Vaccine", "Gunbunny", "FALSE");
        insertDigimon("Kyubimon", "Kyubimon", "Adult/Champion", "Data", "9-Tailed Fox", "FALSE");
        insertDigimon("Taomon", "Taomon", "Perfect/Ultimate", "Data", "Kitsune Miko", "FALSE");
        insertDigimon("Impmon", "Impmon", "Child/Rookie", "Virus", "Kid in a purple onesie", "FALSE");
        insertDigimon("Beelzebumon", "Beelzemon", "Ultimate/Mega", "Virus", "Demon Lord of Gluttony", "FALSE");

        presentDigimon[460]  = true; presentDigimon[454]  = true;
        presentDigimon[1019] = true; presentDigimon[374]  = true;
        presentDigimon[572]  = true; presentDigimon[1013] = true;
        presentDigimon[507]  = true; presentDigimon[100]  = true;

        insertEvolution("Guilmon", "Growmon");
        insertEvolution("Terriermon", "Galgomon");
        insertEvolution("Kyubimon", "Taomon");
        insertEvolution("Impmon", "Beelzebumon", "(Warp Evolution)");

        insertAttack("Fireball", "Pyro Sphere", "Guilmon", "Explosive Fireball");
        insertAttack("Rock Breaker", "Rock Breaker", "Guilmon", "Claw Swipe");
        insertAttack("Exhaust Flame", "Pyro Blaster", "Growmon", "Fire Laser");
        insertAttack("Plasma Blade", "Dragon Slash", "Growmon", "Forearm Blades");
        insertAttack("Blazing Fire", "Bunny Blast", "Terriermon", "Energy Blast");
        insertAttack("Petit Twister", "Terrier Tornado", "Terriermon", "Throws Tornado");
        insertAttack("Gatling Arm", "Gargo Laser", "Galgomon", "Fires Guns");
        insertAttack("Dum Dum Upper", "Bunny Pummel", "Galgomon", "Fires Guns While Punching");
        insertAttack("Koenryu", "Dragon Wheel", "Kyubimon", "Fire Dragon");
        insertAttack("Onibidama", "Fox-Tail Inferno", "Kyubimon", "Fireballs from the tails");
        insertAttack("Bonhitsusen", "Talisman of Light", "Taomon", "Energy Seal Laser");
        insertAttack("Oṃ", "Talisman Spell", "Taomon", "Makes a dome shield");
        insertAttack("Night of Fire", "Badaboom", "Impmon", "Mini-Fireballs");
        insertAttack("Pillar of Fire", "", "Impmon", "Wall of Flames");
        insertAttack("Double Impact", "Double Impact", "Beelzebumon", "Fires two bullets");
        insertAttack("Darkness Claw", "", "Beelzebumon", "Kills Leomon");

        insertPicture("Guilmon","R.mipmap.guilmon.jpg");
        insertPicture("Growmon","R.mipmap.growmon.jpg");
        insertPicture("Terriermon","R.mipmap.terriermon.jpg");
        insertPicture("Galgomon","R.mipmap.galgomon.jpg");
        insertPicture("Kyubimon","R.mipmap.kyubimon.jpg");
        insertPicture("Taomon","R.mipmap.taomon.jpg");
        insertPicture("Impmon","R.mipmap.impmon.jpg");
        insertPicture("Beelzebumon","R.mipmap.beelzebumon.jpg");

        //Populate Tables
        new Downloader().start();
    }

    private class Downloader extends Thread
    {
        public void run()
        {
            int digimonToGetAtOnce = 20;
            HttpURLConnection connection;
            URL url;
            String wikimonAddress, downloadResult;
            BufferedInputStream in;
            for(int i=0;i<digimonToGetAtOnce;)
            {
                int rand = (int)(Math.random()*digimonArray.length);
                if(!presentDigimon[rand])
                {
                    wikimonAddress = "https://wikimon.net/api.php?action=parse&format=json&page=" + digimonArray[rand] + "&prop=text";
                    try {
                        url = new URL(wikimonAddress);
                        connection = (HttpURLConnection) url.openConnection();
                        in = new BufferedInputStream(connection.getInputStream());
                        downloadResult = readStream(in);
                        connection.disconnect();
                        i++;
                        presentDigimon[rand] = true;
                        sortInput(downloadResult);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        private String readStream(InputStream is) throws IOException {
            StringBuilder sb = new StringBuilder();
            BufferedReader r = new BufferedReader(new InputStreamReader(is),1000);
            for (String line = r.readLine(); line != null; line =r.readLine()){
                sb.append(line);
            }
            is.close();
            return sb.toString();
        }
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldver, int newVer)
    {
        db.execSQL("DROP TABLE IF EXISTS "+DIGIMON_TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS "+ATTACKS_TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS "+EVOLUTIONS_TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS "+PICTURES_TABLE_NAME);
        onCreate(db);
   }
    public void insertDigimon(String name, String dub_name, String level, String attribute, String description, String favourite)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DIGIMON_COLUMN_NAME,name);
        values.put(DIGIMON_COLUMN_DUB_NAME,dub_name);
        values.put(DIGIMON_COLUMN_LEVEL,level);
        values.put(DIGIMON_COLUMN_ATTRIBUTE,attribute);
        values.put(DIGIMON_COLUMN_DESCRIPTION,description);
        values.put(DIGIMON_COLUMN_FAVOURITE, favourite);
        db.insert(DIGIMON_TABLE_NAME, null, values);
        db.close();
    }
    public void insertAttack(String name, String dub_name, String digimon, String description)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(ATTACKS_COLUMN_NAME, name);
        values.put(ATTACKS_COLUMN_DUB_NAME,dub_name);
        values.put(ATTACKS_COLUMN_DIGIMON, digimon);
        values.put(ATTACKS_COLUMN_DESCRIPTION, description);
        db.insert(ATTACKS_TABLE_NAME, null, values);
        db.close();
    }
    public void insertEvolution(String from, String to, String conditions)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(EVOLUTIONS_COLUMN_TO,to);
        values.put(EVOLUTIONS_COLUMN_FROM,from);
        values.put(EVOLUTIONS_COLUMN_CONDITIONS,conditions);
        db.insert(EVOLUTIONS_TABLE_NAME, null, values);
        db.close();
    }
    public void insertEvolution(String from, String to)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(EVOLUTIONS_COLUMN_TO,to);
        values.put(EVOLUTIONS_COLUMN_FROM,from);
        db.insert(EVOLUTIONS_TABLE_NAME, null, values);
        db.close();
    }
    public void insertPicture(String digimon, String path)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(PICTURES_COLUMN_DIGIMON,digimon);
        values.put(PICTURES_COLUMN_PATH,path);
        db.insert(PICTURES_TABLE_NAME, null, values);
        db.close();
    }
    public Cursor defaultMainMenu()
    {
        SQLiteDatabase db = getWritableDatabase();
        String query = "SELECT "+DIGIMON_TABLE_NAME+".ROWID AS _id, "+DIGIMON_COLUMN_NAME+", "+DIGIMON_COLUMN_FAVOURITE+" FROM "+DIGIMON_TABLE_NAME;
        Cursor cursor = db.rawQuery(query,null);
        int dummy = cursor.getCount();
        db.close();
        return cursor;
    }
    public Cursor searchMenuQuery(String search)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT ROWID AS _id, "+DIGIMON_COLUMN_NAME+", "+DIGIMON_COLUMN_FAVOURITE+", "+PICTURES_TABLE_NAME+"."+PICTURES_COLUMN_PATH+" FROM "+DIGIMON_TABLE_NAME+" JOIN "+PICTURES_TABLE_NAME+" ON "+PICTURES_TABLE_NAME+"."+PICTURES_COLUMN_DIGIMON+"="+DIGIMON_TABLE_NAME+"."+DIGIMON_COLUMN_NAME +" WHERE "+DIGIMON_COLUMN_NAME+" LIKE '%"+search+"%' UNION SELECT ROWID AS _id, "+DIGIMON_COLUMN_NAME+", "+DIGIMON_COLUMN_FAVOURITE+", "+PICTURES_TABLE_NAME+"."+PICTURES_COLUMN_DIGIMON+" FROM "+DIGIMON_TABLE_NAME+" JOIN "+PICTURES_TABLE_NAME+" ON "+PICTURES_TABLE_NAME+"."+PICTURES_COLUMN_DIGIMON+"="+DIGIMON_TABLE_NAME+"."+DIGIMON_COLUMN_NAME +" WHERE "+DIGIMON_COLUMN_DUB_NAME+" LIKE '%"+search+"%'";
        Cursor cursor = db.rawQuery(query,null);
        int dummy = cursor.getCount();
        db.close();
        return cursor;
    }
    public Cursor favouritesMenuQuery()
    {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT ROWID AS _id, "+DIGIMON_COLUMN_NAME+", "+PICTURES_TABLE_NAME+"."+PICTURES_COLUMN_PATH+" FROM "+DIGIMON_TABLE_NAME+" JOIN "+PICTURES_TABLE_NAME+" ON "+PICTURES_TABLE_NAME+"."+PICTURES_COLUMN_DIGIMON+"="+DIGIMON_TABLE_NAME+"."+DIGIMON_COLUMN_NAME +" WHERE "+DIGIMON_COLUMN_FAVOURITE+" = 'TRUE'";
        Cursor cursor = db.rawQuery(query,null);
        int dummy = cursor.getCount();
        db.close();
        return cursor;
    }
    public Cursor digimonProfileQuery(String name)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT ROWID AS _id, * FROM "+DIGIMON_TABLE_NAME+" WHERE "+DIGIMON_COLUMN_NAME+" = '"+name+"'";
        Cursor cursor = db.rawQuery(query, null);
        int dummy = cursor.getCount();
        db.close();
        return cursor;
    }
    public Cursor digimonAttacksQuery(String name)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT ROWID AS _id, * FROM "+ATTACKS_TABLE_NAME+" WHERE "+ATTACKS_COLUMN_DIGIMON+" = '"+name+"'";
        Cursor cursor = db.rawQuery(query, null);
        int dummy = cursor.getCount();
        db.close();
        return cursor;
    }
    public Cursor digimonEvolutionFromQuery(String name)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT ROWID AS _id, * FROM "+EVOLUTIONS_TABLE_NAME+" WHERE "+EVOLUTIONS_COLUMN_FROM+" = '"+name+"'";
        Cursor cursor = db.rawQuery(query, null);
        int dummy = cursor.getCount();
        db.close();
        return cursor;
    }
    public Cursor digimonEvolutionToQuery(String name)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT ROWID AS _id, * FROM "+EVOLUTIONS_TABLE_NAME+" WHERE "+EVOLUTIONS_COLUMN_TO+" = '"+name+"'";
        Cursor cursor = db.rawQuery(query, null);
        int dummy = cursor.getCount();
        db.close();
        return cursor;
    }
    public Cursor digimonPictureQuery(String name)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT ROWID AS _id, "+PICTURES_COLUMN_PATH+" FROM "+PICTURES_TABLE_NAME+" WHERE "+PICTURES_COLUMN_DIGIMON+" = '"+name+"'";
        Cursor cursor = db.rawQuery(query, null);
        int dummy = cursor.getCount();
        db.close();
        return cursor;
    }
    public void sortInput(String input)
    {
        int index1, index2;
        String name, dubName, level, attribute, description, imageUrl, imagePath;
        String attackName="", attackDubName="", attackDescription="No description given.";
        String from = "", to = "", conditions = "";
        ArrayList<String[]> attacks = new ArrayList<String[]>();
        ArrayList<String[]> evolvesFrom = new ArrayList<String[]>();
        ArrayList<String[]> evolvesTo = new ArrayList<String[]>();
        boolean tableEnded = false, rowEnded = false;

        index2 = input.indexOf("\",\n\"pageid\":");
        name = input.substring(20,index2);

        index1 = input.indexOf("Japanese</b></span></span><br />")+32;
        index2 = input.indexOf("\\n</td></tr>\\n<tr>\\n<td style=\\\"border-top:1px solid gray\\\" align=\\\"center\\\"><span class=\\\"plainlinks\\\">",index1);
        description = input.substring(index1, index2);
        description = Jsoup.parse(description).text();

        if(input.contains("<b>Dub:</b>\\n</td>\\n<td>\\n</td>\\n<td>\\n</td>\\n<td valign=\\\"center\\\"><i>"))
        {
            index1 = input.indexOf("<b>Dub:</b>\\n</td>\\n<td>\\n</td>\\n<td>\\n</td>\\n<td valign=\\\"center\\\"><i>")+72;
            index2 = input.indexOf("</i>",index1);
            dubName = input.substring(index1,index2);
        }
        else dubName = name;

        index1 = input.indexOf("Level</font></a></b>\\n</td>\\n<td style=\\\"background:#252525;width:175px;border-bottom:1px solid #808080\\\"><a href=\\\"/")+118;
        index2 = input.indexOf("\\\" title=\\\"",index1);
        level = input.substring(index1,index2);

        index1 = input.indexOf("Attribute</font></a></b>\\n</td>\\n<td style=\\\"background:#252525;width:175px;border-bottom:1px solid #808080\\\"><a href=\\\"/Category:")+136;
        index2 = input.indexOf("\\\" title=\\\"",index1);
        attribute = input.substring(index1,index2);

        index2 = input.indexOf("width=\\\"320\\\" height=\\\"320\\\"");
        index1 = input.lastIndexOf("src=\\\"",index2);
        imageUrl = "https://wikimon.net"+input.substring(index1,index2);


        index1 = input.indexOf("<i><b>")+6;
        index2 = input.indexOf("</b></i>",index1);

        for(int i = 0;!tableEnded;i++)
        {
            //name, dub_name, digimon, description
            attackName = input.substring(index1,index2);

            index1 = input.indexOf("<i>",index2)+3;
            index2 = input.indexOf("</i>",index1);
            attackDubName = input.substring(index1,index2);
            index1 = input.indexOf("</td>",index2);
            index1 = input.indexOf("</td>",index1);
            index1 = input.indexOf("</td>",index1);
            index1 = input.indexOf("<i>",index1)+3;
            index2 = input.indexOf("</i>",index1);
            if(input.substring(index1,index2).compareTo("<br />")!=0)
                attackDubName = input.substring(index1,index2);
            else if(attackDubName.compareTo("<br />")==0)
                attackDubName = attackName;

            index1 = input.indexOf("<td style",index2);
            index1 = input.indexOf("\\\">",index1)+3;
            index2 = input.indexOf("\\n</td></tr>");
            attackDescription = input.substring(index1,index2);
            attacks.add(i,new String[]{attackName,attackDubName,name,attackDescription});
            if(input.substring(index2,index2+64).contains("</table>"))
            {
                tableEnded = true;
            }
        }
        tableEnded = false;
        for(int i = 0;!tableEnded;i++)
        {
            index1 = input.indexOf("<li>",index2);
            index1 = input.indexOf("\\\"/",index1)+3;
            index2 = input.indexOf("\\\" title",index1);
            from = input.substring(index1,index2);

            index1 = input.indexOf("</a>",index2);
            index2 = input.indexOf("</li>",index1);
            if(Jsoup.parse(input.substring(index1,index2)).text().contains("("))
            {
                conditions = Jsoup.parse(input.substring(index1,index2)).text();
                evolvesFrom.add(new String[]{from,name,conditions});
            }
            else evolvesFrom.add(new String[]{from,name});

            if(input.substring(index1,index1+9).contains("</ul>"))
            {
                tableEnded = true;
            }
        }
        tableEnded = false;
        for(int i = 0;!tableEnded;i++)
        {
            index1 = input.indexOf("<li>",index2);
            index1 = input.indexOf("\\\"/",index1)+3;
            index2 = input.indexOf("\\\" title",index1);
            to = input.substring(index1,index2);

            index1 = input.indexOf("</a>",index2);
            index2 = input.indexOf("</li>",index1);
            if(Jsoup.parse(input.substring(index1,index2)).text().contains("("))
            {
                conditions = Jsoup.parse(input.substring(index1,index2)).text();
                evolvesTo.add(new String[]{name,to,conditions});
            }
            else evolvesTo.add(new String[]{name,to});
            if(input.substring(index1,index1+9).contains("</ul>"))
            {
                tableEnded = true;
            }
        }

        insertDigimon(name,dubName,level,attribute,description,"FALSE");
        for(int i=0;i<attacks.size();i++)
        {
            insertAttack(attackName,attackDubName,name,attackDescription);
        }
        for(int i=0;i<evolvesFrom.size();i++)
        {
            if(evolvesFrom.get(i).length==3)
            insertEvolution(evolvesFrom.get(1)[0],name,evolvesFrom.get(1)[2]);
            else
                insertEvolution(evolvesFrom.get(1)[0],name);
        }
        for(int i=0;i<evolvesTo.size();i++)
        {
            if(evolvesTo.get(i).length==3)
                insertEvolution(name,evolvesTo.get(1)[1],evolvesTo.get(1)[2]);
            else
                insertEvolution(name,evolvesTo.get(1)[1]);
        }
        new DownloadFile().execute(imageUrl,name);
        insertPicture(name,"/res/mipmap/xxxhdpi/"+name+".jpg");
    }

    class DownloadFile extends AsyncTask<String,Integer,Long> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
        @Override
        protected Long doInBackground(String... aurl) {
            int count;
            try {
                URL url = new URL((String) aurl[0]);
                URLConnection connection = url.openConnection();
                connection.connect();
                String targetFileName=aurl[1]+".jpg";
                int lengthOfFile = connection.getContentLength();
                String PATH = Environment.getExternalStorageDirectory()+ "/res/mipmap/xxxhdpi/";
                File folder = new File(PATH);
                if(!folder.exists()){
                    folder.mkdir();//If there is no folder it will be created.
                }
                InputStream input = new BufferedInputStream(url.openStream());
                OutputStream output = new FileOutputStream(PATH+targetFileName);
                byte data[] = new byte[1024];
                long total = 0;
                while ((count = input.read(data)) != -1) {
                    total += count;
                    publishProgress ((int)(total*100/lengthOfFile));
                    output.write(data, 0, count);
                }
                output.flush();
                output.close();
                input.close();
            } catch (Exception e) {}
            return null;
        }
    }
}

欢迎任何和所有帮助。

1 个答案:

答案 0 :(得分:0)

您的简单游标适配器有问题, 这里提供三列

String[]columns = new String[] {"name","favourite","path"};

但是这里只提供两种观点:

int[]to = new int[] {R.id.Digimon_name,R.id.Digimon_favourites};

也在您的原始查询中

String query = "SELECT "+DIGIMON_TABLE_NAME+".ROWID AS _id, 
"+DIGIMON_COLUMN_NAME+", "+DIGIMON_COLUMN_FAVOURITE+" FROM 
"+DIGIMON_TABLE_NAME;

您只提供了“_id”,“name”和“favorite”列,而不是“路径”。