Listadapter和数据库

时间:2012-04-11 13:33:26

标签: android listview

我有一个zip文件,我必须提取信息,我可以从中获取具体信息。取出信息的过程大约需要0.7秒。我所做的是在ListAdapter中添加一个Asynchronous类(要创建多个线程,这样它也可以加载其他类似的线程),现在在我的异步类中它创建多个线程,这会导致数据库向其预先存在的信息添加信息。 p>

现在我的问题是“如何在不对数据库造成重复的情况下对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;
        }
    }       

1 个答案:

答案 0 :(得分: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()