如何从另一个类

时间:2017-10-30 10:43:57

标签: java android xml

我的内容视图是一个名为ftagment top的Listview,它显示给另一个名为SinglePackageActivity的xml。 SinglePackageActivity中有一个mapfragment,我需要设置lat和long。

如何在SinglePackageActivity中调用地图片段。

我的代码如下:

Fragment_toplist

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:id="@+id/fri"
    android:background="#fff">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lvMovies" />

    <FrameLayout
        android:id="@+id/maincontainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

activity_singlepackage

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:fitsSystemWindows="true"
    app:itemBackground="@drawable/list_item_bg_pressed"
    app:itemIconTint="#fff"
    app:layout_collapseParallaxMultiplier="1.0"
    tools:context="com.panenviron.dtpcthrissur.activity.SinglePackageActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbara"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        android:weightSum="1"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/lat"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/Long"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@drawable/list_item_bg_pressed">
        <!-- Let's add fragment -->
        <FrameLayout
            android:id="@+id/framei"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    <!---
         Navigation view to show the menu items
    -->
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/list_item_bg_pressed">


        <FrameLayout

            android:id="@+id/frame_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:animateLayoutChanges="true">

            <RelativeLayout
                android:id="@+id/main_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent">


                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:id="@+id/container"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    tools:context="com.panenviron.nav_smp.Bottom_Navigation">

                    <FrameLayout
                        android:id="@+id/content"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1">


                        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                            xmlns:app="http://schemas.android.com/apk/res-auto"
                            xmlns:tools="http://schemas.android.com/tools"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="#fff"
                            android:orientation="horizontal">

                            <ScrollView
                                android:layout_width="match_parent"
                                android:layout_height="match_parent">

                                <RelativeLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent">

                                    <ImageView
                                        android:id="@+id/slider"
                                        android:layout_width="match_parent"
                                        android:layout_height="210dp"
                                        android:scaleType="fitXY"
                                        android:background="@drawable/thrissur_head" />

                                    <ImageView
                                        android:id="@+id/imageView6"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:layout_alignBottom="@+id/textView16"
                                        android:layout_alignParentEnd="true"
                                        android:layout_alignParentRight="true"
                                        android:layout_below="@+id/slider"
                                        android:layout_marginTop="8dp"
                                        android:src="@drawable/thrissur_bmap" />

                                    <LinearLayout
                                        android:id="@+id/l1"
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:layout_alignParentLeft="true"
                                        android:layout_alignParentStart="true"
                                        android:layout_below="@+id/textView18"
                                        android:orientation="horizontal">


                                    </LinearLayout>

                                    <LinearLayout
                                        android:id="@+id/l2"
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:layout_alignParentLeft="true"
                                        android:layout_alignParentStart="true"
                                        android:layout_below="@+id/l1"
                                        android:orientation="horizontal">


                                    </LinearLayout>

                                    <LinearLayout
                                        android:id="@+id/l3"
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:layout_alignParentLeft="true"
                                        android:layout_alignParentStart="true"
                                        android:layout_below="@+id/l2"
                                        android:orientation="horizontal">

                                      <fragment
                                                android:id="@+id/glmap"
                                                android:name="com.google.android.gms.maps.SupportMapFragment"
                                                android:layout_width="match_parent"
                                                android:layout_height="250dp"
                                                android:layout_alignParentLeft="true"
                                                android:layout_alignParentStart="true"
                                                android:layout_alignParentTop="true"
                                                android:layout_marginLeft="7dp"
                                                android:layout_marginRight="7dp"
                                                android:layout_marginTop="25dp"/>


                                            <TextView
                                                android:id="@+id/textView21"
                                                android:layout_width="match_parent"
                                                android:layout_height="wrap_content"
                                                android:layout_alignParentStart="true"
                                                android:layout_below="@+id/book"
                                                android:layout_marginBottom="10dp"
                                                android:layout_marginTop="22dp"
                                                android:fontFamily="cursive"
                                                android:gravity="center"
                                                android:paddingTop="10dp"
                                                android:text="Be Share With"
                                                android:textAllCaps="false"
                                                android:textColor="@android:color/background_dark"
                                                android:textSize="16sp"
                                                android:textStyle="bold" />

                                            <ImageButton
                                                android:id="@+id/imageButton4"
                                                android:layout_width="wrap_content"
                                                android:layout_height="wrap_content"
                                                android:layout_below="@+id/textView21"
                                                android:layout_toLeftOf="@+id/imageButton5"
                                                android:layout_toStartOf="@+id/imageButton5"
                                                android:background="#fff"
                                                app:srcCompat="@mipmap/facebook" />

                                            <ImageButton
                                                android:id="@+id/imageButton5"
                                                android:layout_width="wrap_content"
                                                android:layout_height="wrap_content"
                                                android:layout_below="@+id/textView21"
                                                android:layout_centerHorizontal="true"
                                                android:background="#fff"
                                                app:srcCompat="@mipmap/google" />

                                            <ImageButton
                                                android:id="@+id/imageButton8"
                                                android:layout_width="wrap_content"
                                                android:layout_height="wrap_content"
                                                android:layout_below="@+id/textView21"
                                                android:layout_toEndOf="@+id/imageButton5"
                                                android:layout_toRightOf="@+id/imageButton5"
                                                android:background="#fff"
                                                app:srcCompat="@mipmap/twitter" />

                                            <Button
                                                android:id="@+id/button14"
                                                android:layout_width="match_parent"
                                                android:layout_height="wrap_content"
                                                android:layout_alignParentLeft="true"
                                                android:layout_alignParentStart="true"
                                                android:layout_below="@+id/imageButton5"
                                                android:layout_marginTop="24dp"
                                                android:background="@android:color/background_dark"
                                                android:text="SEE MORE ON DTPCThrissur.COM"
                                                android:textColor="@android:color/background_light"
                                                android:textStyle="bold" />

                                            <ImageButton
                                                android:id="@+id/book"
                                                android:layout_width="wrap_content"
                                                android:layout_height="110dp"
                                                android:layout_below="@+id/textView19"
                                                android:layout_centerHorizontal="true"
                                                android:layout_marginLeft="20dp"
                                                android:layout_marginRight="20dp"
                                                android:background="@drawable/bb" />

                                        </RelativeLayout>
                                    </LinearLayout>

                                    <LinearLayout
                                        android:id="@+id/l4"
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:layout_alignParentLeft="true"
                                        android:layout_alignParentStart="true"
                                        android:layout_below="@+id/l3"
                                        android:orientation="horizontal">

                                    </LinearLayout>

                                    <Button
                                        android:id="@+id/find_kuttanad"
                                        android:layout_width="match_parent"
                                        android:layout_height="25dp"
                                        android:layout_alignParentEnd="true"
                                        android:layout_alignParentRight="true"
                                        android:layout_below="@+id/textView18"
                                        android:background="@color/cardview_shadow_start_color"
                                        android:fontFamily="serif"
                                        android:gravity="center"
                                        android:paddingLeft="8dp"
                                        android:paddingRight="8dp"
                                        android:text="Find more about Thrissur"
                                        android:textAllCaps="false"
                                        android:textColor="#075070"
                                        android:textSize="15sp"
                                        android:textStyle="bold" />

                                    <ProgressBar
                                        android:id="@+id/progressBars"
                                        style="?android:attr/progressBarStyle"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:layout_alignBottom="@+id/slider"
                                        android:layout_centerHorizontal="true"
                                        android:layout_marginBottom="78dp" />

                                </RelativeLayout>

                            </ScrollView>
                        </LinearLayout>


                    </FrameLayout>


                </LinearLayout>


            </RelativeLayout>

        </FrameLayout>

        <TextView
            android:id="@+id/coders"
            android:layout_width="0dp"
            android:layout_height="0dp" />

    </RelativeLayout>


