Zoom1992
Zoom1992

Reputation: 73

Passing two Variables for a ListView Android

I'm developing a Item QR Scanner application using Zxing Scanner Library and i want to enter scan data into a listview. So i want to enter ItemName and ItemPrice into ListView after scanning the item. To differentiate itemName and itemPrice from scanner result(plain text) i used string tokenizer class then i can get itemName and Price seperately.

The solution should be while a user scanned an item it should display item Name and item Price separately in the list View. The problem is how to use a String and Integer can use to implement this system.

StringTokenizer tokenizer = new StringTokenizer(scanRes, ":");
   itmName = tokenizer.nextToken();
   itmPrice = Integer.valueOf(tokenizer.nextToken());

And i'm passing the result to a String name "scanRes" (mentioned above code) through this statement.

qrResult.setText(intent.getStringExtra("SCAN_RESULT"));
String scanRes = intent.getStringExtra("SCAN_RESULT");

i want to view the assigned values in two variables mentioned above(itmName and itmPrice) into a ListView. I have implemented a solution bt it needs String array to pass values. Bt i need to pass the values saved in this two Variables. I'll post below the solution i have created. Thanks a lot if anyone can tell what are the things i have to change in the Code.

This is the code for Scanner Activity Class. And its giving an error for the "onItemClick" method at the bottom.

public class QRScanMenu extends Activity {
String itmName;
Integer itmPrice;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.qrscannermenu);

    HandleClick handleClick = new HandleClick();
    findViewById(R.id.QRButton).setOnClickListener(handleClick);
    findViewById(R.id.ExitButton).setOnClickListener(handleClick);



}

private class HandleClick implements View.OnClickListener{
    public void onClick(View arg0){
        try{
            Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            switch (arg0.getId()){
                case R.id.QRButton:
                    intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
                    break;

                case R.id.ExitButton:
                    Intent i = new Intent(QRScanMenu.this, MainActivity.class);
                    startActivity(i);
                    break;
            }
            startActivityForResult(intent, 0);
        }
        catch (ActivityNotFoundException ex){
            showDialog(QRScanMenu.this, "No Scanner Application Found", "Download Scanner Application?", "Yes", "No").show();
        }
    }
}

private static AlertDialog showDialog(final Activity activity, CharSequence title, CharSequence message, CharSequence btnYes, CharSequence btnNo){
    AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity);
    downloadDialog.setTitle(title);
    downloadDialog.setTitle(message);
    downloadDialog.setPositiveButton(btnYes, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Uri uri = Uri.parse("market://search?q=pname:" + "com.google.zxing.client.android");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            try {
                activity.startActivity(intent);
            } catch (ActivityNotFoundException ex) {

            }
        }
    });
    downloadDialog.setNegativeButton(btnNo, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    return downloadDialog.show();
}

public void onActivityResult(int reqCode, int resCode, Intent intent){
    if (reqCode == 0){
        TextView qrStatus = (TextView) findViewById(R.id.statusText);
        TextView qrResult = (TextView) findViewById(R.id.resultText);
        ListView listView = (ListView) findViewById(R.id.qrItmList);

        if(resCode == RESULT_OK){
            qrStatus.setText(intent.getStringExtra("SCAN_RESULT_FORMAT"));
            qrResult.setText(intent.getStringExtra("SCAN_RESULT"));
            String scanRes = intent.getStringExtra("SCAN_RESULT");
            StringTokenizer tokenizer = new StringTokenizer(scanRes, ":");
            itmName = tokenizer.nextToken();
            final String[] newd = new String[1];
            newd[1]=itmName;
            itmPrice = Integer.valueOf(tokenizer.nextToken());
            Integer[] newI = new Integer[1];
            newI[1]=itmPrice;

            customList adapter = new customList(QRScanMenu.this, newd, newI);
            listView.setAdapter(adapter);
            listView.setOnClickListener(new AdapterView.OnItemClickListener(){
                public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                    Toast.makeText(QRScanMenu.this, "You Clicked at "+newd, Toast.LENGTH_SHORT).show();
                }
            });



            //adapter.add(itmName);
            //adapter.add(itmPrice);
        }
        else if (resCode == RESULT_CANCELED){
            qrStatus.setText("Press QR Scan button to Scan Item");
            qrResult.setText("Scan Cancelled");
        }
    }
}
}

