Shruti Shukla

Lurker
Apr 7, 2012
7
0
25
public class HorizontialListView extends AdapterView<ListAdapter> {

public boolean mAlwaysOverrideTouch = true;
protected ListAdapter mAdapter;
private int mLeftViewIndex = -1;
private int mRightViewIndex = 0;
protected int mCurrentX;
protected int mNextX;
private int mMaxX = Integer.MAX_VALUE;
private int mDisplayOffset = 0;
protected Scroller mScroller;
private GestureDetector mGesture;
private Queue<View> mRemovedViewQueue = new LinkedList<View>();
private OnItemSelectedListener mOnItemSelected;
private OnItemClickListener mOnItemClicked;
private boolean mDataChanged = false;


public HorizontialListView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}

private synchronized void initView() {
mLeftViewIndex = -1;
mRightViewIndex = 0;
mDisplayOffset = 0;
mCurrentX = 0;
mNextX = 0;
mMaxX = Integer.MAX_VALUE;
mScroller = new Scroller(getContext());
mGesture = new GestureDetector(getContext(), mOnGesture);
}

@Override
public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener) {
mOnItemSelected = listener;
}

@Override
public void setOnItemClickListener(AdapterView.OnItemClickListener listener){
mOnItemClicked = listener;
}

private DataSetObserver mDataObserver = new DataSetObserver() {

@Override
public void onChanged() {
synchronized(HorizontialListView.this){
mDataChanged = true;
}
invalidate();
requestLayout();
}

@Override
public void onInvalidated() {
reset();
invalidate();
requestLayout();
}

};

@Override
public ListAdapter getAdapter() {
return mAdapter;
}

@Override
public View getSelectedView() {
//TODO: implement
return null;
}

@Override
public void setAdapter(ListAdapter adapter) {
if(mAdapter != null) {
mAdapter.unregisterDataSetObserver(mDataObserver);
}
mAdapter = adapter;
mAdapter.registerDataSetObserver(mDataObserver);
reset();
}

private synchronized void reset(){
initView();
removeAllViewsInLayout();
requestLayout();
}

@Override
public void setSelection(int position) {
//TODO: implement
}

private void addAndMeasureChild(final View child, int viewPos) {
LayoutParams params = child.getLayoutParams();
if(params == null) {
params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
}

addViewInLayout(child, viewPos, params, true);
child.measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST),
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.AT_MOST));
}

@Override
protected synchronized void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);

if(mAdapter == null){
return;
}

if(mDataChanged){
int oldCurrentX = mCurrentX;
initView();
removeAllViewsInLayout();
mNextX = oldCurrentX;
mDataChanged = false;
}

if(mScroller.computeScrollOffset()){
int scrollx = mScroller.getCurrX();
mNextX = scrollx;
}

if(mNextX < 0){
mNextX = 0;
mScroller.forceFinished(true);
}
if(mNextX > mMaxX) {
mNextX = mMaxX;
mScroller.forceFinished(true);
}

int dx = mCurrentX - mNextX;

removeNonVisibleItems(dx);
fillList(dx);
positionItems(dx);

mCurrentX = mNextX;

if(!mScroller.isFinished()){
post(new Runnable(){
@Override
public void run() {
requestLayout();
}
});

}
}

private void fillList(final int dx) {
int edge = 0;
View child = getChildAt(getChildCount()-1);
if(child != null) {
edge = child.getRight();
}
fillListRight(edge, dx);

edge = 0;
child = getChildAt(0);
if(child != null) {
edge = child.getLeft();
}
fillListLeft(edge, dx);


}

private void fillListRight(int rightEdge, final int dx) {
while(rightEdge + dx < getWidth() && mRightViewIndex < mAdapter.getCount()) {

View child = mAdapter.getView(mRightViewIndex, mRemovedViewQueue.poll(), this);
addAndMeasureChild(child, -1);
rightEdge += child.getMeasuredWidth();

if(mRightViewIndex == mAdapter.getCount()-1){
mMaxX = mCurrentX + rightEdge - getWidth();
}
mRightViewIndex++;
}

}

private void fillListLeft(int leftEdge, final int dx) {
while(leftEdge + dx > 0 && mLeftViewIndex >= 0) {
View child = mAdapter.getView(mLeftViewIndex, mRemovedViewQueue.poll(), this);
addAndMeasureChild(child, 0);
leftEdge -= child.getMeasuredWidth();
mLeftViewIndex--;
mDisplayOffset -= child.getMeasuredWidth();
}
}

