Xml Files:-
mainlay.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#164c1c"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="2dp"
android:gravity="center"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="@+id/addstockistedit"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="@drawable/roundedittext"
android:completionThreshold="1"
android:ems="10"
android:hint="SEARCH PRODUCT"
android:inputType="text"
android:maxLength="30"
android:paddingLeft="10dp"
android:singleLine="true"
android:textColor="#000"
android:textColorHint="#000"
android:textCursorDrawable="@drawable/color_cursor"
android:textSize="12dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginBottom="2dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:orientation="vertical" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginTop="5dp"
android:background="#fff" >
<TextView
android:id="@+id/brandheading"
android:layout_height="20dp"
android:layout_weight="1"
android:gravity="left"
android:padding="2dip"
android:text="PRODUCT"
android:textColor="#000000"
android:textSize="11dp"
android:textStyle="bold"
android:typeface="monospace" />
</TableRow>
<ListView
android:id="@+id/list22"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp" >
</ListView>
</LinearLayout>
</LinearLayout>
mainlay_row.xml
<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"
android:background="#164c1c"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="8dp" >
<TextView
android:id="@+id/txt_id"
android:layout_width="1dp"
android:layout_height="1dp"
android:textColor="#000"
android:visibility="invisible" />
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="5dp" >
<TextView
android:id="@+id/txt_fName"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#164c1c"
android:gravity="left|center"
android:padding="2dip"
android:text="PRODUCT"
android:textColor="#fff"
android:textSize="11dp" />
</TableRow>
</LinearLayout>
searchitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#3b444b"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dp"
android:text="sx"
android:textColor="#fff"
android:textSize="12sp"
android:typeface="serif" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="0"
android:src="@drawable/ic_launcher"
android:visibility="invisible" />
</LinearLayout>
fillallfield.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="5px"
android:background="@drawable/toastround"
android:orientation="horizontal"
android:padding="10dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="3dp"
android:text="Invalid..."
android:textAllCaps="true"
android:textColor="#fff"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
Java Files:-
MainActivity.java
package com.parthiv.listviewusinglocaljson.in;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.StringRequest;
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.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.text.InputFilter;
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.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class MainActivity extends FragmentActivity {
Typeface tf;
TextView brandheading, companyheading, inputheading, companyheading1;
ArrayList<adddistrictProduct> products = new ArrayList<adddistrictProduct>();
ArrayList<adddistrictProduct1> products1 = new ArrayList<adddistrictProduct1>();
String fontPath = "fonts/Smoolthan Bold.otf";
String jsonResponse;
int scrolpostionnn = 0;
ProgressDialog mProgressDialog;
int totallength1 = 0, totallength = 0, totallength2 = 0;
String[] pid;
String[] pname;
String dbyc = "c";
String enrponu = "0";
ArrayList<String> searchArrayList, searchArrayList1;
List<HashMap<String, String>> aList;
String[] pid1;
String[] pname1;
String drugno = "", regino = "";
String[] strings;
String[] subs;
String[] stockid;
String finalpincode = "0", finalpincode1 = "0,";
int dbudis = 0;
String productidd;
private DBHelper mHelper;
private SQLiteDatabase dataBase;
String finalproduct;
String finalqty;
String finalmrp;
String finalvalue;
String finaltpts;
String finaltvaluee, finaltvaluee2;
String finalproname, finalpronamew;
TextView textView1;
private DBHelper mHelper12;
private ListView userList;
private ArrayList<String> userId = new ArrayList<String>();
private ArrayList<String> user_fName = new ArrayList<String>();
private ArrayList<String> user_lName = new ArrayList<String>();
private ArrayList<String> user_mrp = new ArrayList<String>();
private ArrayList<String> user_tve = new ArrayList<String>();
private ArrayList<String> user_pts = new ArrayList<String>();
private ArrayList<String> user_tptr = new ArrayList<String>();
private ArrayList<String> user_tpts = new ArrayList<String>();
AutoCompleteTextView addstockistedit;
int addqy;
String value = "";
String value2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// overridePendingTransition(R.anim.move_left_in_activity,
// R.anim.move_left_out_activity);
setContentView(R.layout.mainlay);
this.deleteDatabase(DBHelper.DATABASE_NAME);
tf = Typeface.createFromAsset(getAssets(), fontPath);
userList = (ListView) findViewById(R.id.list22);
addstockistedit = (AutoCompleteTextView) findViewById(R.id.addstockistedit);
addstockistedit.setTypeface(tf);
brandheading = (TextView) findViewById(R.id.brandheading);
brandheading.setTypeface(tf);
try {
SharedPreferences pref = getApplicationContext()
.getSharedPreferences("logcheck", MODE_PRIVATE);
String localout = "2";
JSONArray m_jArry;
if (localout.equals("2")) {
m_jArry = new JSONArray(loadJSONFromAsset1());
} else {
m_jArry = new JSONArray(loadJSONFromAsset());
}
// JSONObject obj = new JSONObject(loadJSONFromAsset());
System.out.println("---" + m_jArry);
System.out.println("---" + m_jArry.length());
searchArrayList = new ArrayList<String>();
stockid = new String[m_jArry.length()];
for (int i = 0; i < m_jArry.length(); i++) {
JSONObject jo_inside = m_jArry.getJSONObject(i);
String formula_value = jo_inside.getString("id");
String formula_value1 = jo_inside.getString("pro_name") + "("
+ jo_inside.getString("packing") + ")";
String formula_value2 = jo_inside.getString("pts");
String formula_value3 = jo_inside.getString("ptr");
String formula_value5s = jo_inside.getString("ptr");
System.out.println("------>" + formula_value1);
System.out.println("------>" + formula_value5s);
mHelper = new DBHelper(MainActivity.this);
dataBase = mHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DBHelper.KEY_PRODUCTID, formula_value);
values.put(DBHelper.KEY_PRODUCTNAME,
jo_inside.getString("pro_name"));
values.put(DBHelper.KEY_QTY, "0");
values.put(DBHelper.KEY_FQTY, "0");
stockid[i] = jo_inside.getString("pro_name") + ":" + i;
searchArrayList.add(jo_inside.getString("pro_name"));
values.put(DBHelper.KEY_PTR, "0");
values.put(DBHelper.KEY_PTS, "0");
values.put(DBHelper.KEY_TPTR, "0");
values.put(DBHelper.KEY_TPTS, "0");
// insert data into database
dataBase.insert(DBHelper.TABLE_NAME, null, values);
}
if (m_jArry.length() == 0) {
} else {
ShowAllContent();
}
} catch (JSONException e) {
e.printStackTrace();
// mProgressDialog.dismiss();
}
// new diseaselist().execute("");
}
private void displayData() {
DBHelper mHelper = new DBHelper(this);
SQLiteDatabase dataBase = mHelper.getWritableDatabase();
Cursor mCursor = dataBase.rawQuery("SELECT * FROM "
+ DBHelper.TABLE_NAME, null);
userId.clear();
user_fName.clear();
user_lName.clear();
user_mrp.clear();
user_tve.clear();
user_pts.clear();
user_tptr.clear();
user_tpts.clear();
if (mCursor.moveToFirst()) {
do {
userId.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_ID)));
user_fName.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_PRODUCTNAME)));
user_lName.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_PTR)));
user_mrp.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_PTS)));
user_tve.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_QTY)));
user_pts.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_FQTY)));
user_tptr.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_TPTR)));
user_tpts.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_TPTS)));
} while (mCursor.moveToNext());
}
// Toast.makeText(getApplicationContext(), ""+scrolpostionnn,
// 1000).show();
// userList.smoothScrollToPosition(scrolpostionnn);
mCursor.close();
int index = userList.getFirstVisiblePosition();
View v = userList.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
productlistDisplayAdapter disadpt = new productlistDisplayAdapter(
MainActivity.this, userId, user_fName, user_lName, user_mrp,
user_tve, user_pts, user_tptr, user_tpts);
disadpt.notifyDataSetChanged();
userList.setAdapter(disadpt);
// restore the position of listview
userList.setSelectionFromTop(index, top);
userList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
/*
* final Dialog viewdialog1 = new Dialog(pobbookingclient.this);
* viewdialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
* viewdialog1.getWindow().getAttributes().windowAnimations =
* R.style.DialogAnimation;
* viewdialog1.getWindow().setBackgroundDrawable( new
* ColorDrawable( android.graphics.Color.TRANSPARENT));
* viewdialog1.setContentView(R.layout.pobquadd);
* viewdialog1.setCancelable(true);
* viewdialog1.setCanceledOnTouchOutside(true); TextView
* heading=(TextView)viewdialog1.findViewById(R.id.heading);
* heading.setTypeface(tf); final EditText
* qty=(EditText)viewdialog1.findViewById(R.id.qty);
*
*
* Button yes=(Button)viewdialog1.findViewById(R.id.ok);
*
* Button no=(Button)viewdialog1.findViewById(R.id.close);
*
* yes.setTypeface(tf); no.setTypeface(tf);
*
*
* qty.setTypeface(tf);
*
* qty.setHint("TYPE CUSTOMER NEEDED QTY");
*
*
*
* yes.setOnClickListener(new OnClickListener() {
*
* @Override public void onClick(View v) { // TODO
* Auto-generated method stub
*
* InputMethodManager inputManager = (InputMethodManager)
* getSystemService(Context.INPUT_METHOD_SERVICE);
*
* inputManager.hideSoftInputFromWindow(getCurrentFocus().
* getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
* title.setFocusable(false);
* title.setFocusableInTouchMode(true); title.clearFocus();
*
* if(qty.getText().toString().equals("")) { Context
* context=getApplicationContext(); LayoutInflater
* inflater=getLayoutInflater(); View customToastroot
* =inflater.inflate(R.layout.fillallfield, null); TextView
* validate
* =(TextView)customToastroot.findViewById(R.id.textView1);
* validate.setText("TYPE CUSTOMER NEEDED QTY..");
* validate.setTypeface(tf); validate.setTextColor(Color.WHITE);
* Toast customtoast=new Toast(context);
* customtoast.setView(customToastroot);
* customtoast.setGravity(Gravity.BOTTOM | Gravity.BOTTOM,0, 0);
* customtoast.setDuration(1000); customtoast.show(); } else {
*
*
* String sqty=""+qty.getText().toString();
*
*
* int qty=Integer.parseInt(sqty);
*
*
* int addqy=qty;
*
*
*
* pobproductadd mHelper = new
* pobproductadd(pobbookingclient.this);
*
* mHelper = new pobproductadd(pobbookingclient.this);
*
* SQLiteDatabase dataBase=mHelper.getWritableDatabase();
* ContentValues values=new ContentValues();
*
*
* values.put(pobproductadd.KEY_PTR,addqy);
* values.put(pobproductadd.KEY_PTS,addqy);
*
*
* dataBase.update(pobproductadd.TABLE_NAME, values,
* pobproductadd.KEY_ID+"="+userId.get(arg2), null);
*
* scrolpostionnn=arg2; displayData(); viewdialog1.dismiss();
*
* }
*
*
*
* } });
*
* no.setOnClickListener(new OnClickListener() {
*
* @Override public void onClick(View v) { // TODO
* Auto-generated method stub InputMethodManager inputManager =
* (InputMethodManager)
* getSystemService(Context.INPUT_METHOD_SERVICE);
*
* inputManager.hideSoftInputFromWindow(getCurrentFocus().
* getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
* viewdialog1.dismiss(); } });
*
*
*
*
* viewdialog1.show();
*/
}
});
}
public class productlistDisplayAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> id;
private ArrayList<String> firstName;
private ArrayList<String> lastName;
private ArrayList<String> mrp;
private ArrayList<String> tv;
private ArrayList<String> pts;
private ArrayList<String> tptr;
private ArrayList<String> tpts;
public productlistDisplayAdapter(Context c, ArrayList<String> id,
ArrayList<String> fname, ArrayList<String> lname,
ArrayList<String> mrp, ArrayList<String> tv,
ArrayList<String> pts, ArrayList<String> tptr,
ArrayList<String> tpts) {
this.mContext = c;
this.id = id;
this.firstName = fname;
this.lastName = lname;
this.mrp = mrp;
this.tv = tv;
this.pts = pts;
this.tptr = tptr;
this.tpts = tpts;
}
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.mainlay_row, null);
mHolder = new Holder();
mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id);
mHolder.txt_fName = (TextView) child
.findViewById(R.id.txt_fName);
child.setTag(mHolder);
} else {
mHolder = (Holder) child.getTag();
}
mHolder.txt_id.setText(id.get(pos));
mHolder.txt_fName.setText("" + firstName.get(pos));
mHolder.txt_fName.setTypeface(tf);
return child;
}
public class Holder {
TextView txt_id;
TextView txt_fName;
}
}
boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Context context = getApplicationContext();
LayoutInflater inflater = getLayoutInflater();
View customToastroot = inflater.inflate(R.layout.fillallfield, null);
TextView validate = (TextView) customToastroot
.findViewById(R.id.textView1);
validate.setText("DOUBLE TAP IN ORDER TO EXIT");
validate.setTypeface(tf);
validate.setTextColor(Color.WHITE);
Toast customtoast = new Toast(context);
customtoast.setView(customToastroot);
customtoast.setGravity(Gravity.BOTTOM | Gravity.BOTTOM, 0, 0);
customtoast.setDuration(1000);
customtoast.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
}
public boolean contains(String haystack, String needle) {
haystack = haystack == null ? "" : haystack;
needle = needle == null ? "" : needle;
// Works, but is not the best.
// return haystack.toLowerCase().indexOf( needle.toLowerCase() ) > -1
return haystack.toLowerCase().contains(needle.toLowerCase());
}
public String loadJSONFromAsset() {
// File yourFile = new File(Environment.getExternalStorageDirectory(),
// "/AYURVEDA ONE/ayurpro.json");
String json = null;
try {
// FileInputStream is = new FileInputStream(yourFile);
// InputStream is = yourFile;
InputStream is = getResources().getAssets().open("product.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
public String loadJSONFromAsset1() {
// File yourFile = new File(Environment.getExternalStorageDirectory(),
// "/AYURVEDA ONE/ayurpro.json");
String json = null;
try {
// FileInputStream is = new FileInputStream(yourFile);
// InputStream is = yourFile;
InputStream is = getResources().getAssets().open("product1.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
private void ShowAllContent() {
AutoCompleteAdapter adapter = new AutoCompleteAdapter(
MainActivity.this, R.layout.searchitem, R.id.textView1,
searchArrayList);
addstockistedit.setTypeface(tf);
addstockistedit.setAdapter(adapter);
addstockistedit.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus()
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
String ss = arg0.getItemAtPosition(arg2) + ":";
// stockistname.setText("PRODUCT SELECTED-"+ss);
String s = "";
for (int i = 0; i < (stockid.length); i++) {
if (contains(stockid[i], ss)) {
s = stockid[i];
}
}
String[] animalsArray = s.split(":");
// Toast.makeText(getApplicationContext(), ""+animalsArray[1],
// 1000).show();
int popo = Integer.parseInt(animalsArray[1]);
userList.smoothScrollToPosition(popo);
addstockistedit.setText("");
}
});
displayData();
}
}
DBHelper.java
package com.parthiv.listviewusinglocaljson.in;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
static String DATABASE_NAME="productadsddaddffddstabaseqce";
public static final String TABLE_NAME="prossddduffddctaddtableqce";
public static final String KEY_PRODUCTID="productid";
public static final String KEY_PRODUCTNAME="productname";
public static final String KEY_QTY="qty";
public static final String KEY_FQTY="fqty";
public static final String KEY_PTS="pts";
public static final String KEY_PTR="ptr";
public static final String KEY_TPTS="tpts";
public static final String KEY_TPTR="tptr";
public static final String KEY_ID="id";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+" ("+KEY_ID+" INTEGER PRIMARY KEY, "+KEY_PRODUCTID+" TEXT, "+KEY_PRODUCTNAME+" TEXT, "+KEY_QTY+" TEXT, "+KEY_FQTY+" TEXT, "+KEY_PTR+" TEXT, "+KEY_PTS+" TEXT, "+KEY_TPTR+" TEXT, "+KEY_TPTS+" TEXT)";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
}
adddistrictProduct.java
package com.parthiv.listviewusinglocaljson.in;
public class adddistrictProduct {
String name;
String price;
int image;
boolean box;
adddistrictProduct(String _describe, String _price, int _image, boolean _box) {
name = _describe;
price = _price;
image = _image;
box = _box;
}
}
adddistrictProduct1.java
package com.parthiv.listviewusinglocaljson.in;
public class adddistrictProduct1 {
String name;
String price;
int image;
boolean box;
adddistrictProduct1(String _describe, String _price, int _image, boolean _box) {
name = _describe;
price = _price;
image = _image;
box = _box;
}
}
mainlay.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#164c1c"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="2dp"
android:gravity="center"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="@+id/addstockistedit"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="@drawable/roundedittext"
android:completionThreshold="1"
android:ems="10"
android:hint="SEARCH PRODUCT"
android:inputType="text"
android:maxLength="30"
android:paddingLeft="10dp"
android:singleLine="true"
android:textColor="#000"
android:textColorHint="#000"
android:textCursorDrawable="@drawable/color_cursor"
android:textSize="12dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginBottom="2dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:orientation="vertical" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginTop="5dp"
android:background="#fff" >
<TextView
android:id="@+id/brandheading"
android:layout_height="20dp"
android:layout_weight="1"
android:gravity="left"
android:padding="2dip"
android:text="PRODUCT"
android:textColor="#000000"
android:textSize="11dp"
android:textStyle="bold"
android:typeface="monospace" />
</TableRow>
<ListView
android:id="@+id/list22"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp" >
</ListView>
</LinearLayout>
</LinearLayout>
mainlay_row.xml
<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"
android:background="#164c1c"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="8dp" >
<TextView
android:id="@+id/txt_id"
android:layout_width="1dp"
android:layout_height="1dp"
android:textColor="#000"
android:visibility="invisible" />
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="5dp" >
<TextView
android:id="@+id/txt_fName"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#164c1c"
android:gravity="left|center"
android:padding="2dip"
android:text="PRODUCT"
android:textColor="#fff"
android:textSize="11dp" />
</TableRow>
</LinearLayout>
searchitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#3b444b"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dp"
android:text="sx"
android:textColor="#fff"
android:textSize="12sp"
android:typeface="serif" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="0"
android:src="@drawable/ic_launcher"
android:visibility="invisible" />
</LinearLayout>
fillallfield.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="5px"
android:background="@drawable/toastround"
android:orientation="horizontal"
android:padding="10dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="3dp"
android:text="Invalid..."
android:textAllCaps="true"
android:textColor="#fff"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
Java Files:-
MainActivity.java
package com.parthiv.listviewusinglocaljson.in;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.StringRequest;
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.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.text.InputFilter;
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.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class MainActivity extends FragmentActivity {
Typeface tf;
TextView brandheading, companyheading, inputheading, companyheading1;
ArrayList<adddistrictProduct> products = new ArrayList<adddistrictProduct>();
ArrayList<adddistrictProduct1> products1 = new ArrayList<adddistrictProduct1>();
String fontPath = "fonts/Smoolthan Bold.otf";
String jsonResponse;
int scrolpostionnn = 0;
ProgressDialog mProgressDialog;
int totallength1 = 0, totallength = 0, totallength2 = 0;
String[] pid;
String[] pname;
String dbyc = "c";
String enrponu = "0";
ArrayList<String> searchArrayList, searchArrayList1;
List<HashMap<String, String>> aList;
String[] pid1;
String[] pname1;
String drugno = "", regino = "";
String[] strings;
String[] subs;
String[] stockid;
String finalpincode = "0", finalpincode1 = "0,";
int dbudis = 0;
String productidd;
private DBHelper mHelper;
private SQLiteDatabase dataBase;
String finalproduct;
String finalqty;
String finalmrp;
String finalvalue;
String finaltpts;
String finaltvaluee, finaltvaluee2;
String finalproname, finalpronamew;
TextView textView1;
private DBHelper mHelper12;
private ListView userList;
private ArrayList<String> userId = new ArrayList<String>();
private ArrayList<String> user_fName = new ArrayList<String>();
private ArrayList<String> user_lName = new ArrayList<String>();
private ArrayList<String> user_mrp = new ArrayList<String>();
private ArrayList<String> user_tve = new ArrayList<String>();
private ArrayList<String> user_pts = new ArrayList<String>();
private ArrayList<String> user_tptr = new ArrayList<String>();
private ArrayList<String> user_tpts = new ArrayList<String>();
AutoCompleteTextView addstockistedit;
int addqy;
String value = "";
String value2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// overridePendingTransition(R.anim.move_left_in_activity,
// R.anim.move_left_out_activity);
setContentView(R.layout.mainlay);
this.deleteDatabase(DBHelper.DATABASE_NAME);
tf = Typeface.createFromAsset(getAssets(), fontPath);
userList = (ListView) findViewById(R.id.list22);
addstockistedit = (AutoCompleteTextView) findViewById(R.id.addstockistedit);
addstockistedit.setTypeface(tf);
brandheading = (TextView) findViewById(R.id.brandheading);
brandheading.setTypeface(tf);
try {
SharedPreferences pref = getApplicationContext()
.getSharedPreferences("logcheck", MODE_PRIVATE);
String localout = "2";
JSONArray m_jArry;
if (localout.equals("2")) {
m_jArry = new JSONArray(loadJSONFromAsset1());
} else {
m_jArry = new JSONArray(loadJSONFromAsset());
}
// JSONObject obj = new JSONObject(loadJSONFromAsset());
System.out.println("---" + m_jArry);
System.out.println("---" + m_jArry.length());
searchArrayList = new ArrayList<String>();
stockid = new String[m_jArry.length()];
for (int i = 0; i < m_jArry.length(); i++) {
JSONObject jo_inside = m_jArry.getJSONObject(i);
String formula_value = jo_inside.getString("id");
String formula_value1 = jo_inside.getString("pro_name") + "("
+ jo_inside.getString("packing") + ")";
String formula_value2 = jo_inside.getString("pts");
String formula_value3 = jo_inside.getString("ptr");
String formula_value5s = jo_inside.getString("ptr");
System.out.println("------>" + formula_value1);
System.out.println("------>" + formula_value5s);
mHelper = new DBHelper(MainActivity.this);
dataBase = mHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DBHelper.KEY_PRODUCTID, formula_value);
values.put(DBHelper.KEY_PRODUCTNAME,
jo_inside.getString("pro_name"));
values.put(DBHelper.KEY_QTY, "0");
values.put(DBHelper.KEY_FQTY, "0");
stockid[i] = jo_inside.getString("pro_name") + ":" + i;
searchArrayList.add(jo_inside.getString("pro_name"));
values.put(DBHelper.KEY_PTR, "0");
values.put(DBHelper.KEY_PTS, "0");
values.put(DBHelper.KEY_TPTR, "0");
values.put(DBHelper.KEY_TPTS, "0");
// insert data into database
dataBase.insert(DBHelper.TABLE_NAME, null, values);
}
if (m_jArry.length() == 0) {
} else {
ShowAllContent();
}
} catch (JSONException e) {
e.printStackTrace();
// mProgressDialog.dismiss();
}
// new diseaselist().execute("");
}
private void displayData() {
DBHelper mHelper = new DBHelper(this);
SQLiteDatabase dataBase = mHelper.getWritableDatabase();
Cursor mCursor = dataBase.rawQuery("SELECT * FROM "
+ DBHelper.TABLE_NAME, null);
userId.clear();
user_fName.clear();
user_lName.clear();
user_mrp.clear();
user_tve.clear();
user_pts.clear();
user_tptr.clear();
user_tpts.clear();
if (mCursor.moveToFirst()) {
do {
userId.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_ID)));
user_fName.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_PRODUCTNAME)));
user_lName.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_PTR)));
user_mrp.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_PTS)));
user_tve.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_QTY)));
user_pts.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_FQTY)));
user_tptr.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_TPTR)));
user_tpts.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_TPTS)));
} while (mCursor.moveToNext());
}
// Toast.makeText(getApplicationContext(), ""+scrolpostionnn,
// 1000).show();
// userList.smoothScrollToPosition(scrolpostionnn);
mCursor.close();
int index = userList.getFirstVisiblePosition();
View v = userList.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
productlistDisplayAdapter disadpt = new productlistDisplayAdapter(
MainActivity.this, userId, user_fName, user_lName, user_mrp,
user_tve, user_pts, user_tptr, user_tpts);
disadpt.notifyDataSetChanged();
userList.setAdapter(disadpt);
// restore the position of listview
userList.setSelectionFromTop(index, top);
userList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
/*
* final Dialog viewdialog1 = new Dialog(pobbookingclient.this);
* viewdialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
* viewdialog1.getWindow().getAttributes().windowAnimations =
* R.style.DialogAnimation;
* viewdialog1.getWindow().setBackgroundDrawable( new
* ColorDrawable( android.graphics.Color.TRANSPARENT));
* viewdialog1.setContentView(R.layout.pobquadd);
* viewdialog1.setCancelable(true);
* viewdialog1.setCanceledOnTouchOutside(true); TextView
* heading=(TextView)viewdialog1.findViewById(R.id.heading);
* heading.setTypeface(tf); final EditText
* qty=(EditText)viewdialog1.findViewById(R.id.qty);
*
*
* Button yes=(Button)viewdialog1.findViewById(R.id.ok);
*
* Button no=(Button)viewdialog1.findViewById(R.id.close);
*
* yes.setTypeface(tf); no.setTypeface(tf);
*
*
* qty.setTypeface(tf);
*
* qty.setHint("TYPE CUSTOMER NEEDED QTY");
*
*
*
* yes.setOnClickListener(new OnClickListener() {
*
* @Override public void onClick(View v) { // TODO
* Auto-generated method stub
*
* InputMethodManager inputManager = (InputMethodManager)
* getSystemService(Context.INPUT_METHOD_SERVICE);
*
* inputManager.hideSoftInputFromWindow(getCurrentFocus().
* getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
* title.setFocusable(false);
* title.setFocusableInTouchMode(true); title.clearFocus();
*
* if(qty.getText().toString().equals("")) { Context
* context=getApplicationContext(); LayoutInflater
* inflater=getLayoutInflater(); View customToastroot
* =inflater.inflate(R.layout.fillallfield, null); TextView
* validate
* =(TextView)customToastroot.findViewById(R.id.textView1);
* validate.setText("TYPE CUSTOMER NEEDED QTY..");
* validate.setTypeface(tf); validate.setTextColor(Color.WHITE);
* Toast customtoast=new Toast(context);
* customtoast.setView(customToastroot);
* customtoast.setGravity(Gravity.BOTTOM | Gravity.BOTTOM,0, 0);
* customtoast.setDuration(1000); customtoast.show(); } else {
*
*
* String sqty=""+qty.getText().toString();
*
*
* int qty=Integer.parseInt(sqty);
*
*
* int addqy=qty;
*
*
*
* pobproductadd mHelper = new
* pobproductadd(pobbookingclient.this);
*
* mHelper = new pobproductadd(pobbookingclient.this);
*
* SQLiteDatabase dataBase=mHelper.getWritableDatabase();
* ContentValues values=new ContentValues();
*
*
* values.put(pobproductadd.KEY_PTR,addqy);
* values.put(pobproductadd.KEY_PTS,addqy);
*
*
* dataBase.update(pobproductadd.TABLE_NAME, values,
* pobproductadd.KEY_ID+"="+userId.get(arg2), null);
*
* scrolpostionnn=arg2; displayData(); viewdialog1.dismiss();
*
* }
*
*
*
* } });
*
* no.setOnClickListener(new OnClickListener() {
*
* @Override public void onClick(View v) { // TODO
* Auto-generated method stub InputMethodManager inputManager =
* (InputMethodManager)
* getSystemService(Context.INPUT_METHOD_SERVICE);
*
* inputManager.hideSoftInputFromWindow(getCurrentFocus().
* getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
* viewdialog1.dismiss(); } });
*
*
*
*
* viewdialog1.show();
*/
}
});
}
public class productlistDisplayAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> id;
private ArrayList<String> firstName;
private ArrayList<String> lastName;
private ArrayList<String> mrp;
private ArrayList<String> tv;
private ArrayList<String> pts;
private ArrayList<String> tptr;
private ArrayList<String> tpts;
public productlistDisplayAdapter(Context c, ArrayList<String> id,
ArrayList<String> fname, ArrayList<String> lname,
ArrayList<String> mrp, ArrayList<String> tv,
ArrayList<String> pts, ArrayList<String> tptr,
ArrayList<String> tpts) {
this.mContext = c;
this.id = id;
this.firstName = fname;
this.lastName = lname;
this.mrp = mrp;
this.tv = tv;
this.pts = pts;
this.tptr = tptr;
this.tpts = tpts;
}
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.mainlay_row, null);
mHolder = new Holder();
mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id);
mHolder.txt_fName = (TextView) child
.findViewById(R.id.txt_fName);
child.setTag(mHolder);
} else {
mHolder = (Holder) child.getTag();
}
mHolder.txt_id.setText(id.get(pos));
mHolder.txt_fName.setText("" + firstName.get(pos));
mHolder.txt_fName.setTypeface(tf);
return child;
}
public class Holder {
TextView txt_id;
TextView txt_fName;
}
}
boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Context context = getApplicationContext();
LayoutInflater inflater = getLayoutInflater();
View customToastroot = inflater.inflate(R.layout.fillallfield, null);
TextView validate = (TextView) customToastroot
.findViewById(R.id.textView1);
validate.setText("DOUBLE TAP IN ORDER TO EXIT");
validate.setTypeface(tf);
validate.setTextColor(Color.WHITE);
Toast customtoast = new Toast(context);
customtoast.setView(customToastroot);
customtoast.setGravity(Gravity.BOTTOM | Gravity.BOTTOM, 0, 0);
customtoast.setDuration(1000);
customtoast.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
}
public boolean contains(String haystack, String needle) {
haystack = haystack == null ? "" : haystack;
needle = needle == null ? "" : needle;
// Works, but is not the best.
// return haystack.toLowerCase().indexOf( needle.toLowerCase() ) > -1
return haystack.toLowerCase().contains(needle.toLowerCase());
}
public String loadJSONFromAsset() {
// File yourFile = new File(Environment.getExternalStorageDirectory(),
// "/AYURVEDA ONE/ayurpro.json");
String json = null;
try {
// FileInputStream is = new FileInputStream(yourFile);
// InputStream is = yourFile;
InputStream is = getResources().getAssets().open("product.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
public String loadJSONFromAsset1() {
// File yourFile = new File(Environment.getExternalStorageDirectory(),
// "/AYURVEDA ONE/ayurpro.json");
String json = null;
try {
// FileInputStream is = new FileInputStream(yourFile);
// InputStream is = yourFile;
InputStream is = getResources().getAssets().open("product1.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
private void ShowAllContent() {
AutoCompleteAdapter adapter = new AutoCompleteAdapter(
MainActivity.this, R.layout.searchitem, R.id.textView1,
searchArrayList);
addstockistedit.setTypeface(tf);
addstockistedit.setAdapter(adapter);
addstockistedit.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus()
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
String ss = arg0.getItemAtPosition(arg2) + ":";
// stockistname.setText("PRODUCT SELECTED-"+ss);
String s = "";
for (int i = 0; i < (stockid.length); i++) {
if (contains(stockid[i], ss)) {
s = stockid[i];
}
}
String[] animalsArray = s.split(":");
// Toast.makeText(getApplicationContext(), ""+animalsArray[1],
// 1000).show();
int popo = Integer.parseInt(animalsArray[1]);
userList.smoothScrollToPosition(popo);
addstockistedit.setText("");
}
});
displayData();
}
}
DBHelper.java
package com.parthiv.listviewusinglocaljson.in;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
static String DATABASE_NAME="productadsddaddffddstabaseqce";
public static final String TABLE_NAME="prossddduffddctaddtableqce";
public static final String KEY_PRODUCTID="productid";
public static final String KEY_PRODUCTNAME="productname";
public static final String KEY_QTY="qty";
public static final String KEY_FQTY="fqty";
public static final String KEY_PTS="pts";
public static final String KEY_PTR="ptr";
public static final String KEY_TPTS="tpts";
public static final String KEY_TPTR="tptr";
public static final String KEY_ID="id";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+" ("+KEY_ID+" INTEGER PRIMARY KEY, "+KEY_PRODUCTID+" TEXT, "+KEY_PRODUCTNAME+" TEXT, "+KEY_QTY+" TEXT, "+KEY_FQTY+" TEXT, "+KEY_PTR+" TEXT, "+KEY_PTS+" TEXT, "+KEY_TPTR+" TEXT, "+KEY_TPTS+" TEXT)";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
}
adddistrictProduct.java
package com.parthiv.listviewusinglocaljson.in;
public class adddistrictProduct {
String name;
String price;
int image;
boolean box;
adddistrictProduct(String _describe, String _price, int _image, boolean _box) {
name = _describe;
price = _price;
image = _image;
box = _box;
}
}
adddistrictProduct1.java
package com.parthiv.listviewusinglocaljson.in;
public class adddistrictProduct1 {
String name;
String price;
int image;
boolean box;
adddistrictProduct1(String _describe, String _price, int _image, boolean _box) {
name = _describe;
price = _price;
image = _image;
box = _box;
}
}
AutoCompleteAdapter.java
package com.parthiv.listviewusinglocaljson.in;
import java.util.ArrayList;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
public class AutoCompleteAdapter extends ArrayAdapter<String> implements
Filterable {
private ArrayList<String> fullList;
private ArrayList<String> mOriginalValues;
private ArrayFilter mFilter;
public AutoCompleteAdapter(Context context, int resource,
int textViewResourceId, ArrayList<String> fullList) {
super(context, resource, textViewResourceId, fullList);
this.fullList = fullList;
mOriginalValues = new ArrayList<String>(fullList);
}
@Override
public int getCount() {
return fullList.size();
}
@Override
public String getItem(int position) {
return fullList.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ArrayFilter();
}
return mFilter;
}
private class ArrayFilter extends Filter {
private Object lock;
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if (mOriginalValues == null) {
synchronized (lock) {
mOriginalValues = new ArrayList<String>(fullList);
}
}
if (prefix == null || prefix.length() == 0) {
synchronized (lock) {
ArrayList<String> list = new ArrayList<String>(
mOriginalValues);
results.values = list;
results.count = list.size();
}
} else {
final String prefixString = prefix.toString().toLowerCase();
ArrayList<String> values = mOriginalValues;
int count = values.size();
ArrayList<String> newValues = new ArrayList<String>(count);
for (int i = 0; i < count; i++) {
String item = values.get(i);
if (item.toLowerCase().contains(prefixString)) {
newValues.add(item);
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
if (results.values != null) {
fullList = (ArrayList<String>) results.values;
} else {
fullList = new ArrayList<String>();
}
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}
Screenshots:-
DOWNLOAD ListViewusingLocalJson.zip
No comments:
Post a Comment