Debasis Mandal
Debasis Mandal

Reputation: 21

Can't access a class object from a function says unknown class

Trying to fetch all the selected values from a list view which is stored in a class array. When I tried to fetch from that class and trying to create a object in a function I can't use it. Here is the code

public void showResult(View view) {
        String result = "Selected Product are :";
        int totalAmount=0;
        ArrayList<contactStruct> product = new ArrayList<contactStruct>();

        for (product p : boxAdapter.getBox()) {
            if (p.box){
                result += "\n" + p.name;
                totalAmount+=p.price;
            }
        }
        Toast.makeText(this, result+"\n"+"Total Amount:="+totalAmount, Toast.LENGTH_LONG).show();
    } 

it is within onCreate. It says unknown class product. Please help. Yes its on the same package. Here is the class

public class contactStruct {
    String name;
    String mob;
    int image;
    boolean box;


    contactStruct(String _describe, String _mob, int _image, boolean _box) {
        name = _describe;
        mob = _mob;
        image = _image;
        box = _box;
    }
}

And Here is my complete code

import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class LoadContacts extends AppCompatActivity {

    ArrayList<contactStruct> products = new ArrayList<contactStruct>();
    Cursor cursor ;
    String name, phoneNumber;
    int imgP;
    ListAdapter boxAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load_contacts);
        fillData();
        boxAdapter = new com.example.piku.SteaUd.ListAdapter(this, products);
        ListView lvMain = (ListView) findViewById(R.id.lview);
        lvMain.setAdapter(boxAdapter);
        Button submit = (Button)findViewById(R.id.selectCon);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showResult(v);
            }
        });
    }

    void fillData() {
        cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null, null, null);
        while (cursor.moveToNext()) {
            name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

            phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            imgP =cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI));
            products.add(new contactStruct(""+name,phoneNumber,imgP,false));

        }
        cursor.close();


    }

    public void showResult(View view) {
        String result = "Selected Product are :";
        int totalAmount=0;
        ArrayList<contactStruct> prod = new ArrayList<contactStruct>();

        for (prod p : boxAdapter.getBox()) {
            if (p.box){
                result += "\n" + p.name;
                totalAmount+=p.price;
            }
        }
        Toast.makeText(this, result+"\n"+"Total Amount:="+totalAmount, Toast.LENGTH_LONG).show();
    }
}

Upvotes: 0

Views: 567

Answers (2)

trocchietto
trocchietto

Reputation: 2617

Did you try to make the class unknown as public? I suggest you post your class contactStruct. maybe is not in the same package, it looks like at compile time is not recognized, but is just an assumption, if does not work please post the entire error message

EDIT: OK so what you need to know is to specify the Object p.

Possibly is:

for (ListAdapter p : boxAdapter.getBox()) {

should work like this, the cycle for does not work like this you cannot pass prod or product to p.

For the future please study the official java documentation that teaches you how to use this feature

Upvotes: 1

ReDLaNN
ReDLaNN

Reputation: 524

The error seems to be is in showResult in which you have

(prod p : boxAdapter.getBox())

in the for loop prod should be the type of the object p (the Class) while prod seems to be the arraylist object, but the IDE should give you an error there, in fact Android Studio tells me (writing the same thing) Unknown class prod

Plus make sure the boxAdapter is iterable

        com.example.piku.SteaUd.ListAdapter(this, products);

should implement Iterable.

Upvotes: 0

Related Questions