Reputation: 1042
I have a zip file which I have to extract information from which I can take specific information . The process of taking out the information takes about .7 of a second estimated. What I did was add in an Asynchronous class inside my ListAdapter (To make multiple threads so it can also load other similar threads) and now in my Asynchronous class it makes multiple threads which causes the database add information to its pre existing information.
Now my question is "How would I make an asynchronous threading on a listadapter without causing duplicates on the database?"
Here is the code:
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;
}
}
Upvotes: 0
Views: 201
Reputation: 553
Below is an example of an AsyncTask I'm using in the app I'm currently developing. I hope it helps get you on the right track.
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();
}
}
Then when I want to call it...
@Override
protected void onResume() {
new pullCombatActions().execute();
new prepCombat().execute();
super.onResume();}
To insure it doesn't keep adding the same data some If() statements should work. Another option would be to include the data already in your database. If this is data that should always be there, having it already there when the program first runs could save you some trouble.
I see your put statements but I'm not seeing where you tell it which row to place it.
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);
}
Would be a method to setup what and where I put the data in my database. Then I can call the method by
updateEntry(slot, String.valueOf(strNumber), String.valueOf(chaNumber), String.valueOf(wisNumber), String.valueOf(dexNumber), nameNumber, damId, HPId, npcId, attId, dodgeId, dreadId, critId);
Whenever you want to preform this save you would call the above code. Likely in a doInBackground()
Upvotes: 1