想知道是否有人可以帮我弄清楚如何将EditText框的内容添加到Android上的ListView列表中。我正在处理一个项目,使用条形码扫描器扫描条形码,并将结果返回到EditText框。
我现在正尝试使用一个按钮来编码EditText框的内容,以便在该活动内或另一个活动中添加列表中的内容。我已经看过简单的注释列表示例和其他一些示例,但是,当我尝试实现一些相同的概念时,我没有得到任何地方或者我认为我得到了某个地方,但代码什么也没做。对不起,已经很晚了......整晚都想弄清楚... 任何帮助,建议,都会受到极大的赞赏......
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();
}
}
}
}
答案 0 :(得分:1)
在按钮单击事件中,将editText的文本添加到用于填充ListView的列表中,并调用ArrayAdapter的notifyDataSetChanged()。希望这会奏效。
noteList.add(mBarcodeEdit.getText());
aa.notifyDataSetChanged();
答案 1 :(得分:1)
我已经稍微更改了代码,并且根据您的要求工作,请查看相同的
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();
}
}
}
}