如何在抽屉导航活动的片段中显示数据库中的列表视图

时间:2016-04-24 23:36:50

标签: android listview fragment

您能帮助我在片段(抽屉导航活动)中显示数据库中的列表视图吗?我尝试了这个,但它不能用于片段。

这是我的文件fragment3.java

public class fragment3 extends Fragment {
private String JSON_STRING;
private ListView listView;
public static final String URL="http://10.0.3.2/Scripts/select.php";
public static final String TAG_JSON_ARRAY="result";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment3, container, false);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment3);
        listView = (ListView) findViewById(R.id.listView);
        listView.setOnItemClickListener(this);
        getJSON();
    }
private void display(){
    JSONObject jsonObject = null;
    ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
    try {
        jsonObject = new JSONObject(JSON_STRING);
        JSONArray result = jsonObject.getJSONArray(TAG_JSON_ARRAY);

        for(int i = 0; i<result.length(); i++){
            JSONObject jo = result.getJSONObject(i);
            String id= jo.getString("id");
            String name = jo.getString("name");
            String email= jo.getString("email");

            HashMap<String,String> liste= new HashMap<>();
            // liste.put("Type",type);
            liste.put("id",id);
            liste.put("email",email);
            list.add(liste);
        }

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

    ListAdapter adapter = new SimpleAdapter(
            fragment3.this, list, R.layout.listView,
            new String[]{"id","email"},
            new int[]{R.id.id, R.id.name});

    listView.setAdapter(adapter);
}

private void getJSON(){
    class GetJSON extends AsyncTask<Void,Void,String> {

        ProgressDialog loading;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(fragment3.this,"Fetching Data","Wait...",false,false);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            JSON_STRING = s;
            display();
        }

        @Override
        protected String doInBackground(Void... params) {
            RequestHandler rh = new RequestHandler();
            String s = rh.sendGetRequest(URL);
            return s;
        }
    }
    GetJSON gj = new GetJSON();
    gj.execute();
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Intent intent = new Intent(this, ViewUser.class);
    HashMap<String,String> map =(HashMap)parent.getItemAtPosition(position);
    String tid= map.get("id").toString();
    intent.putExtra("Type", tid);
    startActivity(intent);
}

} 提前谢谢

0 个答案:

没有答案