简体   繁体   中英

How to get an object from one class to another class?

I am new to android developing. I am trying to program an application related to auction. I studied this open-source application sample and try to customize it. http://product.hubspot.com/blog/building-an-auction-app-in-a-weekend

It applied Parse as server to upload and retrieve data.

I need to copy an object called AuctionItem from one class to another class for getting variables(objectID) in that object.

AuctionItem.java

@ParseClassName("Item")
public class AuctionItem extends ParseObject {

    public AuctionItem() {}

    public String getObjectID() { return getString("objectID");}
    public String getName() {return getString("name");}
    public String getDescription() {return getString("description");}
    public String getDonorName() {return getString("donorname");}

}

ItemListActivity.java

public class ItemListActivity extends ActionBarActivity {
     List<AuctionItem> allItems = new ArrayList<AuctionItem>();
     BaseAdapter adapter;
     DataManager data = DataManager.getInstance();
     String listQuery = DataManager.QUERY_ALL;

     public void setup() {
         allItems = data.getItemsMatchingQuery(listQuery, this);
         adapter = new AuctionItemAdapter();
     }

     public class AuctionItemAdapter extends BaseAdapter {
         @Override
         public AuctionItem getItem(int position) {
              return allItems.get(position);
         }
         @Override
         public View getView(int position, View convertView, ViewGroup parent) {
              final AuctionItem item = getItem(position);
         }
     }

DataManager.java

public class DataManager {
     private List<AuctionItem> allItems = new ArrayList<AuctionItem>();
     static DataManager singletonInstance;
     public static final String QUERY_ALL = "ALL";

     public DataManager() {}

     public static DataManager getInstance() {
         if (singletonInstance == null)
           singletonInstance = new DataManager();

         return singletonInstance;
         }
     public List<AuctionItem> getItemsMatchingQuery(String query, Activity context) {
         if (query.equals(QUERY_ALL)) {
         return allItems;
         }

         else {
             ArrayList<String> queryWords = new ArrayList<String>();
             queryWords.addAll(Arrays.asList(query.split(" ")));

             ArrayList<AuctionItem> results = new ArrayList<AuctionItem>();
             for (AuctionItem item : allItems) {
                 for (String word : queryWords) {
                    if (word.length() > 1 &&
                     (item.getName().toLowerCase().contains(word.toLowerCase()) || item.getDonorName().toLowerCase().contains(word.toLowerCase()) ||
                      item.getDescription().toLowerCase().contains(word.toLowerCase())))
                    results.add(item);
                 }
              }

              return results;
         }
    }
}

I want AuctionItem item this object and call objectID from another class. I tried to make another Auction called copy and program it likes this:

public AuctionItem copy;
copy = item;

But when I use other class to call this variable, it said: Non-static field cannot be referenced from a static context.

What should I do to get this object.Please help!!

Try to get your Object so:

public AuctionItem copy;
String item = copy.getObjectID();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM