应用程序的所有活动的导航抽屉布局

时间:2015-07-04 08:16:45

标签: android listview navigation-drawer

我有两个活动 - 用户登录页面的MainActivity,以及在ListView中显示用户详细信息(用户制作的项目)的第二个活动。我想创建一个导航抽屉布局,它将出现在两个活动中。 I found the solution here.但问题是,我的第二个活动是扩展ListActivity以在ListView中显示用户详细信息,因此我无法扩展其他任何内容。这是我的第二项活动:

public class ProjectList extends ListActivity {
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter;
ListView listView;
JSONArray projects = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.project_list_inflator);
    listView = (ListView)findViewById(android.R.id.list);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
    listView.setAdapter(adapter);
    new HttpGetHandler().execute();
}



private class HttpGetHandler extends AsyncTask<String, Void, Void> {
    String jsonUrl = "some url";
    String imgUrl = "http://canvasflip.com/protected/app/elements/userElements/";
    @Override
    protected Void doInBackground(String... params) {
        HttpGet httpGet = new HttpGet(jsonUrl);
        //HttpGet httpGet1 = new HttpGet(imgUrl);
        try {
            HttpResponse httpResponse = MainActivity.httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            InputStream content = httpEntity.getContent();
            String result = convertToString(content);
            JSONObject jsonObject = new JSONObject(result);
            projects = jsonObject.getJSONArray("projects");
            ProjectList.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        for(int i = 0; i<projects.length(); i++) {
                            JSONObject p = projects.getJSONObject(i);
                            String title = p.getString("title");
                            listItems.add(title);
                            adapter.notifyDataSetChanged();
                        }

                    }catch(Exception e) {
                        Log.d("MSG", e.toString());
                    }
                }
            });
        }catch(Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    public String convertToString(InputStream inputStream) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line = "";
        String result = "";
        while((line = bufferedReader.readLine())!=null) {
            result += line;

        }
        inputStream.close();
        return result;
    }
 }
}

1 个答案:

答案 0 :(得分:0)

创建一个具有Navigation抽屉实现源的新Activity

public class DrawerActivity extends ListActivity 将DrawerLayout覆盖为

  

protected DrawerLayout drawerLayout;

在抽屉的onCreate中,将抽屉布局初始化为,

drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout1);

让你的MainActivity扩展DrawerAcivity。

然后将setContentView替换为给定的。

//  setContentView(R.layout.activity_main);

    LayoutInflater inflater = (LayoutInflater) getApplicationContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View contentView = inflater.inflate(R.layout.activity_main, null, false);
    drawerLayout.addView(contentView, 0);

我已成功完成整个申请。

快乐的编码:)