0

我有一个 zip 文件,我必须从中提取可以获取特定信息的信息。提取信息的过程大约需要 0.7 秒的估计时间。我所做的是在我的 ListAdapter 中添加一个异步类(为了创建多个线程,以便它也可以加载其他类似的线程),现在在我的异步类中它创建了多个线程,这导致数据库将信息添加到其预先存在的信息中。

现在我的问题是“如何在 listadapter 上创建异步线程而不导致数据库重复?”

这是代码:

Map<TextView, String> authorViews=Collections.synchronizedMap(new WeakHashMap<TextView, String>());
    Map<TextView, String> dateViews=Collections.synchronizedMap(new WeakHashMap<TextView, String>());
private class PresentInformation extends AsyncTask<Context, Void, Void>{

    private TextView Tauthor;
    private TextView Tlabel;
    String position;

    String date = null;
    String author = null;

    public PresentInformation(TextView author, TextView label, String Position) {
        // TODO Auto-generated constructor stub
        this.Tauthor = author;
        this.Tlabel = label;
        this.position = Position;
    }

    @Override
    protected Void doInBackground(Context... params) {
        // TODO Auto-generated method stub


        Boolean addToDB;

        if(author_exist(Tauthor)){
            author = getAuthorFName(position);
            addToDB = false;
        }else{
            //Declare an action to test if author does exist
            authorViews.put(Tauthor, position);
            author = getAuthor(position);
            addToDB = true;
        }
        dateViews.put(Tlabel, position);
        if(date_exist(Tlabel)){
            date = db.getDate(position);
            addToDB = false;
        }else{
            dateViews.put(Tlabel, position);
            date = date/time();
            addToDB = true;
        }

        if(addToDB){//Adds to database if they don't exist
            db.addDatabase(new Database(position, author, date));
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        if(author == null){
            author = "Author not found!";
        }
        if(date == null){
            date = "Date not found!";
        }
        Tlabel.setText(date);
        Tlabel.setText(author);
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        Tauthor.setText("Loading author please wait...");
        Tlabel.setText("Loading date please wait...");
    }

    public Boolean author_exist(TextView tv){
        String temp = authorViews.get(tv);
        if(temp ==null)
            return true;
        return false;
    }
    public Boolean date_exist(TextView tv){
        String temp = dateViews.get(tv);
        if(temp ==null)
            return true;
        return false;
    }
}

public class IconicAdapter extends ArrayAdapter<String>{

        IconicAdapter() {
            super(main.this, R.layout.bookselection_row, R.id.Book_Title, bookLocation);
            }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View row = null;
            if(row == null)
                row =  super.getView(position, convertView, parent);

             icon=(ImageView)row.findViewById(R.id.icon);
             author = (TextView)row.findViewById(R.id.book_Author);
             date_label = (TextView)row.findViewById(R.id.label);

             String path = bookLocation.get(position);

//               Collections(path, position, author, date_label);

            new PresentInformation(author, date_label, path).execute(main.this);

            try{
                Log.i("BookString input", bookLocation.get(position));
                loadImage.Uploader(bookLocation.get(position), icon);
            }catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }

            return row;
        }
    }       
4

1 回答 1

1

下面是我在当前正在开发的应用程序中使用的 AsyncTask 示例。我希望它能帮助你走上正轨。

private class prepCombat extends AsyncTask<String, Void, String>{
        @Override
        protected String doInBackground(String... params) {
            playerBattlePrep();
            findNPC();
            return null;}
        @Override
        protected void onPostExecute(String result) {
            new layoutCombat().execute();
        }
     }

然后当我想调用它时...

@Override
protected void onResume() {     
    new pullCombatActions().execute();
    new prepCombat().execute();
    super.onResume();}

为了确保它不会继续添加相同的数据,一些 If() 语句应该可以工作。另一种选择是包含数据库中已有的数据。如果这是应该始终存在的数据,那么在程序首次运行时就已经存在它可以为您节省一些麻烦。

我看到了你的 put 语句,但我没有看到你告诉它在哪一行放置它。

public void updateEntry(int rowId, String str, String cha, String wis, String dex, String name, int damId, int HPId, int npcId, int attId, int dodgeId, int dreadId, int critId) throws SQLException {

        ContentValues cvUpdate = new ContentValues();
        cvUpdate.put("Str", str);
        cvUpdate.put("Cha", cha);
        cvUpdate.put("Wis", wis);
        cvUpdate.put("Dex", dex);
        cvUpdate.put("Name", name);
        cvUpdate.put("StatDam", damId);
        cvUpdate.put("StatHP", HPId);
        cvUpdate.put("HP", HPId);
        cvUpdate.put("StatNpc", npcId);
        cvUpdate.put("StatAtt", attId);
        cvUpdate.put("StatDodge", dodgeId);
        cvUpdate.put("StatDread", dreadId);
        cvUpdate.put("StatCrit", critId);
        cvUpdate.put("Rank", "0");
        cvUpdate.put("Lvl", "1");
        ...

        ContentValues csUpdate = new ContentValues();
        csUpdate.put("PlayerHp", HPId);
        csUpdate.put("CombatFlag", "0");    

    dbhelper.myDataBase.update(dbhelper.SAVE_TABLE, cvUpdate, "_id" + "="  + rowId, null);
    dbhelper.myDataBase.update(dbhelper.COMBATSAVE_TABLE, csUpdate, "_id" + "="  + rowId, null);

    }

将是一种设置我将数据放入数据库中的内容和位置的方法。然后我可以通过

updateEntry(slot, String.valueOf(strNumber), String.valueOf(chaNumber), String.valueOf(wisNumber), String.valueOf(dexNumber), nameNumber, damId, HPId, npcId, attId, dodgeId, dreadId, critId);

每当您想要执行此保存时,您都可以调用上面的代码。可能在 doInBackground()

于 2012-04-12T17:43:59.600 回答