Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MainAdapter.java
- ===========================
- import android.content.Context;
- import android.content.Intent;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
- import java.util.List;
- /**
- * Created by teacher on 1/15/2018.
- */
- public class MainAdapter extends BaseAdapter {
- List<ClsItems> items;
- Context context;
- public MainAdapter(List<ClsItems> items, Context context) {
- this.items = items;
- this.context = context;
- }
- @Override
- public int getCount() {
- return items.size();
- }
- @Override
- public Object getItem(int position) {
- return null;
- }
- @Override
- public long getItemId(int position) {
- return 0;
- }
- @Override
- public View getView(final int position, View convertView, ViewGroup parent) {
- View myItem = LayoutInflater.from(context).inflate(R.layout.main_item, null);
- ImageView myLogo = myItem.findViewById(R.id.imgLogo);
- TextView myTxt = myItem.findViewById(R.id.txtMain);
- myLogo.setImageDrawable(items.get(position).myLogo);
- myTxt.setText(items.get(position).name);
- myItem.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent myIntent = new Intent(context, WebView.class);
- myIntent.putExtra("url",items.get(position).URL);
- // will also deliver the phones number
- context.startActivity(myIntent);
- }
- });
- return myItem;
- }
- }
- ClsItems.java
- ===================
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.graphics.drawable.Drawable;
- import android.util.Log;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- /**
- * Created by teacher on 1/15/2018.
- */
- public class ClsItems {
- Drawable myLogo;
- String name;
- String URL;
- List<String> tel;
- //example of data
- // 347234:23423:www.bll.org.il:04854562%65132165%2313598%654989
- public ClsItems(Context context, String data) {
- String[] myData = data.split(":");
- this.myLogo = context.getDrawable(Integer.parseInt(myData[0]));
- this.name = myData[1];
- this.URL = myData[2];
- String[] allTel = myData[3].split("%");
- tel = new ArrayList<>();
- for (String item : allTel) {
- tel.add(item);
- }
- }
- public static List<ClsItems> getData(Context context)
- {
- //create empty list of ClsItems
- List<ClsItems> returnList = new ArrayList<>();
- //open the shared preferences file to get the data (we don't need editor, since we doing read only)
- SharedPreferences sp=context.getSharedPreferences("items", Context.MODE_PRIVATE);
- //create a collection of all the data from our shared preferences by hashMap<K,V>
- Map<String, ?> allEntries=sp.getAll();
- //iterate on each item, to get the value
- for (Map.Entry<String, ?> item:allEntries.entrySet())
- {
- //create a new instance of ClsItems class
- ClsItems singleItem = new ClsItems(context,item.getValue().toString());
- //add the item to our list.
- returnList.add(singleItem);
- }
- //return the list
- return returnList;
- }
- public static boolean addEntry(Context context, String entryName, String data) {
- try {
- //make shared prefernces instance
- SharedPreferences sp = context.getSharedPreferences("items", Context.MODE_PRIVATE);
- //open shared prefernces for editing
- SharedPreferences.Editor spe = sp.edit();
- //insert our new entry
- spe.putString(entryName,data);
- //commit (save) the changes
- spe.commit();
- return true;
- } catch (Exception e) {
- return false;
- }
- }
- /*
- public ClsItems(Drawable myLogo, String name, String URL, String dataTel, Context context) {
- this.myLogo = myLogo;
- this.name = name;
- this.URL = URL;
- this.context = context;
- String dt[] = dataTel.split("%");
- tel=new ArrayList<>();
- for (String item:dt)
- {
- tel.add(item);
- }
- Log.e("zz", "ClsItems: Kill Hani & zana & kakadu" );
- }
- */
- }
- WebView.java
- ==================
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.Toast;
- public class WebView extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_web_view);
- Bundle extras = getIntent().getExtras();
- String url = "http://"+extras.getString("url");
- Toast.makeText(this, url, Toast.LENGTH_SHORT).show();
- }
- }
- MainActivity.java
- =================
- import android.content.Context;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.ListView;
- import android.widget.Toast;
- import java.util.ArrayList;
- import java.util.List;
- public class MainActivity extends AppCompatActivity {
- Context context;
- List<ClsItems> myItems;
- ListView myList;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //setData();
- setPointer();
- }
- private void setPointer() {
- this.context=this;
- myItems = new ArrayList<>();
- myList=findViewById(R.id.lvMain);
- myItems = ClsItems.getData(this);
- MainAdapter myAdapter=new MainAdapter(myItems,context);
- myList.setAdapter(myAdapter);
- }
- private void setData() {
- /*
- myItems = new ArrayList<>();
- myItems.add(new ClsItems(this,R.drawable.bbl + ":" +
- R.string.bll + ":" +
- "www.hackeru.co.il" + ":" +
- "05212345678%0531234567%0501234567"));
- */
- //creating a string of new entry
- String newEntry=R.drawable.bbl + ":" +
- R.string.bll + ":" +
- "www.hackeru.co.il" + ":" +
- "05212345678%0531234567%0501234567";
- //adding a new entry
- if (!ClsItems.addEntry(this,"bll",newEntry))
- {
- Toast.makeText(context, "Error in saving data...", Toast.LENGTH_SHORT).show();
- }
- }
- }
- activity_web_view.xml
- ==========================
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <WebView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/myWebView"/>
- </LinearLayout>
- activity_main.xml
- ==========================
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:layout_margin="16dp">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="האפליקציה שלי"
- android:gravity="center"
- android:textSize="32sp"/>
- <ListView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/lvMain"></ListView>
- </LinearLayout>
- main_item.xml
- =============
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal" android:layout_width="match_parent"
- android:layout_height="80dp"
- android:layoutDirection="rtl">
- <ImageView
- android:layout_width="80dp"
- android:layout_height="wrap_content"
- android:src="@drawable/bbl"
- android:id="@+id/imgLogo"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="המוסד לביטוח לאומי"
- android:textSize="28sp"
- android:layout_gravity="center"
- android:id="@+id/txtMain"/>
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement