1. Are you ready for the Galaxy S20? Here is everything we know so far!

"Search by" after import an Excel file

Discussion in 'Android Development' started by GodLovesYou, Feb 22, 2016.

  1. GodLovesYou

    GodLovesYou Android Enthusiast
    Thread Starter

    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?
     


  2. Deleted User

    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.
     
  3. GodLovesYou

    GodLovesYou Android Enthusiast
    Thread Starter

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

    GodLovesYou Android Enthusiast
    Thread Starter

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

    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.
     
  6. GodLovesYou

    GodLovesYou Android Enthusiast
    Thread Starter

    Code (Java):
    1. import android.app.Dialog;
    2. import android.app.ListActivity;
    3. import android.content.ActivityNotFoundException;
    4. import android.content.ContentValues;
    5. import android.content.Context;
    6. import android.content.Intent;
    7. import android.database.sqlite.SQLiteDatabase;
    8. import android.os.Bundle;
    9. import android.view.View;
    10. import android.widget.Button;
    11. import android.widget.ListAdapter;
    12. import android.widget.ListView;
    13. import android.widget.SimpleAdapter;
    14. import android.widget.TextView;
    15. import java.io.BufferedReader;
    16. import java.io.FileReader;
    17. import java.io.IOException;
    18. import java.util.ArrayList;
    19. import java.util.HashMap;
    20. public class MainActivity extends ListActivity {
    21.     TextView lbl;
    22.     DBController controller = new DBController(this);
    23.     Button btnimport;
    24.     ListView lv;
    25.     final Context context = this;
    26.     ListAdapter adapter;
    27.     ArrayList<HashMap<String, String>> myList;
    28.     public static final int requestcode = 1;
    29.     @Override
    30.     protected void onCreate(Bundle savedInstanceState) {
    31.         super.onCreate(savedInstanceState);
    32.         setContentView(R.layout.activity_main);
    33.         lbl = (TextView) findViewById(R.id.txtresulttext);
    34.         btnimport = (Button) findViewById(R.id.btnupload);
    35.         lv = getListView();
    36.         btnimport.setOnClickListener(new View.OnClickListener() {
    37.             @Override
    38.             public void onClick(View v) {
    39.                 Intent fileintent = new Intent(Intent.ACTION_GET_CONTENT);
    40.                 fileintent.setType("gagt/sdf");
    41.                 try {
    42.                     startActivityForResult(fileintent, requestcode);
    43.                 } catch (ActivityNotFoundException e) {
    44.                     lbl.setText("No activity can handle picking a file. Showing alternatives.");
    45.                 }
    46.             }
    47.         });
    48.         myList= controller.getAllProducts();
    49.         if (myList.size() != 0) {
    50.             ListView lv = getListView();
    51.             ListAdapter adapter = new SimpleAdapter(MainActivity.this, myList,
    52.                     R.layout.v, new String[]{"Company", "Name", "Price"}, new int[]{
    53.                     R.id.txtproductcompany, R.id.txtproductname, R.id.txtproductprice});
    54.             setListAdapter(adapter);
    55.             lbl.setText("");
    56.         }
    57.     }
    58.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    59.         if (data == null)
    60.             return;
    61.         switch (requestCode) {
    62.             case requestcode:
    63.                 String filepath = data.getData().getPath();
    64.                 controller = new DBController(getApplicationContext());
    65.                 SQLiteDatabase db = controller.getWritableDatabase();
    66.                 String tableName = "proinfo";
    67.                 db.execSQL("delete from " + tableName);
    68.                 try {
    69.                     if (resultCode == RESULT_OK) {
    70.                         try {
    71.                             FileReader file = new FileReader(filepath);
    72.                             BufferedReader buffer = new BufferedReader(file);
    73.                             ContentValues contentValues = new ContentValues();
    74.                             String line = "";
    75.                             db.beginTransaction();
    76.                             while ((line = buffer.readLine()) != null) {
    77.                                 String[] str = line.split(",", 3);  // defining 3 columns with null or blank field //values acceptance
    78.                                 //Id, Company,Name,Price
    79.                                 String company = str[0].toString();
    80.                                 String Name = str[1].toString();
    81.                                 String Price = str[2].toString();
    82.                                 contentValues.put("Company", company);
    83.                                 contentValues.put("Name", Name);
    84.                                 contentValues.put("Price", Price);
    85.                                 db.insert(tableName, null, contentValues);
    86.                                 lbl.setText("Successfully Updated Database.");
    87.                             }
    88.                             db.setTransactionSuccessful();
    89.                             db.endTransaction();
    90.                         } catch (IOException e) {
    91.                             if (db.inTransaction())
    92.                                 db.endTransaction();
    93.                             Dialog d = new Dialog(this);
    94.                             d.setTitle(e.getMessage().toString() + "first");
    95.                             d.show();
    96.                             // db.endTransaction();
    97.                         }
    98.                     } else {
    99.                         if (db.inTransaction())
    100.                             db.endTransaction();
    101.                         Dialog d = new Dialog(this);
    102.                         d.setTitle("Only CSV files allowed");
    103.                         d.show();
    104.                     }
    105.                 } catch (Exception ex) {
    106.                     if (db.inTransaction())
    107.                         db.endTransaction();
    108.                     Dialog d = new Dialog(this);
    109.                     d.setTitle(ex.getMessage().toString() + "second");
    110.                     d.show();
    111.                     // db.endTransaction();
    112.                 }
    113.         }
    114.         myList= controller.getAllProducts();
    115.         if (myList.size() != 0) {
    116.             ListView lv = getListView();
    117.             ListAdapter adapter = new SimpleAdapter(MainActivity.this, myList,
    118.                     R.layout.v, new String[]{"Company", "Name", "Price"}, new int[]{
    119.                     R.id.txtproductcompany, R.id.txtproductname, R.id.txtproductprice});
    120.             setListAdapter(adapter);
    121.             lbl.setText("Data Imported");
    122.         }
    123.     }
    124. }
     
  7. Deleted User

    Deleted User Guest

  8. GodLovesYou

    GodLovesYou Android Enthusiast
    Thread Starter

  9. Deleted User

    Deleted User Guest

    Yes that would do the job.
     
  10. GodLovesYou

    GodLovesYou Android Enthusiast
    Thread Starter

    In xml layout I need to create a Search TextView and Button?
     
  11. Deleted User

    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.
     
  12. Deleted User

    Deleted User Guest

  13. GodLovesYou

    GodLovesYou Android Enthusiast
    Thread Starter

  14. Deleted User

    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.
     
  15. GodLovesYou

    GodLovesYou Android Enthusiast
    Thread Starter

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

    Btw thank you for your help!
     
  16. GodLovesYou

    GodLovesYou Android Enthusiast
    Thread Starter

    Man are you there to help me out abou my search my problem.
    I am trying from yesterday and I could't find a solution for that.
     
  17. Deleted User

    Deleted User Guest

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

    GodLovesYou Android Enthusiast
    Thread Starter

    Yes, sure.

    This is my search code that I have at the moment
    Code (Java):
    1. final EditText etSearch = (EditText)findViewById(R.id.etSearch);
    2.  
    3.         etSearch.addTextChangedListener(new TextWatcher() {
    4.             @Override
    5.             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    6.  
    7.             }
    8.  
    9.             @Override
    10.             public void onTextChanged(CharSequence s, int start, int before, int count) {
    11.  
    12.             }
    13.  
    14.             @Override
    15.             public void afterTextChanged(Editable s) {
    16.                 String company = etSearch.getText().toString();
    17.                 controller = new DBController(getApplicationContext());
    18.                 SQLiteDatabase db = controller.getWritableDatabase();
    19.                 String tableName = "proinfo";
    20.                 db.execSQL("SELECT Company FROM " + tableName + " WHERE <Company> like " + etSearch.getText() + "%");
    21.             }
    22.         });
    And my layout code
    Code (Java):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:layout_width="match_parent"
    4.     android:layout_height="match_parent"
    5.     android:orientation="vertical"
    6.     android:weightSum="9"
    7.     android:background="#FFC7C7C7">
    8.  
    9.  
    10.     <LinearLayout
    11.         android:id="@+id/lvcontainer"
    12.         android:layout_width="match_parent"
    13.         android:layout_height="0dp"
    14.         android:layout_weight="0.5"
    15.         android:layout_alignParentTop="true"
    16.         android:orientation="horizontal"
    17.         android:padding="1dp"
    18.         android:background="#FFC7C7C7"
    19.         android:weightSum="3">
    20.  
    21.         <TextView
    22.             android:id="@+id/txtproductcompany"
    23.             android:layout_width="0dp"
    24.             android:layout_height="wrap_content"
    25.             android:layout_weight="1"
    26.             android:gravity="left"
    27.             android:text="COMPANY"
    28.             android:textColor="#000000"
    29.             android:textSize="13sp" />
    30.         <TextView
    31.             android:id="@+id/txtproductname"
    32.             android:layout_width="0dp"
    33.             android:layout_height="wrap_content"
    34.             android:layout_weight="1"
    35.             android:gravity="left"
    36.             android:padding="3dp"
    37.             android:text="PRODUCT"
    38.             android:textColor="#000000"
    39.             android:textSize="13sp" />
    40.         <TextView
    41.             android:id="@+id/txtproductprice"
    42.             android:layout_width="0dp"
    43.             android:layout_height="wrap_content"
    44.             android:layout_weight="1"
    45.             android:gravity="left"
    46.             android:text="PRICE"
    47.             android:padding="3dp"
    48.             android:textColor="#000000"
    49.             android:textSize="13sp" />
    50.     </LinearLayout>
    51.  
    52.     <EditText
    53.         android:id="@+id/etSearch"
    54.         android:orientation="horizontal"
    55.         android:weightSum="3"
    56.         android:layout_width="match_parent"
    57.         android:layout_height="wrap_content">
    58.     </EditText>
    59.  
    60.     <ListView
    61.         android:id="@android:id/list"
    62.         android:layout_width="match_parent"
    63.         android:layout_height="0dp"
    64.         android:layout_below="@+id/lvcontainer"
    65.         android:layout_weight="7">
    66.     </ListView>
    67.     <TextView
    68.         android:id="@+id/txtresulttext"
    69.         android:layout_width="match_parent"
    70.         android:layout_height="0dp"
    71.         android:layout_marginLeft="5dp"
    72.         android:layout_below="@android:id/list"
    73.         android:layout_marginTop="2dp"
    74.         android:layout_weight="0.5"
    75.         android:gravity="left"
    76.         android:text=""
    77.         android:textColor="#FFF55F54"
    78.         android:textSize="20sp"
    79.         android:textStyle="italic|bold">
    80.     </TextView>
    81.     <LinearLayout
    82.         android:id="@+id/lvbottom"
    83.         android:layout_width="match_parent"
    84.         android:layout_height="0dp"
    85.         android:layout_alignParentBottom="true"
    86.         android:orientation="horizontal"
    87.         android:layout_weight="1"
    88.         android:weightSum="1">
    89.         <Button
    90.             android:id="@+id/btnupload"
    91.             android:layout_width="0dp"
    92.             android:layout_height="match_parent"
    93.             android:layout_weight="1"
    94.             android:gravity="center"
    95.             android:text="UPLOAD"
    96.             android:textColor="#ffffff"
    97.             android:background="#1083f5"
    98.             android:textSize="15sp"
    99.             android:textStyle="bold" />
    100.     </LinearLayout>
    101. </LinearLayout>
    If you could help me I would be appreciated

    Have a good day
     
  19. Deleted User

    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?
     
  20. GodLovesYou

    GodLovesYou Android Enthusiast
    Thread Starter

    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.
     
  21. Deleted User

    Deleted User Guest

    I know what you want to do. I asked you to explain what the problem is.
     
  22. GodLovesYou

    GodLovesYou Android Enthusiast
    Thread Starter

    I have two public classes, one of them is my DatabaseController and the other one is my MainActivity. I don't know if my Select query is OK.
     
  23. Deleted User

    Deleted User Guest

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

    GodLovesYou Android Enthusiast
    Thread Starter

    No. I think my code can't find Company column
     
Loading...
Similar Threads - Search import Excel
  1. LB123
    Replies:
    5
    Views:
    209
  2. Bahria Town Rawalpindi
    Replies:
    13
    Views:
    319
  3. Daniel03929911
    Replies:
    3
    Views:
    300
  4. JamieKitson
    Replies:
    1
    Views:
    278
  5. App Update
    Replies:
    0
    Views:
    117
  6. iTP
    Replies:
    1
    Views:
    164
  7. Tharpebb63
    Replies:
    1
    Views:
    351
  8. Gloria J Houston
    Replies:
    1
    Views:
    165
  9. LUIZ SIQUEIRA NETO
    Replies:
    0
    Views:
    221
  10. guriausa
    Replies:
    4
    Views:
    502

Share This Page

Loading...