Android 在写入.csv之前,请检查重复的编辑文本
我的第一个CSV活动中有几个编辑文本:Android 在写入.csv之前,请检查重复的编辑文本,android,database,csv,Android,Database,Csv,我的第一个CSV活动中有几个编辑文本: Button save; CSV csv; StringBuffer filePathc; public final static String EXTRA_MESSAGE = "com.example.mapcard.MESSAGE"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setConten
Button save;
CSV csv;
StringBuffer filePathc;
public final static String EXTRA_MESSAGE = "com.example.mapcard.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_card);
txtName= (EditText) findViewById(R.id.txtName);
txtCompany= (EditText) findViewById(R.id.txtCompany);
txtPhone= (EditText) findViewById(R.id.txtPhone);
txtMobile = (EditText) findViewById(R.id.txtMobile);
txtAddress = (EditText) findViewById(R.id.txtAddress);
txtEmail = (EditText) findViewById(R.id.txtEmail);
txtWebsite = (EditText) findViewById(R.id.txtWebsite);
txtTitle = (EditText) findViewById(R.id.txtTitle);
filePathc = new StringBuffer();
filePathc.append("/sdcard/Android/data/");
file = new File(filePathc+filename.toString());
csv = new CSV(file);
save=(Button)findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
csv.writeHeader(txtName.getText().toString()+ ","
+txtCompany.getText().toString()
+ "," +txtPhone.getText().toString()
+ "," +txtMobile.getText().toString()
+ "," +txtAddress.getText().toString()
+ "," +txtEmail.getText().toString()
+ "," +txtWebsite.getText().toString()
+ "," +txtTitle.getText().toString());
Intent intent = new Intent(NewCard.this, Template.class);
intent.putExtra("name", Name);
intent.putExtra("company", Company);
intent.putExtra("phone", Phone);
intent.putExtra("mobile", Mobile);
intent.putExtra("address", Address);
intent.putExtra("email", Email);
intent.putExtra("website", Website);
intent.putExtra("title", Title);
startActivity(intent);
第二个活动是将此数据写入.csv文件:
public class CSV {
private PrintWriter csvWriter;
private File file;
public CSV(File file) {
this.file = file;
}
public void writeHeader(String data) {
try {
if (data != null) {
csvWriter = new PrintWriter(new FileWriter(file, true));
csvWriter.print("\r\n"+",");
csvWriter.print(data);
csvWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}}
已成功创建目录中的文件并插入数据。我正在尝试检查姓名或电子邮件等数据,如果重复/已记录,则不保存到.csv中,并用Toast“相同数据”警告用户
我认为这与线路有关:
try {
if (data != null) {
感谢您的帮助和建议。谢谢。代码>数据!=null不包括字符串为空但不为null的情况。因此,基本上,您可以通过调用
getText()
从文本视图中获得“
。添加&&!“”.equals(数据)
应该可以解决这个问题
更新
空字符串和非斜体字符串之间有区别。非斜体字符串表示您将其声明为字符串a代码>但没有其他内容
如果使用editText.getText(),editText将返回一个已初始化但仅包含当前内容的字符串。因此,基本上,如果editext中没有任何内容,它将返回“”
总而言之:
String a;
null == "" // false
a == null // true
"".equals(editText.getTExt()) // true if the editext doesnt contain anything
使用以下函数
public boolean exist(String newData){
try {
FileInputStream fIn = new FileInputStream(file);
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = "";
while ((aDataRow = myReader.readLine()) != null) {
if(aDataRow.contains(newData))//<--check if data exists
return true;
}
txtData.setText(aBuffer);
myReader.close();
} catch (Exception e) {
}
return false;
}
thx@vipulmittal写入头已经完成,但第一部分在我的代码中有一些冲突。如何更好地嵌套它?第一部分的问题是什么?存在非法的变量修饰符;仅允许最终版本。您在使用exist方法时,它应该是CSV类中嵌套的CSV类。错误:未为CSV类型定义findViewById(int)方法。over:EditText txtName=(EditText)findviewbyd(R.id.txtName)@Umitk添加了一些示例,如果希望您了解我的意思,但这将检查手头的数据。我想从前面插入的名称中签入csv。比如说,数据上可能有20个,并且没有一个应该被复制。因此,检查每一行中与数据相关联的键,在您的情况下,我想是名称
public void writeHeader(String data) {
try {
if (data != null&&!data.isEmpty()&&!exist(data)) {
csvWriter = new PrintWriter(new FileWriter(file, true));
csvWriter.print("\r\n"+",");
csvWriter.print(data);
csvWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}