ListView With Search Using Volley And Sqlite

ListView With Search Using Volley And Sqlite

xml files:

searchviewlist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#62a178"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_gravity="center"
        android:gravity="center" >

        <EditText
            android:id="@+id/search"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:background="@drawable/edittextshape"
            android:hint="  search Movies Name"
            android:paddingLeft="10dp"
            android:textColor="#000"
            android:textColorHint="#000" />
    </LinearLayout>

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:divider="#fff"
        android:dividerHeight="1dp" />


</LinearLayout>

searchviewlist_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#62a178"
    android:orientation="horizontal"
    android:padding="5dip" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/id"
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:textColor="#fff"
            android:textSize="18dip"
            android:textStyle="bold"
            android:visibility="gone" />

        <TextView
            android:id="@+id/name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Name"
            android:textColor="#fff"
            android:textSize="18dip"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/gender"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Gender"
            android:textColor="#fff"
            android:textSize="15dip" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="5dp"
            android:weightSum="2" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
           
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/releseyear"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Release Year - "
                    android:textColor="#fff"
                   android:layout_marginLeft="10dp"
                    android:textSize="12dip" />

                <TextView
                    android:id="@+id/releseyear1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="year"
                    android:textColor="#fff"
               
                    android:textSize="12dip" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_gravity="right"
                android:gravity="right"
             android:layout_marginRight="20dp"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/rating"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Rating - "
                    android:textColor="#fff"
               
                    android:textSize="12dip" />

                <TextView
                    android:id="@+id/rating1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="rating"
                    android:textColor="#fff"
             
                    android:textSize="12dip" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>


</LinearLayout>

Java files:

SearchviewList.java

package com.listviewwithsearch.parthiv;

import java.util.ArrayList;

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

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class SearchviewList extends Activity {

ListView list;

String fontPath = "fonts/Smoolthan Bold.otf";
String oncallvalve;
Typeface tf;
ProgressDialog mProgressDialog;
int totallength1 = 0;
int count = 0;
String jsonResponse;
private String[] standardstr;
String countoflength = "0";
private SQLiteDatabase dataBase2;
DBHelper mHelper2;
EditText searchview;

private ArrayList<String> userIdd = new ArrayList<String>();
private ArrayList<String> user_name = new ArrayList<String>();
private ArrayList<String> user_gender = new ArrayList<String>();
private ArrayList<String> user_release = new ArrayList<String>();
private ArrayList<String> user_rating = new ArrayList<String>();

String userid[];
String clientname[];
String clientgender[];
String release[];
String rating[];

int yui;
int textlength = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.searchviewlist);
SearchviewList.this.deleteDatabase(DBHelper.DATABASE_NAME);
tf = Typeface.createFromAsset(getAssets(), fontPath);

list = (ListView) findViewById(R.id.list);
searchview = (EditText) findViewById(R.id.search);
searchview.setTypeface(tf);

new searchview().execute();

}

class searchview extends AsyncTask<String, String, String> {
@SuppressWarnings("deprecation")
protected void onPreExecute() {
super.onPreExecute();

mProgressDialog = new ProgressDialog(SearchviewList.this);
mProgressDialog.setMessage("Please wait.....");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setCancelable(false);
mProgressDialog.show();

}

protected String doInBackground(String... params) {

String urlJsonArry1 = "http://api.androidhive.info/json/movies.json";

try {
JsonArrayRequest req = new JsonArrayRequest(urlJsonArry1,
new Response.Listener<JSONArray>() {
public void onResponse(JSONArray response) {
try {
jsonResponse = "";
totallength1 = response.length();

countoflength = "" + response.length();

for (int i = 0; i < response.length(); i++) {

JSONObject person = (JSONObject) response
.get(i);

mHelper2 = new DBHelper(
SearchviewList.this);

try {
dataBase2 = mHelper2
.getWritableDatabase();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ContentValues values = new ContentValues();

values.put(DBHelper.KEY_NAME,
person.getString("title"));
values.put(DBHelper.KEY_GENDER,
person.getString("genre"));
values.put(DBHelper.KEY_RDATE,
person.getString("releaseYear"));
values.put(DBHelper.KEY_RATING,
person.getString("rating"));

dataBase2.insert(DBHelper.TABLE_NAME3,
null, values);

}

if (totallength1 == 0) {

Toast.makeText(getApplicationContext(),
"No Data Available", 1000)
.show();
mProgressDialog.dismiss();

} else {

ShowAllContent();
}
} catch (JSONException e) {

Toast.makeText(getApplicationContext(),
"Internet Problem", 1000).show();
mProgressDialog.dismiss();

}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

Toast.makeText(getApplicationContext(),
"Check Internet Connection", 1000)
.show();
mProgressDialog.dismiss();
mHelper2 = new DBHelper(SearchviewList.this);
ShowAllContent();

}
});

AppController.getInstance().addToRequestQueue(req);
} catch (Exception e) {
System.out.println("Exception : " + e.getMessage());
}

return null;
}

private void ShowAllContent() {

mProgressDialog.dismiss();
dataBase2 = mHelper2.getWritableDatabase();

Cursor mCursor = dataBase2.rawQuery("SELECT * FROM "
+ DBHelper.TABLE_NAME3, null);

userIdd.clear();
user_name.clear();
user_gender.clear();
user_release.clear();
user_rating.clear();

int gh = mHelper2.getProfilesCount();

userid = new String[gh];
clientname = new String[gh];
clientgender = new String[gh];
release = new String[gh];
rating = new String[gh];

yui = 0;

if (mCursor.moveToFirst()) {
do {
userIdd.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_ID)));
user_name.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_NAME)));
user_gender.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_GENDER)));
user_release.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_RDATE)));
user_rating.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_RATING)));

