2012-04-11 1 views
0

私は特定の情報を得ることができる情報を抽出する必要があるzipファイルを持っています。情報を取り出すプロセスは約7秒かかります。私がやったのは、ListAdapterの中にAsynchronousクラスを追加したものです(複数のスレッドを作成して他の同様のスレッドをロードできるようにしています)。そして今、非同期クラスで複数のスレッドを作成してデータベースに情報を追加します。リストアダプターとデータベース

私の質問は、 "どのように私はデータベース上で重複を引き起こさずに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

私が現在開発していますアプリで使用している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();} 

。もう1つの選択肢は、既にデータベースにデータを含めることです。これが常に存在するはずのデータであれば、プログラムが最初に実行されたときに既にそれを持っていれば、何らかのトラブルを避けることができます。

私はあなたの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()の可能性があります

+0

私はすでにそれを行いました。私はエグゼクティブを使いました。 4回の試行後に神様に感謝するユニークな鍵を作った。 – sdfwer

関連する問題