private void removeNonVisibleItems(final int dx) {
View child = getChildAt(0);
while(child != null && child.getRight() + dx <= 0) {
mDisplayOffset += child.getMeasuredWidth();
mRemovedViewQueue.offer(child);
removeViewInLayout(child);
mLeftViewIndex++;
child = getChildAt(0);

}

child = getChildAt(getChildCount()-1);
while(child != null && child.getLeft() + dx >= getWidth()) {
mRemovedViewQueue.offer(child);
removeViewInLayout(child);
mRightViewIndex--;
child = getChildAt(getChildCount()-1);
}
}

private void positionItems(final int dx) {
if(getChildCount() > 0){
mDisplayOffset += dx;
int left = mDisplayOffset;
for(int i=0;i<getChildCount();i++){
View child = getChildAt(i);
int childWidth = child.getMeasuredWidth();
child.layout(left, 0, left + childWidth, child.getMeasuredHeight());
left += childWidth;
}
}
}

public synchronized void scrollTo(int x) {
mScroller.startScroll(mNextX, 0, x - mNextX, 0);
requestLayout();
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
boolean handled = mGesture.onTouchEvent(ev);
return handled;
}

protected boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
synchronized(HorizontialListView.this){
mScroller.fling(mNextX, 0, (int)-velocityX, 0, 0, mMaxX, 0, 0);
}
requestLayout();

return true;
}

protected boolean onDown(MotionEvent e) {
mScroller.forceFinished(true);
return true;
}

private OnGestureListener mOnGesture = new GestureDetector.SimpleOnGestureListener() {

@Override
public boolean onDown(MotionEvent e) {
return HorizontialListView.this.onDown(e);
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return HorizontialListView.this.onFling(e1, e2, velocityX, velocityY);
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {

synchronized(HorizontialListView.this){
mNextX += (int)distanceX;
}
requestLayout();

return true;
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Rect viewRect = new Rect();
for(int i=0;i<getChildCount();i++){
View child = getChildAt(i);
int left = child.getLeft();
int right = child.getRight();
int top = child.getTop();
int bottom = child.getBottom();
viewRect.set(left, top, right, bottom);
if(viewRect.contains((int)e.getX(), (int)e.getY())){
if(mOnItemClicked != null){
mOnItemClicked.onItemClick(HorizontialListView.this, child, mLeftViewIndex + 1 + i, mAdapter.getItemId( mLeftViewIndex + 1 + i ));
}
if(mOnItemSelected != null){
mOnItemSelected.onItemSelected(HorizontialListView.this, child, mLeftViewIndex + 1 + i, mAdapter.getItemId( mLeftViewIndex + 1 + i ));
}
break;
}

}
return true;
}



};



}
 
public class DataBaseHelper extends SQLiteOpenHelper {
Cursor cur, cur2;
ArrayList<ArrayList<String>> data;
public static int limit = 20;
private static String DB_PATH = "/data/data/com.imageuploader/databases/";

private static String DB_NAME = "Your database name";

public static SQLiteDatabase myDataBase;
private SQLiteStatement insertStmt;
private Context myContext;
static DataBaseHelper con;

String insertUser;

public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
con = this;
}

public void createDataBase() throws IOException {
try{
boolean dbExist = checkDataBase();
if (dbExist) {
// do nothing - database already exist
} else {
// By calling this method and empty database will be created into
// the default system path
// of your application so we are gonna be able to overwrite that
// database with our database.
this.getReadableDatabase();

try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}catch (IllegalStateException e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("IllegalStateException got at in file manager");
}
}

private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
// database does't exist yet.
System.out.println("SQLiteException got at in file manager");
}catch (IllegalStateException e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("IllegalStateException got at in file manager");
}

if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}

private void copyDataBase() throws IOException {
try{
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}catch (IllegalStateException e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("IllegalStateException got at in file manager");
}
}

public void openDataBase() throws SQLException {
// Open the database
try{
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
}catch (IllegalStateException e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("IllegalStateException got at in file manager");
}
}


