如何根据用户登录从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的用户名登录app,那么我应该只能看到2条记录。如果我以Fayaz身份登录,那么我应该只能在listview中看到1条记录。任何人都可以帮助我..以下是我的代码: 活动:
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 FIle:
<?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);
?>
答案 0 :(得分:1)
您可以将用户名发送到您的服务器,在那里他们将为您提供用户特定的请求,但您也必须要求他们执行此类操作。或创建HasmMap&gt;在hasmap中存储用户特定数据,其中hasmap的KEY应该是username或user_id,然后你可以使用get方法从hasmap获取特定的用户数据。
答案 1 :(得分:0)
将SQL结果保存在常规var中,然后在创建ListView之前根据用户名过滤数据。 对于每个登录的用户名,过滤器仅代表他的数据。
其他选项是过滤适配器内的dada,再次根据连接的用户名隐藏不相关的项目。