gpt4 book ai didi

php - 如何获取ListView中的数据取决于用户?

转载 作者:行者123 更新时间:2023-11-29 11:06:18 25 4
gpt4 key购买 nike

如何根据登录的用户从ListView中的mysql数据库获取数据。下面是我得到的Json:

{"android":[{"username":"lokesh","company":"ffff","client":"xyz","client_no":"5487968475","callback":"2016-12-29 11:30:00"},
{"username":"Rahul","company":"yyyy","client":"Suresh","client_no":"5789486589","callback":"2016-12-16 15:14:00"},
{"username":"Fayaz","company":"ddddd","client":"Haresh","client_no":"4444889955","callback":"2016-12-28 14:00:00"},
{"username":"Rahul","company":"qqqqq","client":"gore","client_no":"4444889955","callback":"2016-12-28 14:00:00"}]}

在这里,我得到了上面的 Json,它是数据库中存在的完整数据,我在 ListView 中获取了所有 4 个条目,但我真正想要的是,如果我通过 Rahul 用户名登录应用程序,那么我应该只能看到 2 条记录。如果我以 Fayaz 身份登录,那么我应该只能在 ListView 中看到 1 条记录。任何人都可以帮助我......下面是我的代码: Activity :

public class AboutUsActivity extends AppCompatActivity implements LoadJSONTask.Listener, AdapterView.OnItemClickListener {

private ListView mListView;


public static final String URL = "http://10.0.2.2/android/get.php";
private List<HashMap<String, String>> mAndroidMapList = new ArrayList<>();

private static final String KEY_COMPANY = "company";
private static final String KEY_CLIENT = "client";
private static final String KEY_CLIENT_NO = "client_no";
private static final String KEY_CALLBACK = "callback";

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

mListView = (ListView) findViewById(R.id.list_view);
mListView.setOnItemClickListener(this);
new LoadJSONTask(this).execute(URL);
}

@Override
public void onLoaded(List<AndroidVersion> androidList) {

for (AndroidVersion android : androidList) {

HashMap<String, String> map = new HashMap<>();

map.put(KEY_COMPANY, android.getCompany());
map.put(KEY_CLIENT, android.getClient());
map.put(KEY_CLIENT_NO, android.getClient_no());
map.put(KEY_CALLBACK, android.getCallback());

mAndroidMapList.add(map);
}

loadListView();
}

@Override
public void onError() {

Toast.makeText(this, "Error !", Toast.LENGTH_SHORT).show();
}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

Toast.makeText(this, mAndroidMapList.get(i).get(KEY_CLIENT_NO),Toast.LENGTH_LONG).show();
}

private void loadListView() {

ListAdapter adapter = new SimpleAdapter(AboutUsActivity.this, mAndroidMapList, R.layout.list_item,
new String[] { KEY_COMPANY, KEY_CLIENT, KEY_CLIENT_NO, KEY_CALLBACK },
new int[] { R.id.company,R.id.client, R.id.client_no,R.id.callback });

mListView.setAdapter(adapter);

}
}

LoadJSONTask 类:

public class LoadJSONTask extends AsyncTask<String, Void, Response> {

public LoadJSONTask(Listener listener) {

mListener = listener;
}

public interface Listener {

void onLoaded(List<AndroidVersion> androidList);

void onError();
}

private Listener mListener;

@Override
protected Response doInBackground(String... strings) {
try {

String stringResponse = loadJSON(strings[0]);
Gson gson = new Gson();

return gson.fromJson(stringResponse, Response.class);
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (JsonSyntaxException e) {
e.printStackTrace();
return null;
}
}

@Override
protected void onPostExecute(Response response) {

if (response != null) {

mListener.onLoaded(response.getAndroid());

} else {

mListener.onError();
}
}

private String loadJSON(String jsonURL) throws IOException {

URL url = new URL(jsonURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
StringBuilder response = new StringBuilder();

while ((line = in.readLine()) != null) {

response.append(line);
}

in.close();
return response.toString();
}
}

PHP 文件:

<?php 
session_start();
$sql = "SELECT * FROM meetdetails";
require_once('dbConnect.php');
$r = mysqli_query($con,$sql);
$android = array();
while($row = mysqli_fetch_array($r)){
array_push($android,array(
'username'=>$row['username'],
'company'=>$row['company_name'],
'client'=>$row['client_name'],
'client_no'=>$row['client_no'],
'callback'=>$row['callback']
));
}

echo json_encode(array('android'=>$android));
mysqli_close($con);
?>

最佳答案

您可以将用户名发送到您的服务器,他们将向您提供用户特定的请求,但您也必须要求他们像这样实现。或者创建HasmMap>将用户特定数据存储在hasmap中,其中hasmap的KEY应该是username或user_id,然后您可以使用get方法从hasmap中获取特定用户数据。

关于php - 如何获取ListView中的数据取决于用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41344426/

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