我一直在关注一些教程来检索json数据并使用volley将其添加到listview中。我一直陷入这个错误试图解决它一段时间,所以我不得不寻求一些帮助。我尝试了很多例子,这个看起来最直观,但我不能通过这个错误,谢谢
这是我的模特课
public class Trilhos {
private int id;
private String titulo;
private int id_user;
private String dificuldade;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitulo() {
return titulo;
}
public void setTitulo(String titulo) {
this.titulo = titulo;
}
public int getId_user() {
return id_user;
}
public void setId_user(int id_user) {
this.id_user = id_user;
}
public String getDificuldade() {
return dificuldade;
}
public void setDificuldade(String dificuldade) {
this.dificuldade = dificuldade;
}
}
我的适配器
public class AdapterTrilhos extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Trilhos> triList;
public AdapterTrilhos(List<Trilhos> triList, Activity activity) {
this.activity = activity;
this.triList = triList;
}
@Override
public int getCount() {
return this.triList.size();
}
@Override
public Object getItem(int position) {
return this.triList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.dadoslista, null);
TextView idtxt = (TextView)
convertView.findViewById(R.id.txtid);
TextView tittxt = (TextView)
convertView.findViewById(R.id.txttit);
TextView usertxt = convertView.findViewById(R.id.txtuser);
TextView diftxt = (TextView)
convertView.findViewById(R.id.txtdif);
Trilhos t = triList.get(position);
idtxt.setText(t.getId());
tittxt.setText(t.getTitulo());
usertxt.setText(t.getId_user());
diftxt.setText(t.getDificuldade());
return convertView;
}
}
我的main_activity(listarTrilhos)
public class ListarTrilhos extends Activity {
private static final String
urlget="http://192.168.1.68/loginsignup/getTrilhos.php";
private ListView listView;
private AdapterTrilhos adapter;
private List<Trilhos> TrilhosList = new ArrayList<Trilhos>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listatrilhos);
listView=findViewById(R.id.listv);
adapter=new AdapterTrilhos(this,TrilhosList);
listView.setAdapter(adapter);
criarLista();
}
public void criarLista(){
RequestQueue mRequestQueue =
Volley.newRequestQueue(getApplicationContext());
JsonArrayRequest request = new JsonArrayRequest(
urlget,
new Response.Listener<JSONArray>(){
@Override
public void onResponse(JSONArray response) {
int count = response.length();
for(int i = 0; i < count; i++){
try {
JSONObject jo = response.getJSONObject(i);
Trilhos tri=new Trilhos();
tri.setId(jo.getInt("id"));
tri.setTitulo(jo.getString("titulo"));
tri.setId_user(jo.getInt("id_user"));
tri.setDificuldade(jo.getString("dificuldade"));
TrilhosList.add(tri);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Toast.makeText(getApplicationContext(), response.toString(),
Toast.LENGTH_LONG).show();
adapter.notifyDataSetChanged();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "onErrorResponse",
Toast.LENGTH_LONG).show();
}
}
);
Object TAG_REQUEST_QUEUE = new Object();
request.setTag(TAG_REQUEST_QUEUE);
mRequestQueue.add(request);
mRequestQueue.start();
}
}
答案 0 :(得分:2)
我没有阅读你的所有代码,但注意到你有构造函数
public AdapterTrilhos(List<Trilhos> triList, Activity activity) {
this.activity = activity;
this.triList = triList;}
将adapter=new AdapterTrilhos(this, TrilhosList);
更改为
adapter=new AdapterTrilhos(TrilhosList, this);