2015-06-15 20 views
0

好吧,我已經在這裏搜索並試圖瞭解現有的問題,但有困難,所以我發佈這個。 在我的應用程序卡中顯示使用recyclerview。當用戶單擊任何項​​目時,其索引(int i)將傳遞給CardViewActivity,並顯示與該值匹配的卡片。 我試圖在列表中顯示數據卡<>。如果我將它保存在同一個文件中,我沒有任何問題。但是,我想將它保存在一個不同的通用文件中,並從其他類訪問它。我目前的代碼正在進入循環我猜,我不能看到它是什麼。從另一個班級獲取列表(Android)

請幫忙。

我Person.class是:

class Person { 
String name; 
String age; 
int photoId; 
List<Person> persons; 

Person(String name, String age, int photoId) { 
    this.name = name; 
    this.age = age; 
    this.photoId = photoId; 
} 
protected void initializePerson(){ 
    persons = new ArrayList<>(); 
    persons.add(new Person("Emma Wilson", "23 years old", R.drawable.emma)); 
    persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.lavery)); 
    persons.add(new Person("Lillie Watts", "35 years old", R.drawable.lillie)); 
    CardViewActivity cardViewActivity = new CardViewActivity(persons, this); 
    cardViewActivity.setDetails(); 

} 
} 

我試圖從CardViewActivity訪問:

public class CardViewActivity extends Activity { 

TextView personName; 
TextView personAge; 
ImageView personPhoto; 
List<Person> persons; 
Person person; 
int id; 




@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Intent intent = getIntent(); 
    id = intent.getIntExtra("position", 0); 
    Toast.makeText(this, "Message"+id, Toast.LENGTH_SHORT).show(); 
    //Person person = new Person(); 
    person.initializePerson(); 

    setContentView(R.layout.cardview_activity); 
    personName = (TextView)findViewById(R.id.person_name); 
    personAge = (TextView)findViewById(R.id.person_age); 
    personPhoto = (ImageView)findViewById(R.id.person_photo); 
    //personName.setText(persons.get(0).name); 
    setDetails(); 

} 

CardViewActivity(List<Person> persons, Person person){ 
    this.persons = persons; 
    this.person = person; 
} 
public void setDetails(){ 
//this.persons = persons; 
//Toast.makeText(this, "Inside setDetails() and id is "+i, Toast.LENGTH_SHORT).show(); 
//Toast.makeText(this, "Person name is "+persons.get(i).name, Toast.LENGTH_SHORT).show(); 
int i = id; 
personName.setText(persons.get(i).name); 
//personAge.setText(persons.get(i).age); 
//personPhoto.setImageResource(persons.get(i).photoId); 

} 

private void initializeData(){ 
    persons = new ArrayList<>(); 
    persons.add(new Person("Emma Wilson", "23 years old", R.drawable.emma)); 
    persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.lavery)); 
    persons.add(new Person("Lillie Watts", "35 years old", R.drawable.lillie)); 


} 
} 
+0

你可以讓列表爲靜態嗎? – clavio

+0

請詳細說明。 – swap

+0

根據你要做什麼,你可以使卡片列表是靜態的。你可以把它放在一個單獨的類,並從其他類像Class.cardList – clavio

回答

0

執行以下

public class SomeConstant { 
    public static List<String> someList = new ArrayList<String>(); 
    static { 
     someList.add("John"); 
     someList.add("adam"); 
    } 
} 

//在這裏你會調用你創建的列表 public class SomeClass {

public static void main(String[] args) { 
     System.out.println(SomeConstant.someList); 
    } 

} 
+0

和這有什麼好處? – swap

+0

並非必須如上所述......在代碼中,您可以將列表定義爲公共靜態,並且您應該能夠訪問其他類 – ASP