我一直在複製粘貼不同的代碼到我的應用程序(因爲我剛剛開始編碼),並從朋友的一些幫助。但我不想打擾他們。從其他活動接收數據ArrayList [Java]
我的問題是,我不知道如何打開在該類的底部上創建的ArrayList到另一個類中,他將值分成字符串,稍後將傳遞,最後應該在我的Youtube API。
首先吸氣,這是第一個活動(LesSelectionActivity.java)
public void getLessons(){
APIClient.get(url, new JsonHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject object) {
if (object != null) {
try {
JSONArray jsonLesson = object.getJSONArray("sqllessons");
mLessons = new ArrayList<Lesson>();
for (int Index = 0; Index < jsonLesson.length(); Index++) {
try {
JSONObject jsonLes = jsonLesson.getJSONObject(Index);
Lesson lesson = new Lesson();
lesson.id = jsonLes.getInt("_id");
lesson.number = jsonLes.getInt("lesnumber");
lesson.videoId = jsonLes.getString("videourl");
System.out.println("data test: " + lesson.id);
mLessons.add(lesson);
} catch (JSONException e) {
e.printStackTrace();
return;
}
}
} catch (JSONException e) {
e.printStackTrace();
return;
}
}
setButtonLessons();
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
Toast.makeText(LesSelectionActivity.this, "FAIL", Toast.LENGTH_LONG).show();
}
});
}
public ArrayList<Lesson> getArrayLesson() {
return mLessons;
}
我希望它在我的「DataFiller」 它沒有XML /佈局打開,並應作爲一個適配器行爲以上活動。
public class DataFiller{
public static ArrayList<Lesson> readLessons(int given) {
ArrayList<Lesson> lessons = new ArrayList<Lesson>();
for (int i = (given * 25) - 24; i <= 25 * given; i++) {
Lesson lesson = new Lesson();
lesson.setId(i);
lessons.add(lesson);
}
return lessons;
}
上面的腳本只是一個空的數組列表,所以我可以測試列表視圖。但是這應該從其他活動編輯到'新'ArrayList。
我不確定你是否需要它,但這裏是課程模型。
public class Lesson implements Serializable {
public int id;
public int number;
public String videoId;
public Lesson() {
}
public Lesson(Lesson lesson) {
number = lesson.number;
id = lesson.id;
videoId = lesson.videoId;
}
public int getId() { return id; }
public void setId(int id) {
this.id = id;
}
public String getVideoId() {
return videoId;
}
public void setVideoId(String videoId) {
this.videoId = videoId;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
我一直在尋找這整個一天的awnser如何使用ArrayList的,但mayby由於缺乏知識的人,我沒有使用正確的搜索字詞。
PS。你是如何學習數據傳輸的(如JSON和ArrayLists)。只是在做和嘗試?或者你有在線課程什麼的?我已經嘗試了基本的Android Studio課程,但是他們堅持了很長的基礎知識之後就失去了興趣。但是我注意到,使用這個應用程序會得到多遠,我堅持的更多(我認爲是)簡單的編碼?
好了,所以首先你必須把整個來龍去脈此,因爲我真的不明白什麼是第一類,什麼是第二類,但我會假設第一個是活動,第二個是適配器,你必須做的是在獲得你的數組列表後,將它傳遞給你的Adapter,如何?創建一個方法來設置它或通過構造函數本身傳遞它,如果你需要從一個活動傳遞給另一個活動,你將不得不通過這個意圖來完成它,請多說明一下你的代碼的上下文,我會感謝你的幫助。 – Eefret
看來第一堂課有一個你可以保存Arraylist的領域。還有你定義的getArrayLesson()方法只返回你想要的數組。你能粘貼更多的代碼嗎? – marco
我想告訴你的代碼,但我不知道你正在尋找哪個代碼。 getArrayLesson()在setButtonLessons上被調用,但那還沒有工作。 – Jaxhead