gpt4 book ai didi

android - 修复 arraylist 的适配器构造函数

转载 作者:行者123 更新时间:2023-11-30 01:51:50 27 4
gpt4 key购买 nike

我正在学习android,我有这个错误,我不知道如何解决

错误构造函数在适配器getdata()的参数中未定义

   adapter = new GridViewAdapter(this, R.layout.grid_item_layout, getData());

我知道 arraylist 的 Gridviewadapter 的构造函数应该被修复..但是如何?

public GridViewAdapter(Context context, int layoutResourceId, ArrayList<Listitem> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.mcontext =context;
}

这是代码

protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);

for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
String id = c.getString(TAG_ID);
String url = c.getString(TAG_URL);
Listitem.add(new Listitem(id,url));
}

adapter = new GridViewAdapter(this, R.layout.grid_item_layout, getData());

list.setAdapter(adapter);

获取数据()

公共(public)无效 getData(){ GetDataJSON 类扩展了 AsyncTask{

    @Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://justedhak.comlu.com/get-data.php");

// Depends on your web service
httppost.setHeader("Content-type", "application/json");

InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();

String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}

@Override
protected void onPostExecute(String result){
myJSON=result;
showList();
}

最佳答案

你的代码应该是这样的。

要从服务器获取数据,请从您的 oncreate 方法调用此行

GetDataJSON .execute();

你的类(class)将是这样的。

public class GetDataJSON extends AsyncTask{
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://justedhak.comlu.com/get-data.php");

// Depends on your web service
httppost.setHeader("Content-type", "application/json");

InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();

String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}

@Override
protected void onPostExecute(String result){
myJSON=result;
showList();
}
}

你的方法 showList 就像。

protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);

for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
String id = c.getString(TAG_ID);
String url = c.getString(TAG_URL);
Listitem.add(new Listitem(id,url));
}

adapter = new GridViewAdapter(this, R.layout.grid_item_layout, Listitem);

list.setAdapter(adapter);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

关于android - 修复 arraylist 的适配器构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32923243/

27 4 0