Search With ListView Using Volley

Search With ListView Using Volley List Activity And Excel Download:-

        The idea is to have an Edittext above the ListView and when the user types a search query,the ListView filters itself to show only the relevant results;Like this:

Xml files:

listviewmain.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="#26001f"
    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 Android Version"
            android:paddingLeft="10dp"
            android:textColor="#000"
            android:textColorHint="#000" />
    </LinearLayout>

    <ListView
        android:id="@+id/android: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>

listviewmain_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="#26001f"
    android:orientation="horizontal"
    android:padding="5dip" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        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/version"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="version"
            android:textColor="#fff"
            android:textSize="18dip"
            android:textStyle="bold" />

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


</LinearLayout>

Java files:

ListViewSearchActivity.java

package com.searchviewwithlist.parthiv;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;

import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

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.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListViewSearchActivity extends ListActivity {

String jsonResponse;

private String[] version;
private String[] imgurl;
File file;
WritableSheet sheet;
WritableWorkbook workbook;

Typeface tf;
String fontPath = "fonts/Smoolthan Bold.otf";
int totallength1 = 0;
private ListView listview;
private EditText searchview;

private ArrayList<String> user_version;
private ArrayList<String> user_url;

ProgressDialog mProgressDialog;
int textlength = 0;

@Override
protected 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.listviewmain);

tf = Typeface.createFromAsset(getAssets(), fontPath);
listview = (ListView) findViewById(android.R.id.list);
searchview = (EditText) findViewById(R.id.search);
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
new approveddoctor().execute("");

FloatingActionButton fabButton = new FloatingActionButton.Builder(
ListViewSearchActivity.this)
.withDrawable(getResources().getDrawable(R.drawable.gridgreen))
.withButtonColor(Color.parseColor("#FFFFFF"))
.withGravity(Gravity.BOTTOM | Gravity.RIGHT)
.withMargins(0, 0, 16, 16).create();
fabButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Uri uri = Uri
.parse("http://androidtutorialsmirror.blogspot.in/p/home.html"); // missing
// 'http://'
// will
// cause
// crashed
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

}
});

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();

user_version.clear();
user_url.clear();

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

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

user_version.add(version[i]);
user_url.add(imgurl[i]);

}
}
}

AppendList(user_version, user_url);
}
});

}

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

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

}

protected String doInBackground(String... params) {

String urlJsonArry1 = "http://androidblog.esy.es/ImageJsonData.php?";

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

// xl report
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings
.setLocale(new Locale("en", "EN"));
int a = 1;
try {
workbook = Workbook.createWorkbook(
file, wbSettings);
// workbook.createSheet("Report", 0);
sheet = workbook.createSheet(
"First Sheet", 0);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

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

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

String sectioid = person
.getString("image_title");
String docid = person
.getString("image_url");

try {
sheet.addCell(new jxl.write.Label(
0,
(i + 1),
person.getString(
"image_title")
.toUpperCase(),
createFormatCellStatus(true)));
sheet.addCell(new jxl.write.Label(
1,
(i + 1),
person.getString(
"image_url")
.toUpperCase(),
createFormatCellStatus(true)));

} catch (RowsExceededException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (WriteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
sheet.addCell(new jxl.write.Label(
0,
0,
"TITLE",
createFormatCellStatus2(true)));
sheet.addCell(new jxl.write.Label(
1,
0,
"URL",
createFormatCellStatus2(true)));

} catch (RowsExceededException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (WriteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

version[i] = sectioid;
imgurl[i] = docid;

}

if (totallength1 == 0) {

Toast.makeText(getApplicationContext(),
"No details has been found...",
1000).show();
mProgressDialog.dismiss();
searchview.setFocusable(false);
searchview.setClickable(false);
finish();

} else {

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

Toast.makeText(getApplicationContext(),
"Problem with internet...", 1000)
.show();
mProgressDialog.dismiss();
searchview.setFocusable(false);
searchview.setClickable(false);

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

Toast.makeText(getApplicationContext(),
"Problem with internet...", 1000)
.show();
mProgressDialog.dismiss();
searchview.setFocusable(false);
searchview.setClickable(false);

}
});

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

return null;
}

private void ShowAllContent() {
mProgressDialog.dismiss();

try {
workbook.write();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
try {
workbook.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

user_version = new ArrayList<String>(Arrays.asList(version));
user_url = new ArrayList<String>(Arrays.asList(imgurl));

setListAdapter(new baseadapter(ListViewSearchActivity.this));
}

}

public void AppendList(ArrayList<String> str, ArrayList<String> array_sort2) {
setListAdapter(new baseadapter(this));
}

public class baseadapter extends BaseAdapter {
Activity cntx;

public baseadapter(Activity context) {
// TODO Auto-generated constructor stub
this.cntx = context;
}

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

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

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

public View getView(final int position, View convertView,
ViewGroup parent) {
View row = null;

LayoutInflater inflater = cntx.getLayoutInflater();
row = inflater.inflate(R.layout.listviewmain_row, null);

TextView androidversion = (TextView) row.findViewById(R.id.version);
TextView versionimage = (TextView) row.findViewById(R.id.imageurl);

androidversion.setTypeface(tf);
versionimage.setTypeface(tf);

androidversion.setText("" + user_version.get(position));
versionimage.setText("" + user_url.get(position));
listview.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"" + user_version.get(arg2), 1000).show();
}
});

return row;
}
}

public WritableCellFormat createFormatCellStatus(boolean b)
throws WriteException {
Colour colour = (b == true) ? Colour.GREEN : Colour.RED;
WritableFont wfontStatus = new WritableFont(
WritableFont.createFont("Arial"),
WritableFont.DEFAULT_POINT_SIZE, WritableFont.BOLD, false,
UnderlineStyle.NO_UNDERLINE, colour);
WritableCellFormat fCellstatus = new WritableCellFormat(wfontStatus);

fCellstatus.setWrap(true);
fCellstatus.setAlignment(jxl.format.Alignment.CENTRE);
fCellstatus.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
fCellstatus.setBorder(jxl.format.Border.ALL,
jxl.format.BorderLineStyle.MEDIUM, jxl.format.Colour.BLACK);
return fCellstatus;
}

public WritableCellFormat createFormatCellStatus2(boolean b)
throws WriteException {
Colour colour = (b == true) ? Colour.BLACK : Colour.BLACK;
WritableFont wfontStatus = new WritableFont(
WritableFont.createFont("Arial"),
WritableFont.DEFAULT_POINT_SIZE, WritableFont.BOLD, false,
UnderlineStyle.NO_UNDERLINE, colour);
WritableCellFormat fCellstatus = new WritableCellFormat(wfontStatus);

fCellstatus.setWrap(true);
fCellstatus.setAlignment(jxl.format.Alignment.CENTRE);
fCellstatus.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
fCellstatus.setBorder(jxl.format.Border.ALL,
jxl.format.BorderLineStyle.MEDIUM, jxl.format.Colour.GREEN);
return fCellstatus;
}

@Override
public void onResume() {
super.onResume();
// put your code here...
String Fnamexls = "TOTAL ANDROID VERSION LIST" + ".xls";
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File(sdCard.getAbsolutePath()
+ "/EXCEL PARTHIV ANDROID LIST");
directory.mkdirs();
file = new File(directory, Fnamexls);
Toast.makeText(getApplicationContext(),
"Android Versions XL Report Succesfully Downloaded..", 1000)
.show();
}
}


Output Screens:











keep updating Everyday.....

No comments:

Post a Comment

back to top