gpt4 book ai didi

android - 无法从我的异步任务访问我的 ListView

转载 作者:行者123 更新时间:2023-11-30 03:31:48 25 4
gpt4 key购买 nike

我正在尝试构建一个填充 ListView 的异步任务。当我尝试使用 findViewBYId 设置我的 listView 时:

ListView lv = (ListView) ((View) c).findViewById(R.id.tastelist);

我收到这个错误:

Cannot cast from Context to View

我的整个异步任务类是:

public class GetTasteJSON extends AsyncTask
<String, Void, String> {

Context c;

public GetTasteJSON(Context context)
{
c = context;
}

@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
return readJSONFeed(arg0[0]);
}

protected void onPostExecute(String result){

//decode json here
try{

JSONObject json = new JSONObject(result);

//acces listview
ListView lv = (ListView) ((View) c).findViewById(R.id.tastelist);

//make array list for beer
final List<BeerData> beerList = new ArrayList<BeerData>();

}
catch(Exception e){

}

}

public String readJSONFeed(String URL) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
try {
HttpResponse response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
inputStream.close();
} else {
Log.d("JSON", "Failed to download file");
}
} catch (Exception e) {
Log.d("readJSONFeed", e.getLocalizedMessage());
}
return stringBuilder.toString();
}

}

最佳答案

转换为

ListView lv = (ListView) ((Activity) c).findViewById(R.id.tastelist);

本例中的上下文应该是您的 Activity

关于android - 无法从我的异步任务访问我的 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17331620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com