尝试从列表视图中获取存储在类数组中的所有选定值。当我尝试从该类中获取并尝试在函数中创建对象时,我无法使用它。这是代码
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();
}
它在onCreate内。它说未知的类产品。请帮忙。是的,它在同一个包装上。这是班级
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;
}
}
这是我的完整代码
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();
}
}
你是否试图让这个班级不为公众?我建议你发布你的课程contactStruct。也许不是在同一个包中,看起来在编译时看不到,但只是一个假设,如果不起作用请发布整个错误信息
编辑:好的,你需要知道的是指定对象p。
可能是:
for (ListAdapter p : boxAdapter.getBox()) {
应该这样工作,周期不能像这样工作你不能通过prod
或product
到p。
为了将来,请学习官方的java文档,教你如何使用这个feature
错误似乎是在你所拥有的showResult
(prod p : boxAdapter.getBox())
在for循环中,prod
应该是对象p(Class)的类型,而prod似乎是arraylist对象,但IDE应该在那里给你一个错误,实际上Android Studio告诉我(写同样的东西)Unknown class prod
另外,请确保boxAdapter是可迭代的
com.example.piku.SteaUd.ListAdapter(this, products);
应该实现Iterable。