抱歉,我的问题很简单。我有一个片段,需要实现接口侦听器。
package com.tumta.henrique.teste;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.tumta.henrique.teste.ConsultaEntidades.*;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class EntidadeFragment extends Fragment implements ConsultaConcluidaListener {
private static final String ARG_SECTION_NUMBER = "section_number";
public EntidadeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_entidade, container, false);
new ConsultaEntidades((ConsultaEntidades)getActivity()).execute();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
public static EntidadeFragment newInstance(int sectionNumber){
EntidadeFragment frag = new EntidadeFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
frag.setArguments(args);
return frag;
}
@Override
public void onConsultaConcluida(List<String> result) {
ListView listaEntidades = (ListView) getView().findViewById(R.id.listaentidades);
ArrayAdapter arrayAdapter = new ArrayAdapter<>(getView().getContext(),android.R.layout.simple_list_item_1, result);
listaEntidades.setAdapter(arrayAdapter);
}
}
我需要在活动时使用它:
new ConsultaEntidades(this).execute();
但是现在我需要在Frag中使用同一行,但是我不能使用this
,当我使用new ConsultaEntidades((ConsultaEntidades)getActivity()).execute();
时它显示〜无法转换FragmentActivity咨询ConsultaEntidades <br><br>
ConsultaEntidades`是我的课程:package com.tumta.henrique.teste;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v4.app.FragmentActivity;
public class ConsultaEntidades extends AsyncTask<Void, Void, List<String>> {
private ConsultaConcluidaListener listener;
private static final String URL_STRING = "http://192.168.0.14:7001/com.henrique.rest/api/v1/status/entidade/";
public ConsultaEntidades(ConsultaConcluidaListener listener){
this.listener = listener;
}
@Override
protected List<String> doInBackground(Void... arg0) {
try {
String resultado = ConsultaServidor();
return InterpretaResultado(resultado);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private List<String> InterpretaResultado(String resultado) throws JSONException {
JSONObject object = new JSONObject(resultado);
JSONArray jsonArray = object.getJSONArray("entidade");
List<String> listaNomes = new ArrayList<String>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonWaether = jsonArray.getJSONObject(i);
//int id = jsonWaether.getInt("ent_id");
String nome = jsonWaether.getString("ent_nome");
listaNomes.add(i, nome);
}
return listaNomes;
}
private String ConsultaServidor() throws IOException {
InputStream is = null;
try {
URL url = new URL(URL_STRING);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(10000);
conn.setReadTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
conn.getResponseCode();
is = conn.getInputStream();
Reader reader = null;
reader = new InputStreamReader(is);
char[] buffer = new char[2048];
reader.read(buffer);
return new String(buffer);
} finally {
if (is != null) {
is.close();
}
}
}
@Override
protected void onPostExecute(List<String> result) {
listener.onConsultaConcluida(result);
super.onPostExecute(result);
}
public interface ConsultaConcluidaListener {
void onConsultaConcluida(List<String> result);
}
}
因此,您注意到我需要在碎片中实现该接口。我怎样才能做到这一点?
最佳答案
您的onCreateView
中有两个错误
您要在返回后实例化AsyncTask
您将getActivity()
强制转换为接口,而没有Activity
实施它。
它应该看起来像:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
new ConsultaEntidades(this).execute();
return inflater.inflate(R.layout.fragment_entidade, container, false);
}