Apps "Search by" after import an Excel file

GodLovesYou

Android Enthusiast
What's up!

I am developing a project base on importing/uploading a .csv file from storage and add on a display. I would like to know if it is possible to make a "Search by" option.

For example I have a column named "Products" and my goal is search all the products with started with Samsung, you know what I mean?
 
D

Deleted User

Guest
Are the contents of your .csv file in a certain format? If so you could create a regular expression (regex) pattern to match each line as you read it in from the file.
 

GodLovesYou

Android Enthusiast
Thread starter
Are the contents of your .csv file in a certain format? If so you could create a regular expression (regex) pattern to match each line as you read it in from the file.

No they aren't. I got three columns and I want to search by one of them.
 
D

Deleted User

Guest
So if you post your code up which does the reading of the file, we can advise on how to modify that to implement a search.
 

GodLovesYou

Android Enthusiast
Thread starter
So if you post your code up which does the reading of the file, we can advise on how to modify that to implement a search.

Java:
import android.app.Dialog;
import android.app.ListActivity;
import android.content.ActivityNotFoundException;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends ListActivity {
    TextView lbl;
    DBController controller = new DBController(this);
    Button btnimport;
    ListView lv;
    final Context context = this;
    ListAdapter adapter;
    ArrayList<HashMap<String, String>> myList;
    public static final int requestcode = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lbl = (TextView) findViewById(R.id.txtresulttext);
        btnimport = (Button) findViewById(R.id.btnupload);
        lv = getListView();
        btnimport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent fileintent = new Intent(Intent.ACTION_GET_CONTENT);
                fileintent.setType("gagt/sdf");
                try {
                    startActivityForResult(fileintent, requestcode);
                } catch (ActivityNotFoundException e) {
                    lbl.setText("No activity can handle picking a file. Showing alternatives.");
                }
            }
        });
        myList= controller.getAllProducts();
        if (myList.size() != 0) {
            ListView lv = getListView();
            ListAdapter adapter = new SimpleAdapter(MainActivity.this, myList,
                    R.layout.v, new String[]{"Company", "Name", "Price"}, new int[]{
                    R.id.txtproductcompany, R.id.txtproductname, R.id.txtproductprice});
            setListAdapter(adapter);
            lbl.setText("");
        }
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data == null)
            return;
        switch (requestCode) {
            case requestcode:
                String filepath = data.getData().getPath();
                controller = new DBController(getApplicationContext());
                SQLiteDatabase db = controller.getWritableDatabase();
                String tableName = "proinfo";
                db.execSQL("delete from " + tableName);
                try {
                    if (resultCode == RESULT_OK) {
                        try {
                            FileReader file = new FileReader(filepath);
                            BufferedReader buffer = new BufferedReader(file);
                            ContentValues contentValues = new ContentValues();
                            String line = "";
                            db.beginTransaction();
                            while ((line = buffer.readLine()) != null) {
                                String[] str = line.split(",", 3);  // defining 3 columns with null or blank field //values acceptance
                                //Id, Company,Name,Price
                                String company = str[0].toString();
                                String Name = str[1].toString();
                                String Price = str[2].toString();
                                contentValues.put("Company", company);
                                contentValues.put("Name", Name);
                                contentValues.put("Price", Price);
                                db.insert(tableName, null, contentValues);
                                lbl.setText("Successfully Updated Database.");
                            }
                            db.setTransactionSuccessful();
                            db.endTransaction();
                        } catch (IOException e) {
                            if (db.inTransaction())
                                db.endTransaction();
                            Dialog d = new Dialog(this);
                            d.setTitle(e.getMessage().toString() + "first");
                            d.show();
                            // db.endTransaction();
                        }
                    } else {
                        if (db.inTransaction())
                            db.endTransaction();
                        Dialog d = new Dialog(this);
                        d.setTitle("Only CSV files allowed");
                        d.show();
                    }
                } catch (Exception ex) {
                    if (db.inTransaction())
                        db.endTransaction();
                    Dialog d = new Dialog(this);
                    d.setTitle(ex.getMessage().toString() + "second");
                    d.show();
                    // db.endTransaction();
                }
        }
        myList= controller.getAllProducts();
        if (myList.size() != 0) {
            ListView lv = getListView();
            ListAdapter adapter = new SimpleAdapter(MainActivity.this, myList,
                    R.layout.v, new String[]{"Company", "Name", "Price"}, new int[]{
                    R.id.txtproductcompany, R.id.txtproductname, R.id.txtproductprice});
            setListAdapter(adapter);
            lbl.setText("Data Imported");
        }
    }
}
 
