gpt4 book ai didi

java - Android ListView - 单击即可转至网站

转载 作者:行者123 更新时间:2023-12-01 08:59:42 26 4
gpt4 key购买 nike

抱歉,如果问题有点新闻类型,但 Android 不了解我:(

所以我有填充 TextView 的 MainActivity.Java。我使用 JSON 从服务器获取数据,它提供了 id、用户名、消息、日期时间、状态、类型等参数。我想要的是每次点击列表项时,它都会进入网站,如下所示:

http://my_web_site_url/json?id=id_of_the_list_item&status=something

即,当用户单击单个列表项时,URL 会使用 JSON 检索到的每个列表项的相同 ID 进行重定向。

如何设置并获取每个list_item_id以及如何访问网站?

感谢任何帮助!

我的代码是:

package something;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

private String TAG = MainActivity.class.getSimpleName();

private ProgressDialog pDialog;
private ListView lv;

// URL to get contacts JSON
private static String url = "my_website_that_returns_json_data";

ArrayList<HashMap<String, String>> contactList;

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

contactList = new ArrayList<>();

lv = (ListView) findViewById(R.id.list);
new GetContacts().execute();
}

/**
* Async task class to get json by making HTTP call
*/
private class GetContacts extends AsyncTask<Void, Void, Void> {

@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();

}

@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();

// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);

Log.e(TAG, "Response from url: " + jsonStr);

if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);

// Getting JSON Array node
JSONArray result = jsonObj.getJSONArray("result");

// looping through All Contacts
for (int i = 0; i < result.length(); i++) {
JSONObject c = result.getJSONObject(i);

String id = c.getString("id");
String username = c.getString("username");
String message = c.getString("message");
String date_status = "Date: " + c.getString("date_time") + " Status: " + c.getString("status");


// tmp hash map for single contact
HashMap<String, String> result1 = new HashMap<>();

// adding each child node to HashMap key => value
result1.put("id", id);
result1.put("username", username);
result1.put("message", message);
result1.put("date_status", date_status);

// adding contact to contact list
contactList.add(result1);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});

}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});

}

return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */

ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.list_item, new String[]{"username", "message",
"date_status"}, new int[]{R.id.username,
R.id.message,R.id.date_status});

lv.setAdapter(adapter);

}

}
}

最佳答案

在 ListView 中使用 onItemClickListener

使用内部类的示例

 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick((AdapterView<?> parent,
View view,
int position,
long id)) {


String item = lv.getItemAtPosition(position);
Toast.makeText(this,"You selected : " + item,Toast.LENGTH_SHORT).show();


}
});

在您的方法中,您将获得数据的位置并加载链接。

关于java - Android ListView - 单击即可转至网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41783561/

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