userid[yui] = mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_ID));
clientname[yui] = mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_NAME));
clientgender[yui] = mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_GENDER));
release[yui] = mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_RDATE));
rating[yui] = mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_RATING));

yui++;

} while (mCursor.moveToNext());
}

searchview.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// Abstract Method of TextWatcher Interface.
}

public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
// Abstract Method of TextWatcher Interface.
}

public void onTextChanged(CharSequence s, int start,
int before, int count) {
textlength = searchview.getText().length();

userIdd.clear();
user_name.clear();
user_gender.clear();
user_release.clear();
user_rating.clear();

for (int i = 0; i < clientname.length; i++) {
if (textlength <= clientname[i].length()) {

if (clientname[i].toLowerCase().contains(
searchview.getText().toString()
.toLowerCase().trim())) {

userIdd.add(userid[i]);
user_name.add(clientname[i]);
user_gender.add(clientgender[i]);
user_release.add(release[i]);
user_rating.add(rating[i]);

}
}
}

DisplayAdapter disadpt = new DisplayAdapter(
SearchviewList.this, userIdd, user_name,
user_gender, user_release, user_rating);
list.setAdapter(disadpt);
disadpt.notifyDataSetChanged();

}
});
try {
if (count == 0) {
DisplayAdapter disadpt = new DisplayAdapter(
SearchviewList.this, userIdd, user_name,
user_gender, user_release, user_rating);
list.setAdapter(disadpt);

} else {

DisplayAdapter disadpt = new DisplayAdapter(
SearchviewList.this, userIdd, user_name,
user_gender, user_release, user_rating);
list.setAdapter(disadpt);
disadpt.notifyDataSetChanged();
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mCursor.close();

}

}

public class DisplayAdapter extends BaseAdapter {

ArrayList<String> id = null;

ArrayList<String> clientnamee = null;
ArrayList<String> calltype = null;
ArrayList<String> redate = null;
ArrayList<String> ratingmovie = null;

Context mContext = null;

public DisplayAdapter(Context c, ArrayList<String> list,
ArrayList<String> cliname, ArrayList<String> calltype,
ArrayList<String> relesdate, ArrayList<String> ratig) {
this.mContext = c;
this.id = list;

this.clientnamee = cliname;
this.calltype = calltype;
this.redate = relesdate;
this.ratingmovie = ratig;

}

public int getCount() {
// TODO Auto-generated method stub
return id.size();
}

public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

public View getView(final int pos, View child, ViewGroup parent) {
Holder mHolder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.searchviewlist_row,
null);
mHolder = new Holder();
mHolder.idtxt = (TextView) child.findViewById(R.id.id);
mHolder.name = (TextView) child.findViewById(R.id.name);
mHolder.gender = (TextView) child.findViewById(R.id.gender);
mHolder.relesedate = (TextView) child
.findViewById(R.id.releseyear1);
mHolder.relesedate1 = (TextView) child
.findViewById(R.id.releseyear);
mHolder.ratingmov = (TextView) child.findViewById(R.id.rating1);
mHolder.ratingmov1 = (TextView) child.findViewById(R.id.rating);

mHolder.name.setTypeface(tf);
mHolder.gender.setTypeface(tf);
mHolder.relesedate.setTypeface(tf);
mHolder.relesedate1.setTypeface(tf);
mHolder.ratingmov.setTypeface(tf);
mHolder.ratingmov1.setTypeface(tf);

child.setTag(mHolder);
} else {
mHolder = (Holder) child.getTag();
}
mHolder.idtxt.setText(id.get(pos).toString());
mHolder.name.setText(clientnamee.get(pos));
mHolder.gender.setText(calltype.get(pos));
mHolder.relesedate.setText(redate.get(pos));
mHolder.ratingmov.setText(ratingmovie.get(pos));

return child;
}

public class Holder {

TextView idtxt;
TextView name;
TextView gender;
TextView relesedate;
TextView ratingmov;
TextView relesedate1;
TextView ratingmov1;

}

}


}

DBHelper.java

package com.listviewwithsearch.parthiv;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {
static String DATABASE_NAME = "dbnameddmmyy";
public static final String TABLE_NAME3 = "tabmmddlnameyy";
public static final String KEY_ID = "id";
public static final String KEY_NAME = "name";
public static final String KEY_GENDER = "gender";
public static final String KEY_RDATE = "release";
public static final String KEY_RATING = "rating";

public DBHelper(Context context) {
super(context, DATABASE_NAME, null, 1);

}

@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME3 + " (" + KEY_ID
+ " INTEGER PRIMARY KEY, " + KEY_NAME + " TEXT,"
+ KEY_GENDER + " TEXT,"
+ KEY_RDATE + " TEXT,"
+ KEY_RATING + " TEXT)";

db.execSQL(CREATE_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME3);

onCreate(db);

}
  public int getProfilesCount() {
        String countQuery = "SELECT  * FROM " + TABLE_NAME3;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        int cnt = cursor.getCount();
        cursor.close();
        return cnt;
    }


}

Output Screens:












Keep Updating Every Day...


No comments:

Post a Comment

back to top