2

Would like to see if someone can help me on figuring out how to add the contents of a EditText box into a ListView list on Android. I have a project I'm working on that, uses Barcode Scanner to scan a barcode, and return the results into the EditText box.

I'm now attempting to code the contents of the EditText box with the use of a button to add the contents in the list either within that activity or on another one. I've looked at the simple note list example and a couple of other examples, however, when I try and implement some of the same concepts, I get no where or I think I get somewhere, but the code does nothing. I'm sorry it's late.. been up all night trying to figure this out... Any help, advice, is greatly and always appreciated...

package com.terrellmcqueen.databaseproject474;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class Main extends Activity implements OnClickListener {
    private static final int REQUEST_BARCODE = 0;
    private TextView mBarcodeEdit;
    private TextView mScanButton;

    // private fields omitted

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mBarcodeEdit = (EditText) findViewById(R.id.myEditText);
        mScanButton = (Button) findViewById(R.id.scanButton);
        mScanButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.scanButton:
                Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                intent.putExtra("SCAN_MODE", "SCAN_MODE");
                startActivityForResult(intent, REQUEST_BARCODE);
                break;
        }
     }

     public void onClick1(View v) {
         switch (v.getId()) {
             case R.id.btnSimple:    
                 ListView myListView = (ListView) findViewById(R.id.myListView);
                 final EditText myEditText = (EditText) findViewById(R.id.myEditText);        
                 final ArrayList<String> noteList = new ArrayList<String>();
                 final ArrayAdapter<String> aa;

                 // binding an array of Strings 
                 aa = new ArrayAdapter<String>(this, 
                         android.R.layout.simple_list_item_1,noteList);

                 // here we set the adapter, this turns it on
                 myListView.setAdapter(aa);

                 // here is the button
                 // Button btnSimple = (Button) findViewById(R.id.btnSimple);

                 //  String barcode = mBarcodeEdit.getText().toString();

                 //  String title = mTitleEdit.getText().toString();
                 //  String price = mPriceEdit.getText().toString();
            }
      }

    public void onActivityResult(int requestCode,int resultCode, Intent intent) {
        if (requestCode == REQUEST_BARCODE) {
            if (resultCode == RESULT_OK) {
                String barcode = intent.getStringExtra("SCAN_RESULT");
                mBarcodeEdit.setText(barcode);
            } else if (resultCode == RESULT_CANCELED) {
                finish();
            }
        }
    }
}

2 Answers 2

1

I have changed the code little bit and it's working according to your requirement please look at the same

package com.barcode;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class BarcodeActivity extends Activity implements OnClickListener {
    private static final int REQUEST_BARCODE = 0;
    private TextView mBarcodeEdit;
    private Button mScanButton;
    private Button mAddButton;

    // private fields omitted

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mBarcodeEdit = (EditText) findViewById(R.id.editText1);
        mScanButton = (Button) findViewById(R.id.button1);
        mAddButton = (Button) findViewById(R.id.add);
        mScanButton.setOnClickListener(this);
        mAddButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            intent.putExtra("SCAN_MODE", "SCAN_MODE");
            startActivityForResult(intent, REQUEST_BARCODE);
            break;
        case R.id.add:
            ListView myListView = (ListView) findViewById(R.id.listView1);
            final EditText myEditText = (EditText) findViewById(R.id.editText1);
            final ArrayList<String> noteList = new ArrayList<String>();
            noteList.add(myEditText.getText().toString());
            final ArrayAdapter<String> aa;

            // binding an array of Strings
            aa = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, noteList);

            // here we set the adapter, this turns it on
            myListView.setAdapter(aa);
            break;
        }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == REQUEST_BARCODE) {
            if (resultCode == RESULT_OK) {
                String barcode = intent.getStringExtra("SCAN_RESULT");
                mBarcodeEdit.setText(barcode);
            } else if (resultCode == RESULT_CANCELED) {
                finish();
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! worked like a charm... another quick question though, right now the code scans, returns a barcode, and I'm able to click the add button to add to list, however, if I (click AddButton) or (scan) to add another item, it just replaces the previous item that was scanned... how do I get it where the code has a list of items scanned... right now it's just one... I'm still working on it now to see what I'm not doing to prevent this from happening...again thanks...
// here we set the adapter, this turns it on myListView.setAdapter(aa); // add the note noteList.add(0,myEditText.getText().toString()); // update the view aa.notifyDataSetChanged(); //erase the text so we can add another note myEditText.setText("");
Never mind the last comment... I got it! Thanks so much for your feedback. Now I'm pushing on to the next phase within my app...
Please accept the ans. there will be tick mark beside the vote. so we can conclude the question
1

In the button click event, add the editText's text to the list used to populate the ListView and call the notifyDataSetChanged() of your ArrayAdapter. Hope that would work.

noteList.add(mBarcodeEdit.getText());
aa.notifyDataSetChanged();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.