Advertisement
Guest User

myOldie

a guest
Jan 18th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.53 KB | None | 0 0
  1. MainAdapter.java
  2. ===========================
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.BaseAdapter;
  9. import android.widget.ImageView;
  10. import android.widget.TextView;
  11.  
  12. import java.util.List;
  13.  
  14. /**
  15.  * Created by teacher on 1/15/2018.
  16.  */
  17.  
  18. public class MainAdapter extends BaseAdapter {
  19.     List<ClsItems> items;
  20.     Context context;
  21.  
  22.     public MainAdapter(List<ClsItems> items, Context context) {
  23.         this.items = items;
  24.         this.context = context;
  25.     }
  26.  
  27.     @Override
  28.     public int getCount() {
  29.         return items.size();
  30.     }
  31.  
  32.     @Override
  33.     public Object getItem(int position) {
  34.         return null;
  35.     }
  36.  
  37.     @Override
  38.     public long getItemId(int position) {
  39.         return 0;
  40.     }
  41.  
  42.     @Override
  43.     public View getView(final int position, View convertView, ViewGroup parent) {
  44.         View myItem = LayoutInflater.from(context).inflate(R.layout.main_item, null);
  45.  
  46.         ImageView myLogo = myItem.findViewById(R.id.imgLogo);
  47.         TextView myTxt = myItem.findViewById(R.id.txtMain);
  48.  
  49.         myLogo.setImageDrawable(items.get(position).myLogo);
  50.         myTxt.setText(items.get(position).name);
  51.         myItem.setOnClickListener(new View.OnClickListener() {
  52.             @Override
  53.             public void onClick(View v) {
  54.                 Intent myIntent = new Intent(context, WebView.class);
  55.                 myIntent.putExtra("url",items.get(position).URL);
  56.                 // will also deliver the phones number
  57.                 context.startActivity(myIntent);
  58.             }
  59.         });
  60.         return myItem;
  61.  
  62.     }
  63. }
  64.  
  65.  
  66.  
  67.  
  68. ClsItems.java
  69. ===================
  70. import android.content.Context;
  71. import android.content.SharedPreferences;
  72. import android.graphics.drawable.Drawable;
  73. import android.util.Log;
  74.  
  75. import java.util.ArrayList;
  76. import java.util.List;
  77. import java.util.Map;
  78.  
  79. /**
  80.  * Created by teacher on 1/15/2018.
  81.  */
  82.  
  83. public class ClsItems {
  84.     Drawable myLogo;
  85.     String name;
  86.     String URL;
  87.     List<String> tel;
  88.     //example of data
  89.     // 347234:23423:www.bll.org.il:04854562%65132165%2313598%654989
  90.  
  91.     public ClsItems(Context context, String data) {
  92.         String[] myData = data.split(":");
  93.         this.myLogo = context.getDrawable(Integer.parseInt(myData[0]));
  94.         this.name = myData[1];
  95.         this.URL = myData[2];
  96.         String[] allTel = myData[3].split("%");
  97.         tel = new ArrayList<>();
  98.         for (String item : allTel) {
  99.             tel.add(item);
  100.         }
  101.     }
  102.  
  103.     public static List<ClsItems> getData(Context context)
  104.     {
  105.         //create empty list of ClsItems
  106.         List<ClsItems> returnList = new ArrayList<>();
  107.         //open the shared preferences file to get the data (we don't need editor, since we doing read only)
  108.         SharedPreferences sp=context.getSharedPreferences("items", Context.MODE_PRIVATE);
  109.  
  110.         //create a collection of all the data from our shared preferences by hashMap<K,V>
  111.         Map<String, ?> allEntries=sp.getAll();
  112.         //iterate on each item, to get the value
  113.         for (Map.Entry<String, ?> item:allEntries.entrySet())
  114.         {
  115.             //create a new instance of ClsItems class
  116.             ClsItems singleItem = new ClsItems(context,item.getValue().toString());
  117.             //add the item to our list.
  118.             returnList.add(singleItem);
  119.         }
  120.         //return the list
  121.         return returnList;
  122.     }
  123.  
  124.  
  125.     public static boolean addEntry(Context context, String entryName, String data) {
  126.         try {
  127.             //make shared prefernces instance
  128.             SharedPreferences sp = context.getSharedPreferences("items", Context.MODE_PRIVATE);
  129.             //open shared prefernces for editing
  130.             SharedPreferences.Editor spe = sp.edit();
  131.             //insert our new entry
  132.             spe.putString(entryName,data);
  133.             //commit (save) the changes
  134.             spe.commit();
  135.             return true;
  136.         } catch (Exception e) {
  137.             return false;
  138.         }
  139.     }
  140.  
  141.     /*
  142.     public ClsItems(Drawable myLogo, String name, String URL, String dataTel, Context context) {
  143.         this.myLogo = myLogo;
  144.         this.name = name;
  145.         this.URL = URL;
  146.         this.context = context;
  147.         String dt[] = dataTel.split("%");
  148.         tel=new ArrayList<>();
  149.         for (String item:dt)
  150.         {
  151.             tel.add(item);
  152.         }
  153.         Log.e("zz", "ClsItems: Kill Hani & zana & kakadu" );
  154.     }
  155.      */
  156. }
  157.  
  158.  
  159. WebView.java
  160. ==================
  161. import android.support.v7.app.AppCompatActivity;
  162. import android.os.Bundle;
  163. import android.widget.Toast;
  164.  
  165. public class WebView extends AppCompatActivity {
  166.  
  167.     @Override
  168.     protected void onCreate(Bundle savedInstanceState) {
  169.         super.onCreate(savedInstanceState);
  170.         setContentView(R.layout.activity_web_view);
  171.         Bundle extras = getIntent().getExtras();
  172.         String url = "http://"+extras.getString("url");
  173.  
  174.         Toast.makeText(this, url, Toast.LENGTH_SHORT).show();
  175.  
  176.     }
  177. }
  178.  
  179.  
  180. MainActivity.java
  181. =================
  182. import android.content.Context;
  183. import android.support.v7.app.AppCompatActivity;
  184. import android.os.Bundle;
  185. import android.widget.ListView;
  186. import android.widget.Toast;
  187.  
  188. import java.util.ArrayList;
  189. import java.util.List;
  190.  
  191. public class MainActivity extends AppCompatActivity {
  192.     Context context;
  193.     List<ClsItems> myItems;
  194.     ListView myList;
  195.     @Override
  196.     protected void onCreate(Bundle savedInstanceState) {
  197.         super.onCreate(savedInstanceState);
  198.         setContentView(R.layout.activity_main);
  199.         //setData();
  200.         setPointer();
  201.     }
  202.  
  203.  
  204.  
  205.     private void setPointer() {
  206.         this.context=this;
  207.         myItems = new ArrayList<>();
  208.         myList=findViewById(R.id.lvMain);
  209.         myItems = ClsItems.getData(this);
  210.         MainAdapter myAdapter=new MainAdapter(myItems,context);
  211.         myList.setAdapter(myAdapter);
  212.     }
  213.  
  214.     private void setData() {
  215.         /*
  216.         myItems = new ArrayList<>();
  217.         myItems.add(new ClsItems(this,R.drawable.bbl + ":" +
  218.                 R.string.bll + ":" +
  219.                 "www.hackeru.co.il" + ":" +
  220.                 "05212345678%0531234567%0501234567"));
  221.         */
  222.  
  223.         //creating a string of new entry
  224.         String newEntry=R.drawable.bbl + ":" +
  225.                 R.string.bll + ":" +
  226.                 "www.hackeru.co.il" + ":" +
  227.                 "05212345678%0531234567%0501234567";
  228.  
  229.         //adding a new entry
  230.         if (!ClsItems.addEntry(this,"bll",newEntry))
  231.         {
  232.             Toast.makeText(context, "Error in saving data...", Toast.LENGTH_SHORT).show();
  233.         }
  234.  
  235.     }
  236. }
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247. activity_web_view.xml
  248. ==========================
  249. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  250.     android:layout_width="match_parent"
  251.     android:layout_height="match_parent"
  252.     android:orientation="vertical" >
  253.  
  254.     <WebView
  255.         android:layout_width="match_parent"
  256.         android:layout_height="match_parent"
  257.         android:id="@+id/myWebView"/>
  258.  
  259. </LinearLayout>
  260.  
  261.  
  262.  
  263. activity_main.xml
  264. ==========================
  265. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  266.     android:layout_width="match_parent"
  267.     android:layout_height="match_parent"
  268.     android:orientation="vertical"
  269.     android:layout_margin="16dp">
  270.  
  271.     <TextView
  272.         android:layout_width="match_parent"
  273.         android:layout_height="wrap_content"
  274.         android:text="האפליקציה שלי"
  275.         android:gravity="center"
  276.         android:textSize="32sp"/>
  277.     <ListView
  278.         android:layout_width="match_parent"
  279.         android:layout_height="match_parent"
  280.         android:id="@+id/lvMain"></ListView>
  281. </LinearLayout>
  282.  
  283.  
  284. main_item.xml
  285. =============
  286. <?xml version="1.0" encoding="utf-8"?>
  287. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  288.     android:orientation="horizontal" android:layout_width="match_parent"
  289.     android:layout_height="80dp"
  290.     android:layoutDirection="rtl">
  291.     <ImageView
  292.         android:layout_width="80dp"
  293.         android:layout_height="wrap_content"
  294.         android:src="@drawable/bbl"
  295.         android:id="@+id/imgLogo"/>
  296.     <TextView
  297.         android:layout_width="wrap_content"
  298.         android:layout_height="wrap_content"
  299.         android:text="המוסד לביטוח לאומי"
  300.         android:textSize="28sp"
  301.         android:layout_gravity="center"
  302.         android:id="@+id/txtMain"/>
  303. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement