I am using Sugar ORM with android studio programming in java. I have my table (Activitytable) set up using a tutorial found here: http://ift.tt/200TqqL and everything seems to be working fine. I have a main menu with buttons on where I have added a test data in and saved in the onCreate method. What appears to be happening is when I click the button from main menu to the view data activity, it calls the select statement twice and therefore any information I have in the database is shown twice. My code is below: My activity table:
import android.renderscript.Sampler;
import com.orm.SugarRecord;
public class Activitytable extends SugarRecord {
public String Title;
public String Overview;
public String Phone_number;
public String email;
public String Address_line1;
public String Address_line2;
public String Town;
public String County;
public String Postcode;
public String Disabled_access;
public String Toilets;
public String Baby_changing;
public String Parking;
public String Suitable_youngest;
public String Suitable_oldest;
public String Category;
public String Opening_times;
public String Opening_days;
public String Average_price;
public String Overall_rating;
public String Fun_factor;
public String Value_money;
public Activitytable()
{
}
public Activitytable(String title, String overview, String phone, String email, String Addressline1, String Addressline2, String Town, String County, String Postcode, String Disabledaccess,
String Toilets, String Babychanging, String Parking, String Suitableyoungest, String Suitableoldest, String Category, String Openingtimes, String Openingdays, String Averageprice,
String Overall_rating, String Fun_factor, String Value_money
)
{
this.Title = title;
this.Overview = overview;
this.Phone_number = phone;
this.email = email;
this.Address_line1 = Addressline1;
this.Address_line2 = Addressline2;
this.Town = Town;
this.County = County;
this.Postcode = Postcode;
this.Disabled_access = Disabledaccess;
this.Toilets = Toilets;
this.Baby_changing = Babychanging;
this.Parking = Parking;
this.Suitable_youngest = Suitableyoungest;
this.Suitable_oldest = Suitableoldest;
this.Category = Category;
this.Opening_times = Openingtimes;
this.Opening_days = Openingdays;
this.Average_price = Averageprice;
this.Overall_rating = Overall_rating;
this.Fun_factor = Fun_factor;
this.Value_money = Value_money;
}
}
My main menu java class: package com.example.kathe.parenttripapp;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.example.kathe.parenttripapp.Database.Menu1;
import com.example.kathe.parenttripapp.DatabaseTables.Activitytable;
public class Menu extends AppCompatActivity {
Button LogoutButton;
private static final String PREFER_NAME = "Reg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Activitytable activity = new Activitytable("Swimming with eels","In a pond","5733573300","pond@pondlife.com","h","h","h","h","h","False","False","False","False","2","15","Outdoor","All Day","Everyday","FREE","1/5","1/5","5/5");
activity.save();
LogoutButton = (Button) findViewById(R.id.LogoutButton);
LogoutButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
SharedPreferences sharedPreferences = getSharedPreferences(PREFER_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.commit();
Toast.makeText(getApplicationContext(),"Logged out",Toast.LENGTH_SHORT).show();
}
});
}
public void goToProfile(View view){
Intent i = new Intent(this,Profile.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
public void goToViewAll(View view){
Intent i = new Intent(this,ViewData.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
public void goToOutdoor(View view){
Intent i = new Intent(this,Outdoor.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
public void goToIndoor(View view){
Intent i = new Intent(this,Indoor.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
public void goToEating(View view){
Intent i = new Intent(this,Eating.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
public void goToLogin(View view){
Intent i = new Intent(this,Login.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
public void goToMenu(View view){
Intent i = new Intent(this,Menu1.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu){
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()) {
case R.id.menu_menu:
Intent i = new Intent(this, Menu.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
return true;
case R.id.menu_Profile:
Intent p = new Intent(this, Profile.class);
p.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
p.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(p);
return true;
}
return true;
}
}
my view all java class: public class ViewData extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_data);
ListView listView = (ListView) findViewById(R.id.viewAll_listview);
long count = Activitytable.count(Activitytable.class);
if(count>0){
Activitytable.listAll(Activitytable.class);
List<Activitytable> activitytable = Activitytable.listAll(Activitytable.class);
ViewAllListView madapter = new ViewAllListView(getApplicationContext(), activitytable);
listView.setAdapter(madapter);
}
else {
Toast.makeText(getApplicationContext(), "No Data Available in Table", Toast.LENGTH_LONG);
}
}
my custom list view adapter:
public class ViewAllListView extends BaseAdapter {
private LayoutInflater inflater;
public ViewHolder holder=null;
private List<Activitytable> activitytable = new ArrayList<>();
ViewAllListView(Context context, List<Activitytable> values)
{
this.activitytable = values;
this.inflater = LayoutInflater.from(context);
}
public int getCount(){
return activitytable.size();
}
@Override
public Object getItem(int position) {return position;}
@Override
public long getItemId(int position) {return 0;}
@Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
convertView=inflater.inflate(R.layout.layout_for_listview_show,null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.title_textview);
holder.Overall_rating = (TextView) convertView.findViewById(R.id.overallrating_textview);
holder.Category = (TextView) convertView.findViewById(R.id.category_textview);
holder.Average_price = (TextView) convertView.findViewById(R.id.averageprice_textview);
convertView.setTag(holder);
}
else {
holder = (ViewHolder)convertView.getTag();
}
holder.title.setText(this.activitytable.get(position).Title);
holder.Overall_rating.setText(this.activitytable.get(position).Overall_rating);
holder.Category.setText(this.activitytable.get(position).Category);
holder.Average_price.setText(this.activitytable.get(position).Average_price);
return convertView;
}
static class ViewHolder
{
TextView title, Overall_rating, Category, Average_price;
}
}
It's not coming up with any errors, its just using SELECT statement twice and so data is appearing twice. P.s. Sorry for my code not being very well aligned, I have no internet and am having to use free wifi in McDonalds, so don't have much time!
Aucun commentaire:
Enregistrer un commentaire