0

i'm very new to posting questions on this site as well as programming. Forgive me if i miss out on something or a wrong format and such. Putting my hands on android for a project, a restaurant order system. Using android eclipse to do it. I have been successful in making an app that scans QR and displays the results.

When you press scan, it opens the camera and scans a QR and displays the results under "Orders". What i haven't been able to figure out is how can i make it so that everytime i scan, it just adds a new result under the orders? Right now, everytime i scan, it replaces the current result with the new one. I want it to keep adding the results into the order.

This the current coding i have for the Main Activity

public class MainActivity extends Activity {
TextView tvResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tvResult = (TextView) findViewById(R.id.tvResult);

    Button scanBtn = (Button) findViewById(R.id.btnScan);

    add:
    scanBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            try {

                Intent intent = new Intent(
                        "com.google.zxing.client.android.SCAN");
                intent.putExtra("SCAN_MODE", "QR_CODE_MODE,PRODUCT_MODE");
                startActivityForResult(intent, 0);

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "ERROR:" + e, 1).show();

            }

        }
    });

}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {

        if (resultCode == RESULT_OK) {
            tvResult.setText(intent.getStringExtra("SCAN_RESULT"));
        } else if (resultCode == RESULT_CANCELED) {
            tvResult.setText("Scan cancelled.");
        }
    }
}

A second question, after displaying the results which after scanning will display "51 Cheese Salami $6.90" for one result as example. The solution to the first question would allow it to be displayed as such

51 Cheese Salami $6.90
52 Charcoal Onion Beef $7.50
53 Salami Panini $6.30

and so on;

I have to send the results to a web service. What would be the best course of action? How would i be able to separate the results into specifics like ID, Name, Price. Parsing it? Adding it into a database first? Is it possible to not involve the use of database? Please correct my question if it doesn't make sense.

  • Every time your app scans a QR code, it overwrites your `tvResult` variable (TextView). If you want to keep all your results, then you need to create a new TextView element every time you scan, instead of overwriting them. [Check out this.](http://stackoverflow.com/questions/4394293/create-a-new-textview-programmatically-then-display-it-below-another-textview) – ctzdev Apr 03 '14 at 07:00
  • you can even use sqlite to store values after each scan. – Nandakishore Shetty Apr 03 '14 at 07:09
  • @NotoriousArab i'll give it a go. Seems perfect for fixing the first question. – user2295154 Apr 03 '14 at 09:22
  • @NandakishoreShetty but i'll still be displaying them, right? Only difference is that the values are stored in a database after each scan. Do you know a good link or anything to learn how to use sqlite in android? – user2295154 Apr 03 '14 at 09:23
  • look at this link http://www.vogella.com/tutorials/AndroidSQLite/article.html – Nandakishore Shetty Apr 03 '14 at 09:57
  • Do you need to save the results after you exit the application? – gkiko Apr 03 '14 at 10:25
  • @gkiko no doesn't need to be saved. As long as the data is sent to the server side properly. – user2295154 Apr 03 '14 at 11:13

1 Answers1

0

I suggest using ListView with custom adapter to display the results of the scan. See example here and here

ActivityMain

public class MainActivity extends Activity {
private Adapter a;
private ArrayList<Car> list;
private TabHost tabhost;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    addListView();
}

private void addListView() {
    list = new ArrayList<Car>();
    // fill list view with Car objects      
    a = new Adapter(list, this);
    final ListView listView = (ListView) findViewById(R.id.list);

    listView.setAdapter(a);
}

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {

        if (resultCode == RESULT_OK) {
            list.add(new Car(/*fill with proper data*/));
            adapter.notifyDataSetChanged(); // to update the listview
        } else if (resultCode == RESULT_CANCELED) {
            // handle somehow 
        }
    }
}

CustomAdapter

private class Adapter extends BaseAdapter { private ArrayList ls; private Context c;

    public Adapter(ArrayList<Car> ls, Context c) {
        this.ls = ls;
        this.c = c;
    }

    @Override
    public int getCount() {
        return ls.size();
    }

    @Override
    public Object getItem(int position) {
        return ls.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = null;
        Container cont;
        if (convertView == null) {
            v = View.inflate(c, R.layout.component, null);
            cont = new Container();
            cont.txtName = (TextView) v.findViewById(R.id.view_name);
                            cont.txtPrice = (TextView) v.findViewById(R.id.view_price);
            cont.txtId = (ImageView) v.findViewById(R.id.view_id);
            v.setTag(cont);
        } else {
            v = convertView;
            cont = (Container) v.getTag();
        }

        (cont.txtName).setText(ls.get(position).name);
                    (cont.txtPrice).setText(ls.get(position).price);
                    (cont.txtId).setText(ls.get(position).id);

        return v;
    }

}

private class Container {
    TextView txtName, txtPrice, txtId;
}

private class Car {
    String name, price;
            int id;

    public Car(String name, String price, int id) {
        this.name = name;
                    this.price = price;
        this.id = id;
    }
}
Community
  • 1
  • 1
gkiko
  • 2,275
  • 3
  • 27
  • 47
  • It's my old code. I refactored some lines and haven't tested it. – gkiko Apr 03 '14 at 11:28
  • i tried it out but i am very new to android as i've starting learning for only a few weeks now, so i'm still very lost. Nevertheless, can i just clarify a few things: this is all under MainActivity, right? Also the part where fill proper data, do you mean the results i get after scanning? – user2295154 Apr 05 '14 at 03:02
  • I also seem to be getting this in the console: R.java was modified manually! Reverting to generated version – user2295154 Apr 05 '14 at 03:12
  • I added the new link to make it clear how `BaseAdapter` works. Initializing the `ListView` and setting the adapter is done under `MainActivity` in `onCreate` method. Yes, filling with proper data means adding the result of the scan to the `ArrayList`. – gkiko Apr 05 '14 at 15:23
  • **R.java was modified** is warning when you change something in the `R.java` file by opening it and editing by hand. You should not do this, as it's generated automatically and hold references on various resources used by your Activities. Don't worry, this kind of warning won't break your app. – gkiko Apr 05 '14 at 15:26
  • Look at `ListView` as it's graphical interface that looks at list of object wrapped in adapter class, which is set by calling `listView.setAdapter(a)`. It draws everything the list contains on the screen with the help of adapter. So after modifying the list you should tell the adapter to redraw the it on the screen. – gkiko Apr 05 '14 at 15:31