Advertisement
Guest User

Untitled

a guest
Aug 15th, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.05 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.v4.widget.DrawerLayout
  3.         xmlns:android="http://schemas.android.com/apk/res/android"
  4.         android:layout_width="match_parent"
  5.         android:layout_height="match_parent"
  6.         android:id="@+id/drawerLayout">
  7.     <FrameLayout
  8.             android:layout_width="match_parent"
  9.             android:layout_height="match_parent"
  10.             android:id="@+id/contentContainer">
  11.         <TextView
  12.                 android:layout_width="wrap_content"
  13.                 android:layout_height="wrap_content"
  14.                 android:text="content txt"
  15.                 android:layout_gravity="center"
  16.                 android:textSize="30sp"
  17.                 android:id="@+id/contentTxt"/>
  18.         <Button
  19.                 android:layout_width="wrap_content"
  20.                 android:layout_height="wrap_content"
  21.                 android:text="xXx"
  22.                 android:id="@+id/btn"/>
  23.     </FrameLayout >
  24.     <ListView
  25.             android:layout_width="240dp"
  26.             android:layout_height="match_parent"
  27.             android:id="@+id/drawerList"/>
  28. </android.support.v4.widget.DrawerLayout>
  29.  
  30. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  31.  
  32. import android.support.v4.widget.DrawerLayout;
  33. import android.support.v7.app.AppCompatActivity;
  34. import android.os.Bundle;
  35. import android.view.View;
  36. import android.widget.*;
  37. import java.util.ArrayList;
  38. import java.util.HashMap;
  39. import java.util.List;
  40.  
  41. public class MainActivity extends AppCompatActivity {
  42.      ListView drawerList;
  43.      DrawerLayout drawerLayout;
  44.      TextView contentTxt;
  45.      private class ItemClickListener implements AdapterView.OnItemClickListener {
  46.          @Override
  47.          public void onItemClick(AdapterView<?> parent,View view,int position,long id) {
  48.              StringBuilder sb= new StringBuilder();
  49.              TextView label= (TextView)view.findViewById(R.id.label);
  50.              TextView n= (TextView)view.findViewById(R.id.n);
  51.              sb.append(label.getText() +"\t"+ n.getText() );
  52.              contentTxt.setText(sb.toString() );
  53.          }
  54.      }
  55.      private class Listener implements View.OnClickListener {
  56.          @Override
  57.          public void onClick(View v) {}
  58.      }
  59.  
  60.      private void initAdapter() {
  61.          String from[]= {"Label","n"};
  62.          int to[]= {R.id.label,R.id.n};
  63.          String strs[]= {"Молоко", "Сметана", "Хлеб", "Пылесос"};
  64.          String ns[]= {"1", "2", "2", "0"};
  65.          List<HashMap<String,Object> > data= new ArrayList<>();
  66.  
  67.          for(int i=0; i<strs.length; i++) {
  68.              HashMap<String,Object> map= new HashMap();
  69.              map.put(from[0],strs[i] );
  70.              map.put(from[1],ns[i] );
  71.          }
  72.          SimpleAdapter adapter= new SimpleAdapter(this,data,R.layout.drawer_list_element,from,to);
  73.          drawerList.setAdapter(adapter);
  74.      }
  75.  
  76.      @Override
  77.      protected void onCreate(Bundle savedInstanceState) {
  78.          super.onCreate(savedInstanceState);
  79.          setContentView(R.layout.main_layout);
  80.  
  81.          Button btn= (Button)findViewById(R.id.btn);
  82.          contentTxt= (TextView)findViewById(R.id.contentTxt);
  83.          drawerList= (ListView)findViewById(R.id.drawerList);
  84.          drawerLayout= (DrawerLayout)findViewById(R.id.drawerLayout);
  85.  
  86.          initAdapter();
  87.          drawerList.setOnItemClickListener(new ItemClickListener());
  88.          btn.setOnClickListener(new Listener() );
  89.      }
  90.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement