通过用户名从mysql获取数据到listview

时间:2017-05-04 14:13:58

标签: java php android mysql listview

我正在做一个Android应用程序,应用程序需要在Mysql的列表视图中显示所有用户访问过的地方。我可以显示所有数据,但我只需要登录用户名标记为他/她访问过的地方的数据。如何通过登录的用户名显示数据?谢谢。

这是我的php脚本

<?php
 $json = file_get_contents('php://input');

 $jsonData= json_decode($json);

$usr= $jsonData->username;

$strSQL ="SELECT * FROM userbeenthere WHERE username='$usr' ";
mysqli_set_charset($objConnect , 'utf8');

$objQuery = mysqli_query($objConnect, $strSQL);
$arrRows = array();
$arryItem = array();

while($arr = mysqli_fetch_array($objQuery)){
$arryItem["location"] = $arr["location"];

$arrRows[]= $arryItem;

}
echo json_encode($arrRows);


?>

这是我的ProfDataParser.class

public class ProfDataParser extends AsyncTask<Void,Void,Boolean>{
Context c;
String jsonData;
ListView lv;
ProgressDialog pd;
ArrayList<String> beenthere = new ArrayList<>();


    public ProfDataParser(Context c, String jsonData, ListView lv) {
        this.c = c;
        this.jsonData = jsonData;
        this.lv = lv;
    }
    @Override
    protected void onPreExecute () {
        super.onPreExecute();
        pd = new ProgressDialog(c);
        pd.setTitle("Parse");
        pd.setMessage("Pasring..Please wait");
        pd.show();
    }
    @Override
    protected Boolean doInBackground (Void...params){
        return this.parseData();
    }
    @Override
    protected void onPostExecute (Boolean result){
        super.onPostExecute(result);
        pd.dismiss();
        if (result) {
            ArrayAdapter adapter = new ArrayAdapter(c, android.R.layout.simple_list_item_1, beenthere);
            lv.setAdapter(adapter);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(c, beenthere.get(position), Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

private Boolean parseData()

{
    try {
        JSONArray ja = new JSONArray(jsonData);
        JSONObject jo;
        beenthere.clear();

        for (int i = 0; i < ja.length(); i++) {
            jo = ja.getJSONObject(i);
            String name = jo.getString("location");
            beenthere.add(name);
        }
        return true;
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return false;
}
}

这是我的beenthere.class

public class beenthere extends AppCompatActivity {
TextView textUsername;
final String TAG = this.getClass().getName();
SharedPreferences pref;
SharedPreferences.Editor editor;

final static String urlAddress="http://enyatravel.com/profile/beenthere.php";

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

    final ListView lv = (ListView) findViewById(R.id.lv_been);
    new ProfDownloader(beenthere.this, urlAddress, lv).execute();

    textUsername = (TextView) findViewById(R.id.username);

    pref = getSharedPreferences("login.conf", Context.MODE_PRIVATE);
    Log.d(TAG, pref.getString("username", ""));

    String username = pref.getString("username", "");
    textUsername.setText(username);


}
}

0 个答案:

没有答案