public SelectResponseData[] get_Data() {
// public ArrayList<ArrayList<String>> get_add_city_Data() {
PendingResponse[] pd_resp = null;
// ArrayList<ArrayList<String>> data1 = new ArrayList<ArrayList<String>>();
// boolean success = false;
try{
openDataBase();
//todaycmmnted //myDataBase = this.getReadableDatabase();
// openDataBase();

try {
String select = "Select address,city,zip,ID,color from Tabler_p where(username='"+username+"') order by(color) ";
Cursor cursor = myDataBase.rawQuery(select, null);
// Cursor cursor = db.query("PENDING", new String[] { "address",
// "city", "zip", "ID","color" }, null, null, null, null, "color", null);
pd_resp = new PendingResponse[cursor.getCount()];
if (cursor.moveToFirst()) {
do {

ArrayList<String> user_id_pass = new ArrayList<String>();
user_id_pass.add(cursor.getString(0));
user_id_pass.add(cursor.getString(1));
user_id_pass.add(cursor.getString(2));
user_id_pass.add(cursor.getString(3));
user_id_pass.add(cursor.getString(4));

// data1.add(user_id_pass);
pd_resp[cursor.getPosition()]=new PendingResponse(user_id_pass);

} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}catch (IllegalStateException e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("IllegalStateException got at in file manager");
}
return pd_resp;
// return data1;
}

@Override
public synchronized void close() {
try{
if (myDataBase != null) {
myDataBase.close();
}
}catch (IllegalStateException e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("IllegalStateException got at in file manager");
}
super.close();

}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}


// for inserting in filemanager
public void insert_Details(String ID, String username,
String Address, String City, String State, String Zip,
String house, String location, ArrayList<String> photos,
int total_images) {
try{
String pics = "";
for (int i = 0; i < photos.size(); i++) {
pics = pics + photos.get(i);
if (i != photos.size() - 1) {
pics = pics + ",";
}
}
myDataBase = this.getWritableDatabase();

String sql1 = "insert into FILE_Table('ID','username','address','city','state','zip','housestyle','location','photos','total') values (?,?,?,?,?,?,?,?,?,?)";

this.insertStmt = myDataBase.compileStatement(sql1);
this.insertStmt.bindString(1, ID);
this.insertStmt.bindString(2, username);
this.insertStmt.bindString(3, Address);
this.insertStmt.bindString(4, City);
this.insertStmt.bindString(5, State);
this.insertStmt.bindString(6, Zip);
this.insertStmt.bindString(7, house);
this.insertStmt.bindString(8, location);
this.insertStmt.bindString(9, pics);
this.insertStmt.bindLong(10, total_images);
try {
Log.i("sql1=", sql1);

this.insertStmt.executeInsert();
// db.execSQL(sql1);

if(GlobalVariable.syncflag!=1)
{
Toast toast = Toast.makeText(myContext,"Successfully added to file manager ", Toast.LENGTH_SHORT);
toast.show();
}


} catch (SQLException e) {
e.printStackTrace();
Toast toast = Toast.makeText(myContext, "Exception",
Toast.LENGTH_SHORT);
// toast.show();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
if (GlobalVariable.syncflag == 1) {
// delete query to delete record from sync table
delete_sync_record(GlobalVariable.username,Address, City);
}
if (GlobalVariable.pendingflag == 1) {
// delete query to delete record from sync table
delete_pending_record(Address, City);
}
}catch (IllegalStateException e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("IllegalStateException got at in file manager");
}
}

// --------insert into sync



public ArrayList<String> selectAllCityFromTable() {

ArrayList<String> list1 = new ArrayList<String>();
try{
openDataBase();
String TABLE_NAME = "CITY_TABLE";
Cursor cursor = myDataBase.query(TABLE_NAME, null, null, null, null,
null, null);

cursor.moveToFirst();

while (cursor.isAfterLast() == false) { // String same=cur.getString(1);

list1.add(cursor.getString(0));
cursor.moveToNext();

}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}catch (IllegalStateException e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("IllegalStateException got at in file manager");
}
return list1;
}


// ---------for update username---------------//
public void updateUsernametoSDB(String name) {
try {
myDataBase = this.getWritableDatabase();

String insertSTARTUP = "update USERNAME set username='" + name
+ "'";

myDataBase.execSQL(insertSTARTUP);

}
catch (IllegalStateException e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("IllegalStateException got at in file manager");
}
catch (Exception er) {
er.printStackTrace();
}

}

// ----------------for delete sync-----
public void delete_record(String username ,String Address, String City) {

try {
// Cursor cur = myDataBase.query("SYNC", null, null, null,
// null, null, null);
// int count = cur.getCount();
// cur.moveToFirst();
myDataBase = this.getWritableDatabase();
// for(int j=0;j<Add_city.size();j++)
// {
// String Address = Add_city.get(j).get(0);
// String City = Add_city.get(j).get(1);
// for (int i = 0; i < count; i++) {
// if(cur.getString(i).equals(rowid))
String where = "address='" + Address + "' and city='" + City + "' and username ='"+username+"' ";
myDataBase.delete("SYNC", where, null);

// }
// }
if (cur != null && !cur.isClosed()) {
cur.close();
}

}
catch (IllegalStateException e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("IllegalStateException got at in file manager");
}
catch (Exception e) {
e.printStackTrace();
}
// return "ok";

}