我正在使用Zxing扫描仪库开发一个物品QR扫描仪应用程序,我想将扫描数据输入到列表视图中。所以我想在扫描项目后将ItemName和ItemPrice输入ListView。为了区分itemName和itemPrice与扫描结果(纯文本),我使用了字符串标记符类,然后我可以单独获取itemName和Price。
解决方案应该是在用户扫描项目时应该在列表视图中单独显示项目名称和项目价格。问题是如何使用String和Integer可以用来实现这个系统。
StringTokenizer tokenizer = new StringTokenizer(scanRes, ":");
itmName = tokenizer.nextToken();
itmPrice = Integer.valueOf(tokenizer.nextToken());
我将通过此语句将结果传递给字符串名称“scanRes”(上面提到的代码)。
qrResult.setText(intent.getStringExtra("SCAN_RESULT"));
String scanRes = intent.getStringExtra("SCAN_RESULT");
我想查看上面提到的两个变量(itmName和itmPrice)中的赋值给ListView。我已经实现了一个解决方案,它需要String数组来传递值。我需要传递这两个变量中保存的值。我将在我创建的解决方案下面发布。非常感谢,如果有人能说出我在代码中需要改变的内容。
这是扫描仪活动类的代码。并且它为底部的“onItemClick”方法提供了错误。
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");
}
}
}
}
我已将此String Arrays用于测试目的。不确定它们是否有效。
final String[] newd = new String[1];
newd[1]=itmName;
itmPrice = Integer.valueOf(tokenizer.nextToken());
Integer[] newI = new Integer[1];
newI[1]=itmPrice;
此列表代码
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;
}
}
这是 columnView.xml
的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>
这是 QR扫描仪活动类
的代码<?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>
答案 0 :(得分:0)
首先,您需要创建一个新对象来保存两个项目
[a-zA-Z]\d{3}
然后为你的物品制作一个ArrayAdapter。
public class Product {
String name;
int price;
}
您的onClickListener将是:
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;
}
}
答案 1 :(得分:0)
final String[] newd = new String[1];
newd[1]=itmName;
String数组的大小为1,但arrayindex以0开头。您将itmName添加到第1位。
itmPrice = Integer.valueOf(tokenizer.nextToken());
Integer[] newI = new Integer[1];
newI[1]=itmPrice;
Integer数组的大小为1,但arrayindex以0开头。您将newI添加到第1个位置。
显然,你会得到ArrayIndexOutofboundsException!
答案 2 :(得分:-1)
我在代码中看不到列表项的任何自定义xml。 为列表视图项创建自定义xml。然后使用inflater充气它