lawang code

lawang Code


Tanyakan kode disini

pencarian data dengan searchview berdasarkan listview di android

ditanyakan oleh Harun 3 tahun yang lalu

permisi gan mau tanya, mohon di bantu ya. ane punya tugas nih, ketika kita ketik kan A misalnya di searchview, maka di list nya muncul yg awalan hurufnya A saja. ketika searchview di kosongkan Semua data muncul di list view.

ini source code nya MainActivity.java

package com.example.hparyanto.myapplication;

import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



import android.widget.ArrayAdapter;

import android.widget.TextView;
import android.widget.AbsListView;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;



import com.example.hparyanto.myapplication.models.LokasiModel;



public class MainActivity extends AppCompatActivity{

    private AbsListView lvLokasi;
    private TextView tvData;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        lvLokasi = (AbsListView)findViewById(R.id.lvLokasi);


        new JSONTask().execute("http://10.0.2.2/koordinat/json.php");
       // new JSONTask().execute("http://routeassistant.esy.es/json.php");


    }



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

        @Override
        protected List<LokasiModel> 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("sekolah");

                List<LokasiModel> lokasiModelList = new ArrayList<>();
                for(int i=0; i<parentArray.length(); i++) {
                    JSONObject finalObject = parentArray.getJSONObject(i);
                    LokasiModel lokasiModel= new LokasiModel();
                    lokasiModel.setId(finalObject.getString("id"));
                    lokasiModel.setKategori(finalObject.getString("kategori"));
                    lokasiModel.setNama(finalObject.getString("tujuan"));
                    lokasiModel.setKoordinat(finalObject.getString("koordinat"));
                    lokasiModel.setDeskripsi(finalObject.getString("deskripsi"));
                    lokasiModel.setUrl(finalObject.getString("url"));

                    lokasiModelList.add(lokasiModel);
                }
                return lokasiModelList;

            } 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(List<LokasiModel> result) {
            super.onPostExecute(result);

            LokasiAdapter adapter = new LokasiAdapter(getApplicationContext(),R.layout.row, result);
            lvLokasi.setAdapter(adapter);
        }
    }








    public class LokasiAdapter extends ArrayAdapter{

        private List<LokasiModel> lokasiModelList;
        private int resource;
        private LayoutInflater inflater;
        public LokasiAdapter(Context context, int resource, List<LokasiModel> objects) {
            super(context, resource, objects);
            lokasiModelList = objects;
            this.resource = resource;
    inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
       }

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

            if(convertView == null){
                convertView = inflater.inflate(resource, null);
            }

            TextView tvnama;
            TextView tvdeskripsi;


            tvnama = (TextView)convertView.findViewById(R.id.tvnama);
            tvdeskripsi = (TextView)convertView.findViewById(R.id.tvdeskripsi);

            tvnama.setText(lokasiModelList.get(position).getNama());
            tvdeskripsi.setText(lokasiModelList.get(position).getDeskripsi());

            return convertView;
        }
    }

}

terus ini activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context="com.example.hparyanto.myapplication.MainActivity"
android:background="@drawable/background">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Kategori Bank dan ATM"
    android:layout_gravity="center_horizontal"
    android:id="@+id/tvkategori"
    android:layout_marginTop="20dp" />


<SearchView
    android:layout_width="340dp"
    android:layout_height="wrap_content"
    android:id="@+id/searchView"
    android:layout_gravity="center_horizontal"
    android:background="#ffe4b5"
    android:layout_marginTop="20dp"
    android:queryHint="Pencarian Data..."/>

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/lvLokasi"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="10dp"
    android:paddingLeft="10dp"
    android:layout_marginRight="10dp"/>

mohon bantuannya gan. trima kasih sebelumnya.

Anda harus login terlebih dahulu untuk memberikan komentar

Recent Tag

X 6
X 48
X 14
X 10
X 1
X 5
X 4
X 18
X 10

Bantuan

Help

Feedback

Help