D

Deleted User

Guest
Yes, you could create a new activity for your searching. As you say, all that's needed in the xml layout is a text box for the user to enter the product name, and a button to trigger the search.
 
D

Deleted User

Guest
Well as you already loaded all the product data into your list adapter, then using filtering on the list is more efficient than doing another database query.
But you can do it either way really. Using a DB query is probably least disruptive to your existing code. I just thought that filtered list was pretty neat.
 

GodLovesYou

Android Enthusiast
Thread starter
Well as you already loaded all the product data into your list adapter, then using filtering on the list is more efficient than doing another database query.
But you can do it either way really. Using a DB query is probably least disruptive to your existing code. I just thought that filtered list was pretty neat.

I am running some tests and when I get something I will report in this post.

Btw thank you for your help!
 
D

Deleted User

Guest
Sure, but can you post the Java code for your search activity, and all the associated layout xml?
 

GodLovesYou

Android Enthusiast
Thread starter
Yes, sure.

This is my search code that I have at the moment
Java:
final EditText etSearch = (EditText)findViewById(R.id.etSearch);

        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String company = etSearch.getText().toString();
                controller = new DBController(getApplicationContext());
                SQLiteDatabase db = controller.getWritableDatabase();
                String tableName = "proinfo";
                db.execSQL("SELECT Company FROM " + tableName + " WHERE <Company> like " + etSearch.getText() + "%");
            }
        });

And my layout code
Java:
<?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:orientation="vertical"
    android:weightSum="9"
    android:background="#FFC7C7C7">


    <LinearLayout
        android:id="@+id/lvcontainer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:layout_alignParentTop="true"
        android:orientation="horizontal"
        android:padding="1dp"
        android:background="#FFC7C7C7"
        android:weightSum="3">

        <TextView
            android:id="@+id/txtproductcompany"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left"
            android:text="COMPANY"
            android:textColor="#000000"
            android:textSize="13sp" />
        <TextView
            android:id="@+id/txtproductname"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left"
            android:padding="3dp"
            android:text="PRODUCT"
            android:textColor="#000000"
            android:textSize="13sp" />
        <TextView
            android:id="@+id/txtproductprice"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left"
            android:text="PRICE"
            android:padding="3dp"
            android:textColor="#000000"
            android:textSize="13sp" />
    </LinearLayout>

    <EditText
        android:id="@+id/etSearch"
        android:orientation="horizontal"
        android:weightSum="3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </EditText>

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@+id/lvcontainer"
        android:layout_weight="7">
    </ListView>
    <TextView
        android:id="@+id/txtresulttext"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginLeft="5dp"
        android:layout_below="@android:id/list"
        android:layout_marginTop="2dp"
        android:layout_weight="0.5"
        android:gravity="left"
        android:text=""
        android:textColor="#FFF55F54"
        android:textSize="20sp"
        android:textStyle="italic|bold">
    </TextView>
    <LinearLayout
        android:id="@+id/lvbottom"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:weightSum="1">
        <Button
            android:id="@+id/btnupload"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="UPLOAD"
            android:textColor="#ffffff"
            android:background="#1083f5"
            android:textSize="15sp"
            android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>

If you could help me I would be appreciated

Have a good day
 
D

Deleted User

Guest
Ok let me rewind this, because I'm still not understanding what the problem is.
Can you explain, in clear terms, what is the problem? Is it a layout issue? Is it a problem with the search?
 

GodLovesYou

Android Enthusiast
Thread starter
Ok let me rewind this, because I'm still not understanding what the problem is.
Can you explain, in clear terms, what is the problem? Is it a layout issue? Is it a problem with the search?

Ok...What this application does is import a cvs file from external storage and then the information from that file will show up on layout screen. After that I would like to be able to search by "Company" and shows me up everything that I write on EditText(Search). For example I write "Samsung" and that shows me all the Samsung models.
 
D

Deleted User

Guest
Looks ok, but your code doesn't seem to be doing anything with the query results.
 
Top