gpt4 book ai didi

带有 Webview 的 Android JSON 自定义 ListView

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

我正在使用这个 tutorial .我正在尝试从 Json 获取图像。Listview 效果很好,当我单击 Listview(示例:第二行)时,我想在 webview 中查看 image2,但我有问题。

private static final String TAG = Jsonlol.class.getSimpleName();
private String url;
private List<ListItem> listItem = new ArrayList<ListItem>();
private ListView listView;
private CustomListAdapter adapter;
private Dialog webViewDialog;
private WebView webView;
private Button btClose;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.json_main);

url = getString(R.string.hellojson);

listView = (ListView) findViewById(R.id.listview);
adapter = new CustomListAdapter(this, listItem);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 0)
{
webView.loadUrl(" Image 1 HERE ");

webViewDialog = new Dialog(Jsonlol.this);
webViewDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
webViewDialog.setContentView(R.layout.wepbas);
webView = (WebView) webViewDialog.findViewById(R.id.wb_webview);
webViewDialog.show();


}
if(position == 1)
{
webView.loadUrl(" Image 2 HERE ");

webViewDialog = new Dialog(Jsonlol.this);
webViewDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
webViewDialog.setContentView(R.layout.wepbas);
webView = (WebView) webViewDialog.findViewById(R.id.wb_webview);
webViewDialog.show();


}
} });




JsonArrayPostRequest itemReq = new JsonArrayPostRequest(url,
new Response.Listener<JSONArray>() {

public void onResponse(JSONArray response) {

for (int i = 0; i < response.length(); i++) {
try {


JSONObject obj = response.getJSONObject(i);
ListItem item = new ListItem();
item.setTitle(obj.getString("title"));
item.setThumbnailUrl(obj.getString("image"));
item.setrlevel(obj.getString("rlevel"));

listItem.add(item);

} catch (JSONException e) {
e.printStackTrace();
}

}

adapter.notifyDataSetChanged();
}


}, new Response.ErrorListener() {

public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());

}
});

AppController.getInstance().addToRequestQueue(itemReq);
}

@Override
public void onDestroy() {
super.onDestroy();
}

这里是 Json

[{
"id": "1",
"title": "Test 1",
"image": "http://api.androidhive.info/json/movies/1.jpg",
"image2": "http://api.androidhive.info/json/movies/1.jpg",
"rlevel": "Test 1"
},
{
"id": "2",
"title": "Test 2",
"image": "http://api.androidhive.info/json/movies/2.jpg",
"image2": "http://api.androidhive.info/json/movies/2.jpg",
"rlevel": "Test 2"
}]

最佳答案

你在错误的地方调用了 webView

if(position == 0)
{
webViewDialog = new Dialog(Jsonlol.this);
webViewDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
webViewDialog.setContentView(R.layout.wepbas);
webView = (WebView) webViewDialog.findViewById(R.id.wb_webview);
webView.loadUrl(listItem.get(0).getThumbnailUrl());
webViewDialog.show();
}
if(position == 1)
{
webViewDialog = new Dialog(Jsonlol.this);
webViewDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
webViewDialog.setContentView(R.layout.wepbas);
webView = (WebView) webViewDialog.findViewById(R.id.wb_webview);
webView.loadUrl(listItem.get(1).getThumbnailUrl());
webViewDialog.show();
}

但是如果您的目标只是在用户点击您的列表项时显示图像,您可以尝试使用 picasso 将图像显示给 ImageView , ion还有这种图书馆。

关于带有 Webview 的 Android JSON 自定义 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36483138/

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