我从api获取了数据,但我不知道如何将这些获取的数据分配给我的适配器?
问题:
如何将获取的数据设置为上面定义的数组?
如何设置适配器?
公共类FetchLists扩展了AsyncTask<> {
public List<MailChimpList> listTitles = new ArrayList<>();
@Override
protected List<MailChimpList> doInBackground(Integer... params) {
int count = params[0];
int offset = params[1];
String urlString = "https://us14.api.mailchimp.com/lists?apikey=efb918ee8791215bac4c8a3a8a77-us14"
urlString = urlString + "&count=" + count + "&offset=" + offset;
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream stream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String line = reader.readLine();
String response = "";
while (line != null) {
response += line;
line = reader.readLine();
}
JSONObject object = new JSONObject(response);
JSONArray lists = object.getJSONArray("lists");
for (int i = 0; i < lists.length(); i++) {
JSONObject listData = (JSONObject) lists.get(i);
MailChimpList mailChimpList = new MailChimpList();
mailChimpList.id = listData.getString("id");
mailChimpList.title = listData.getString("name");
String id = listData.getString("id");
String title = listData.getString("name");
Log.d("ashutosh","id are: "+id);
Log.d("ashutosh","list name are: "+title);
listTitles.add(mailChimpList);
}
} catch (Exception e) {
e.printStackTrace();
}
return listTitles;
}
@Override
protected void onPostExecute(List<MailChimpList> mailChimpLists) {
}
}
答案 0 :(得分:1)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/recycler_containers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"/>
</LinearLayout>
</ScrollView>
由于您的列表是自定义列表,您需要编写客户适配器
您可以将ListView声明为全局变量,并且可以在后执行中访问它。