</FrameLayout>

MainActivity

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_toplist);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        dialog = new SpotsDialog(this, R.style.Custom);
        dialog.setCancelable(false);
        dialog.setMessage("Loading. Please wait...");

     //   SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(glmap);
     //   mapFragment.getMapAsync(SinglePackageActivity.this);

        Bundle bundle = getIntent().getExtras();
        String str = bundle.getString("my_string");
        int a = Integer.parseInt(str);


        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
                .defaultDisplayImageOptions(defaultOptions)
                .build();
        ImageLoader.getInstance().init(config); // Do it on Application start

        lvMovies = (ListView) findViewById(R.id.lvMovies);

        final String URL_TO_HIT = "http://softwaresolution.com/ItemDetailsApi.aspx?code=" + a;

        new JSONTask().execute(URL_TO_HIT);


    }


    public class JSONTask extends AsyncTask<String, String, List<ItemModel>> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog.show();
        }

        @Override
        protected List<ItemModel> doInBackground(String... params) {
            HttpURLConnection connection = null;
            BufferedReader reader = null;

            try {
                URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();
                InputStream stream = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(stream));
                StringBuffer buffer = new StringBuffer();
                String line = "";
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }

                String finalJson = buffer.toString();

                JSONObject parentObject = new JSONObject(finalJson);
                JSONArray parentArray = parentObject.getJSONArray("Item");

                List<ItemModel> movieModelList = new ArrayList<>();

                Gson gson = new Gson();

                for (int i = 0; i < parentArray.length(); i++) {
                    JSONObject finalObject = parentArray.getJSONObject(i);

                    ItemModel DModel = gson.fromJson(finalObject.toString(), ItemModel.class); // a single line json parsing using Gson
//
                    movieModelList.add(DModel);
                }
                return movieModelList;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }
                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(final List<ItemModel> result) {
            super.onPostExecute(result);
            dialog.dismiss();
            if (result != null) {
                MovieAdapter adapter = new MovieAdapter(getApplicationContext(), R.layout.activity_singlepackage, result);
                lvMovies.setAdapter(adapter);
            }
        }
    }

    public class MovieAdapter extends ArrayAdapter {

        private List<ItemModel> movieModelList;
        private int resource;
        private LayoutInflater inflater;

        public MovieAdapter(Context context, int resource, List<ItemModel> objects) {
            super(context, resource, objects);
            movieModelList = objects;
            this.resource = resource;
            inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;

            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(resource, null);

                holder.url = (ImageView) convertView.findViewById(R.id.slider);
                holder.Name = (TextView) convertView.findViewById(R.id.textView14);
                holder.fdetail = (TextView) convertView.findViewById(R.id.textView20);
                holder.LAT = (TextView) convertView.findViewById(R.id.lat);
                holder.Lng = (TextView) convertView.findViewById(R.id.Long);
                holder.sdeta = (TextView) convertView.findViewById(R.id.textView16);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            final ProgressBar progressBar = (ProgressBar) convertView.findViewById(R.id.progressBars);


            // Then later, when you want to display image
            final ViewHolder finalHolder = holder;
            ImageLoader.getInstance().displayImage(movieModelList.get(position).getUrl(), holder.url, new ImageLoadingListener() {
                @Override
                public void onLoadingStarted(String imageUri, View view) {
                    progressBar.setVisibility(View.VISIBLE);
                    finalHolder.url.setVisibility(View.INVISIBLE);
                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    progressBar.setVisibility(View.GONE);
                    finalHolder.url.setVisibility(View.INVISIBLE);
                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    progressBar.setVisibility(View.GONE);
                    finalHolder.url.setVisibility(View.VISIBLE);
                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {
                    progressBar.setVisibility(View.GONE);
                    finalHolder.url.setVisibility(View.INVISIBLE);
                }
            });


            holder.Name.setText("" + movieModelList.get(position).getName());
            holder.fdetail.setText("" + movieModelList.get(position).getDetails());
            holder.LAT.setText("" + movieModelList.get(position).getLAT());
            holder.Lng.setText("" + movieModelList.get(position).getlong1());
            holder.sdeta.setText("" + movieModelList.get(position).getDetails() + "...");


            Button button = (Button) convertView.findViewById(R.id.b_map);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Toast.makeText(getContext(), "Opening Google Map", Toast.LENGTH_SHORT).show();
                    Uri gmmIntentUri = Uri.parse("http://maps.google.com/maps?=" + "saddr=" + movieModelList.get(position).getLAT() + "," +
                            movieModelList.get(position).getlong1() + "&daddr=" + movieModelList.get(position).getLAT() + "," + movieModelList.get(position).getlong1());
                    Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
                    mapIntent.setPackage("com.google.android.apps.maps");

                    if (mapIntent.resolveActivity(ItemActivity.this.getPackageManager()) != null)

                    {
                        startActivity(mapIntent);

                    } else
                    {
                        Toast.makeText(getContext(), "Google Map Not Available", Toast.LENGTH_SHORT).show();
                    }
                }

            });


            return convertView;
        }


        class ViewHolder {


            private ImageView url;
            private TextView Name;
            private TextView fdetail;
            private TextView LAT;
            private TextView Lng;
            private TextView sdeta;

        }


    }

0 个答案:

没有答案