我有三个通用类列表
1 List< Board >
2 List< Class >
3 List< Subject >
在运行期间,我可以返回任何类型的列表,我将如何获得特定列表并显示它?
@Override
public Loader<List<Board>> onCreateLoader(int id, Bundle args) {
String s=null;
if (args!=null)
s = args.getString("string_board_id");
if (id==1) {
return new BoardLoader(this, boardUrl);
}
if (id==2){
return new ClassLoader(this, boardUrl,s);
}
return null;
}
@Override
public void onLoadFinished(Loader<List<Board>> loader, List<Board> data) {
if (data !=null && data.size() != 0){
boardAdapter.addAll(data);
}
else{
Log.e(LOG_TAG,"problem with board list ");
}
}
提前感谢。
答案 0 :(得分:0)
不要在Activity上实现3个回调,而是生成3个LoaderManager.LoaderCallbacks<Type>
个对象。在引用活动的instad时使用这些对象。
在loader.getId()
内使用onLoadFinished()
查看哪个加载程序已完成,以防您想使用带有2个加载器的加载程序回调。
这样的事情:
LoaderManager.LoaderCallbacks<String> mStringCallbacks = new LoaderManager.LoaderCallbacks<String>() {
@Override
public Loader<String> onCreateLoader(int id, Bundle args) {
return null;
}
@Override
public void onLoadFinished(Loader<String> loader, String data) {
}
@Override
public void onLoaderReset(Loader<String> loader) {
}
};
答案 1 :(得分:0)
这是我的家庭活动
package com.directionclasses.directionclasses;
import android.app.LoaderManager;
import android.content.Loader;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class HomeActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<Object>> {
public static final String LOG_TAG = HomeActivity.class.getName();
Spinner spinner;
Spinner spinnerClass;
Spinner spinnerSubject;
private static final int mBoardLoaderManager=1;
private static final int mClassLoaderManager=2;
private static final int mSubjectLoaderManager=3;
private static final String boardUrl = "http://directionclasses.com/Android/selectBoard.php";
private static final String classUrl = "http://directionclasses.com/Android/selectClass.php";
private static final String subjectUrl = "http://directionclasses.com/Android/selectSubject.php";
BoardAdapter boardAdapter = null;
ClassAdapter classAdapter = null;
SubjectAdapter subjectAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar mToolBar=(Toolbar)findViewById(R.id.toolbar);
mToolBar.setTitle(getString(R.string.app_name));
// mToolBar.setNavigationIcon(R.drawable.ic_launcher);
mToolBar.inflateMenu(R.menu.menu_main);
android.app.LoaderManager loaderManager=getLoaderManager();
loaderManager.initLoader(mBoardLoaderManager,null,this);
spinner = (Spinner) findViewById(R.id.spinner1);
spinnerClass = (Spinner) findViewById(R.id.spinner2);
spinnerSubject = (Spinner) findViewById(R.id.spinner3);
boardAdapter = new BoardAdapter(this, new ArrayList<Board>());
classAdapter = new ClassAdapter(this, new ArrayList<Class>());
subjectAdapter = new SubjectAdapter(this, new ArrayList<Subject>());
spinner.setAdapter(boardAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Board board = boardAdapter.getItem(position);
int board_id = board.getBoard_id();
if (board_id != 0) {
Toast.makeText(HomeActivity.this, "jai ho" + board_id, Toast.LENGTH_SHORT).show();
String string_board_id = String.valueOf(board_id);
Bundle b = new Bundle();
b.putString("string_board_id", string_board_id);
android.app.LoaderManager loaderManager=getLoaderManager();
loaderManager.initLoader(mClassLoaderManager,b,HomeActivity.this);
} else {
Toast.makeText(HomeActivity.this, "jai ho with no id", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@Override
public Loader<List<Object>> onCreateLoader(int id, Bundle args) {
String s=null;
if (args!=null) {
s = args.getString("string_board_id");
}
if (id==1) {
return new BoardLoader(this, boardUrl);
}
if (id==2){
return new ClassLoader(this, classUrl,s);
}
return null;
}
@Override
public void onLoadFinished(Loader<List<Object>> loader, List<Object> data) {
int loader_id=loader.getId();
if (loader_id==1){
if (data !=null && data.size() != 0){
List<Board> mythings = (List<Board>) (Object) data;
boardAdapter.addAll(mythings);
}
else{
Log.e(LOG_TAG,"problem with board list ");
}
}
if (loader_id==2){
if (data !=null && data.size() != 0){
List<Class> mythings = (List<Class>) (Object) data;
classAdapter.addAll(mythings);
}
else{
Log.e(LOG_TAG,"problem with class list ");
}
}
if (loader_id==3){
if (data !=null && data.size() != 0){
List<Subject> mythings = (List<Subject>) (Object) data;
subjectAdapter.addAll(mythings);
}
else{
Log.e(LOG_TAG,"problem with subject list ");
}
}
}
@Override
public void onLoaderReset(Loader<List<Object>> loader) {
Log.v(LOG_TAG,"this is the on Load Reset method");
// boardAdapter.clear();
}
}
这是我的BoardLoader.java文件
package com.directionclasses.directionclasses;
import android.content.AsyncTaskLoader;
import android.content.Context;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
public class BoardLoader extends AsyncTaskLoader {
private static final String LOG_TAG = BoardLoader.class.getName();
private String mUrl;
public BoardLoader(Context context, String url) {
super(context);
mUrl = url;
}
@Override
public List<Board> loadInBackground() {
ArrayList<Board> boards1 = new ArrayList<Board>();
URL url1 = null;
try {
url1 = new URL(mUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
Log.e(LOG_TAG, "this is url malformed exception");
}
BufferedReader reader = null;
String text = null;
try {
URLConnection conn = url1.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.flush();
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
text = sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
boards1 = featuresExtractFromJson(text);
return boards1;
}
@Override
protected void onStartLoading() {
forceLoad();
}
private ArrayList<Board> featuresExtractFromJson(String JsonResponse) {
ArrayList<Board> results = new ArrayList<Board>();
try {
JSONArray root = new JSONArray(JsonResponse);
for (int i = 0; i < root.length(); i++) {
JSONObject jsonObject = root.getJSONObject(i);
String board_id = jsonObject.getString("board_id");
String board_name = jsonObject.getString("board_name");
int parse_board_id = Integer.parseInt(board_id);
results.add(new Board(parse_board_id, board_name));
}
} catch (JSONException e) {
e.printStackTrace();
Log.e(LOG_TAG, "Json execption");
}
return results;
}
}
这是我的Board Adapter.java文件
public class BoardAdapter extends ArrayAdapter<Board> {
public BoardAdapter(Context context, ArrayList<Board> boards) {
super(context, 0, boards);
}
public BoardAdapter(@NonNull Context context, @LayoutRes int resource, ArrayList<Board> boards) {
super(context, resource);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.row, parent, false);
}
Board board = getItem(position);
int board_id = board.getBoard_id();
String board_name = board.getBoard_name();
TextView text_name = (TextView) listItemView.findViewById(R.id.tv1);
text_name.setText(board_name);
// TextView text_id = (TextView) listItemView.findViewById(R.id.tv2);
// text_id.setText(board_id+"");
return listItemView;
}
}
这是我的ClassLoader.java文件
public class ClassLoader extends AsyncTaskLoader {
private static final String LOG_TAG = ClassLoader.class.getName();
private String mUrl;
private String borad_id=null;
public ClassLoader(Context context, String url,String borad_id1 ) {
super(context);
mUrl = url;
borad_id=borad_id1;
}
@Override
public Object loadInBackground() {
String data = null;
try {
data = URLEncoder.encode("board_id", "UTF-8") + "=" + URLEncoder.encode(borad_id, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
ArrayList<Class> boards1 = new ArrayList<Class>();
URL url1 = null;
try {
url1 = new URL(mUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
Log.e(LOG_TAG, "this is url malformed exception");
}
BufferedReader reader = null;
String text = null;
try {
URLConnection conn = url1.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
text = sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
boards1 = featuresExtractFromJson(text);
return boards1;
}
@Override
protected void onStartLoading() {
forceLoad();
}
private ArrayList<Class> featuresExtractFromJson(String JsonResponse) {
ArrayList<Class> results = new ArrayList<Class>();
try {
JSONArray root = new JSONArray(JsonResponse);
for (int i = 0; i < root.length(); i++) {
JSONObject jsonObject = root.getJSONObject(i);
String calss_id = jsonObject.getString("calss_id");
String class_name = jsonObject.getString("class_name");
String board_id = jsonObject.getString("board_id");
int parse_board_id = Integer.parseInt(board_id);
int parse_class_id = Integer.parseInt(calss_id);
results.add(new Class(parse_class_id, class_name, parse_board_id));
}
} catch (JSONException e) {
e.printStackTrace();
Log.e(LOG_TAG, "Json execption");
}
return results;
}
}
这是我的ClassAdapter.java文件
public class ClassAdapter extends ArrayAdapter<Class> {
public static final String LOG_TAG = ClassAdapter.class.getName();
public ClassAdapter(Context context, ArrayList<Class> classes) {
super(context, 0, classes);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.row, parent, false);
}
Class classes = getItem(position);
int class_id = classes.getClass_id();
String class_name = classes.getClass_name();
Log.e(LOG_TAG,"string class_name"+class_name);
TextView text_name = (TextView) listItemView.findViewById(R.id.tv1);
text_name.setText(class_name);
// TextView text_id = (TextView) listItemView.findViewById(R.id.tv2);
// text_id.setText(board_id+"");
return listItemView;
}
}
在此代码之后我的电路板值来自sever.u可以在下面的链接中看到 enter image description here
但oniteemclick我的类值是通过调试我的应用程序从服务器上看到它们的主板上相应的。但我的clas值并没有显示在我的班级微调器中。