I've used this String Arrays for Testing purposes. Not sure will they work or not.

final String[] newd = new String[1];
newd[1]=itmName;
itmPrice = Integer.valueOf(tokenizer.nextToken());
Integer[] newI = new Integer[1];
newI[1]=itmPrice;

And this Code for the List

public class customList extends ArrayAdapter<String> {
private final Activity context;
private final String[]itmName;
private final Integer[]itmPrice;
public customList(Activity context, String[]itmName, Integer[]itmPrice){
    super(context, R.layout.columnview, itmName);
    this.context = context;
    this.itmName = itmName;
    this.itmPrice = itmPrice;
}

@Override
public View getView(int position, View view, ViewGroup parent){
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.columnview, null, true);
    TextView txtViewName = (TextView) rowView.findViewById(R.id.itmCol);
    TextView txtViewPrice = (TextView) rowView.findViewById(R.id.priceCol);
    txtViewName.setText(itmName[position]);
    txtViewPrice.setText(itmPrice[position]);
    return rowView;
}
}

This is the error i'm getting for now..

This is the XML Code for columnView.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TableRow>
<TextView
    android:id="@+id/itmCol"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:textStyle="bold" />

<TextView
    android:id="@+id/priceCol"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:textStyle="bold"/>
</TableRow>

And this is the code for QR scanner activity class

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/linLayout1">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/QRButton"
        android:text="QR Scan"
        android:textSize="18sp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ExitButton"
        android:text="Back Main"
        android:textSize="18sp"/>
</LinearLayout>


<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/statusText"
    android:layout_below="@+id/linLayout1"
    android:text="Press QR Scan Button to Scan Items"
    android:textSize="18sp"/>

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/resultText"
    android:text="Ready to Scan"
    android:textSize="18sp"
    android:layout_below="@+id/statusText"
    android:background="@android:color/white"
    android:textColor="@android:color/black"/>

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/qrItmList"
    android:layout_below="@+id/resultText"
    android:layout_centerHorizontal="true">
</ListView>

Upvotes: 0

Views: 1720

Answers (3)

Murali Kumar
Murali Kumar

Reputation: 27

final String[] newd = new String[1];
newd[1]=itmName;

Size of the String array is 1 but the arrayindex starts with 0. you are adding itmName into the 1st position.

itmPrice = Integer.valueOf(tokenizer.nextToken());
Integer[] newI = new Integer[1];
newI[1]=itmPrice;

Size of the Integer array is 1 but the arrayindex starts with 0. you are adding newI into the 1st position.

Obviously, you will get ArrayIndexOutofboundsException!

Upvotes: 0

ajinkya gaurkar
ajinkya gaurkar

Reputation: 207

i cannot see any custom xml for list item in your code. create a custom xml for list view item. and then inflate it using inflater

Upvotes: -1

RobVoisey
RobVoisey

Reputation: 1083

First you need to make a new object to hold your two items

public class Product {
    String name;
    int price;
 }

Then you make an ArrayAdapter for you items.

public class MyAdpater extends ArrayAdapter<Product> {

    private final Activity context;
    public customList(Activity context, Product[] products){
        super(context, R.layout.columnview, products);
        this.context = context;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent){
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.columnview, null, true);
        TextView txtViewName = (TextView) rowView.findViewById(R.id.itmCol);
        TextView txtViewPrice = (TextView) rowView.findViewById(R.id.priceCol);
        txtViewName.setText(getItemAtPosition(position).name);
        txtViewPrice.setText(String.valueOf(getItemAtPosition(position).price]));
        return rowView;
    }
}

Your onClickListener would then be:

listView.setOnClickListener(new AdapterView.OnItemClickListener(){
    public void onItemClick(AdapterView<Product> parent, View view, int position, long id){
            Toast.makeText(QRScanMenu.this, "You Clicked at "+ parent.getItemAtPosition(position).name, Toast.LENGTH_SHORT).show();
    }
});

Upvotes: 0

Related Questions