Android notifyDataSetChanged not refreshing the items after removing - android

So I am trying to remove the itempending however it does not remove it from the listview immediately on click. I have to go back and come back to this screen and it will be removed. However my other listview on the same screen was updated immediately (The submitted profile method). I tried to create a method that does the same function I need and call it in before the notifyDataSetChanged but nothing works. Any advise on why my notifyDataSetChanged is not working would be appreciated thanks.
ArrayAdapter<String> adapterSubmit, adapterPending;
ArrayList<String> itemsSubmit, itemsPending;
ListView lstSubmitPro, lstPendingPro;
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/CaptureLogs";
Button btnSubmit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_pro_list);
btnSubmit = (Button) findViewById(R.id.btnTest);
lstSubmitPro = (ListView) findViewById(R.id.lstSubmitPro);
lstPendingPro = (ListView) findViewById(R.id.lstPendingPro);
itemsSubmit = new ArrayList<String>();
adapterSubmit = new ArrayAdapter(this, R.layout.prolist, R.id.tvRows, itemsSubmit);
lstSubmitPro.setAdapter(adapterSubmit);
itemsPending = new ArrayList<String>();
adapterPending = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, itemsPending);
lstPendingPro.setAdapter(adapterPending);
lstPendingPro.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
pendingSubmitProfile();
SubmittedProfile();
btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SparseBooleanArray sp = lstPendingPro.getCheckedItemPositions();
if (sp!= null) {
for (int i = 0; i < sp.size(); i++) {
if (sp.valueAt(i) == true) {
SubmittedProfile();
adapterSubmit.notifyDataSetChanged();
itemsPending.remove(sp.get(i));
adapterPending.notifyDataSetChanged();
Toast.makeText(ProList.this, "Your profiles have been submitted successfully.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ProList.this, "Please choose a profile to be submitted.", Toast.LENGTH_LONG).show();
}
}
}
}
});
public void pendingSubmitProfile()
{
File dir = new File(path);
File[] files = dir.listFiles();
for (File f : files) {
if (f.isFile()) {
BufferedReader inputStream = null;
try {
inputStream = new BufferedReader(new FileReader(f));
String lineToRead = "--PENDING SUBMIT--";
String CurrentLine;
while ((CurrentLine = inputStream.readLine()) != null) {
if (CurrentLine.equals(lineToRead)) {
String filen = f.getName().substring(0, f.getName().lastIndexOf("."));
itemsPending.add(filen);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void SubmittedProfile()
{
File dir = new File(path);
File[] files = dir.listFiles();
for (File f : files) {
if (f.isFile()) {
BufferedReader inputStream = null;
try {
inputStream = new BufferedReader(new FileReader(f));
String lineToRead = "--SUBMITTED--";
String CurrentLine;
while ((CurrentLine = inputStream.readLine()) != null) {
if (CurrentLine.equals(lineToRead)) {
String filen = f.getName().substring(0, f.getName().lastIndexOf("."));
itemsSubmit.add(filen);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
LOG
Before, 2 items but 1 checked
05-02 08:00:02.409 4293-4293/com.example.irambi.irmobilewizard D/returned value:: 2 1 2
After, 2 items but 1 checked
05-02 08:00:03.726 4293-4293/com.example.irambi.irmobilewizard D/returned value:: 0 1 0
This is for 1 item and 1 checked. I cannot tell if this is before or after since theres only 1 printed on logcat. This will crash and error is as follows
05-02 08:03:35.345 4293-4293/com.example.irambi.irmobilewizard D/returned value:: 1 1 1
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
Before, 2 items but 2 checked
05-02 08:07:56.378 4596-4596/com.example.irambi.irmobilewizard D/returned value:: 2 2 2
After, 2 items but 2 checked - This will crash
05-02 08:07:57.671 4596-4596/com.example.irambi.irmobilewizard D/returned value:: 0 2 0
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
All the testing are done using
itemsPending.remove(sp.keyAt(i));
adapterPending.remove(adapterPending.getItem(sp.keyAt(i)));
Log based on:
Log.d("returned value:", itemsPending.size() + " " + sp.size() + " " + adapterPending.getCount());

Try this one
itemsPending.remove(sp.keyAt(i));
adapterPending.remove(adapterPending.getItem(sp.keyAt(i)));
adapterPending.notifyDataSetChanged();
EDIT:
So basically the switching of list data is working fine. What's messing the code is his file writing. I resolve it this way.
First is a create a function that would update my Pending Files to submitted.
public void submitPendingProfile(String filename){
try {
BufferedReader file = new BufferedReader(new FileReader(path + "/" + filename+".txt"));
String line;
StringBuffer inputBuffer = new StringBuffer();
while ((line = file.readLine()) != null) {
inputBuffer.append(line);
inputBuffer.append('\n');
}
String inputStr = inputBuffer.toString();
file.close();
inputStr = inputStr.replace("--PENDING SUBMIT--", "--SUBMITTED--");
FileOutputStream fileOut = new FileOutputStream(path + "/" + filename+".txt");
fileOut.write(inputStr.getBytes());
fileOut.close();
} catch (Exception e) {
System.out.println("Problem reading file.");
}
}
Then i refactor the loop for simplier process. Like removing the line SubmittedProfile(); that keeps reading all text files if conditions is true. That is a lot of process. Here's how instead.
for(int i = lstPendingPro.getAdapter().getCount() - 1 ; i >= 0; i--) {
if (sp.get(i)) {
//So when file is submitted, i update the files status using the above function.
submitPendingProfile(itemsPending.get(i));
//To avoid rereading of files, just add the item before removing it to the pending list
itemsSubmit.add(itemsPending.get(i));
adapterSubmit.notifyDataSetChanged();
itemsPending.remove(sp.keyAt(i));
adapterPending.notifyDataSetChanged();
Toast.makeText(ProList.this, "Your profiles have been submitted successfully.", Toast.LENGTH_LONG).show();
}
}

Did You tried changing your code to this order
btnSubmit.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick (View v){
SparseBooleanArray sp = lstPendingPro.getCheckedItemPositions();
if (sp != null) {
for (int i = 0; i < sp.size(); i++) {
if (sp.valueAt(i) == true) {
SubmittedProfile();
itemsPending.remove(sp.get(i));
Toast.makeText(ProList.this, "Your profiles have been submitted successfully.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ProList.this, "Please choose a profile to be submitted.", Toast.LENGTH_LONG).show();
}
}
}
adapterSubmit.notifyDataSetChanged();
adapterPending.notifyDataSetChanged();
}
});

Try this code..
itemsPending.remove(itemsPending.indexOf(sp.get(i)));
adapterPending.notifyDataSetChanged();

itemsPending.remove(sp.get(i));
adapterPending.notifyDataSetChanged();
listview.invalidate();
Try above code might be it will help you.

Related

Android make AsyncTask wait for the activity to resume inside a for loop

I have a for loop inside onActivityResult() method that creates a Runnable object and assign in to an AsyncTask. Each Runnable object is responsible of operating on a pdf file, sealing it with a password and then starting an startActivityForResult() method with an Intent to send an email.
Everything works as a charm except that my problem is that the for loop will start all the AsyncTask immediately even though the the activity is paused and the user is on the email client app. I want to make sure that the next AsyncTask doesn't execute until the user gets back to the application after pressing send email button on the email client app.
UPDATE
if (requestCode == 2) {
// Create Insurer annexe, seal document with insurer password and trigger sending email
int lastInsurerPosition = -1;
for (int i = 0; i < Constat.getInstance().getAccidentList().size(); i++) {
if (Constat.getInstance().getAccidentList().get(i).getCar().getInsurerPosition() != -1 &&
!insurersEmails[Constat.getInstance().getAccidentList().get(i).getCar().getInsurerPosition()].equals("null") &&
Constat.getInstance().getAccidentList().get(i).getSendOption() != 1 &&
Constat.getInstance().getAccidentList().get(i).getSendOption() != 2) {
lastInsurerPosition = i;
}
}
if (lastInsurerPosition != -1) {
final int lastInsurerPositionCopy = lastInsurerPosition;
for (int i = 0; i < Constat.getInstance().getAccidentList().size(); i++) {
String insurerEmail = "null";
if (Constat.getInstance().getAccidentList().get(i).getCar().getInsurerPosition() != -1) {
insurerEmail = insurersEmails[Constat.getInstance().getAccidentList().get(i).getCar().getInsurerPosition()];
}
if (Constat.getInstance().getAccidentList().get(i).getSendOption() != 1 &&
Constat.getInstance().getAccidentList().get(i).getSendOption() != 2 &&
!insurerEmail.equals("null")) {
final int finalI = i;
Runnable progressRunnable = new Runnable() {
#Override
public void run() {
try {
String[] toArray = new String[1];
toArray[0] = insurersEmails[Constat.getInstance().getAccidentList().get(finalI).getCar().getInsurerPosition()];
String subject = getResources().getString(R.string.pdf_joint_report);
InputStream is;
String str;
byte[] buffer = null;
int size;
if (Locale.getDefault().getLanguage().equals("en")) {
is = getAssets().open("insurerEmailTemplateENG.html");
} else {
is = getAssets().open("insurerEmailTemplateFR.html");
}
size = is.available();
buffer = new byte[size];
is.read(buffer);
is.close();
String destPath = Constat.getInstance().getPdfPath().replace(".pdf", "_copy" + Constat.getInstance().getAccidentList().get(finalI).getNumAccident() + ".pdf");
String destPath1 = Constat.getInstance().getPdfPath().replace(".pdf", "_copy1.pdf");
if (insurersPdfStructure[Constat.getInstance().getAccidentList().get(finalI).getCar().getInsurerPosition()].equals("1")) {
List<File> filesList = new ArrayList<>();
if (PdfController.getInstance(activityRef.get()).getAnnexePref()) {
filesList.add(new File(Constat.getInstance().getPdfPath()));
filesList.add(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + PACKAGE_NAME + "/annexe.pdf"));
} else {
filesList.add(new File(Constat.getInstance().getPdfPath()));
}
File outputFile = new File(destPath1);
try {
Utilities.mergePdfDocuments(filesList, outputFile);
} catch (DocumentException | IOException e) {
e.printStackTrace();
}
} else {
try {
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(destPath1));
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfReader reader = new PdfReader(new FileInputStream(Constat.getInstance().getPdfPath()));
for (int j = 0; j < reader.getNumberOfPages(); j++) {
PdfImportedPage page = writer.getImportedPage(reader, j + 1);
if (j == 0) {
PdfDictionary parameters = new PdfDictionary();
parameters.put(PdfName.MODDATE, new PdfDate());
PdfFileSpecification fileSpec = PdfFileSpecification.fileEmbedded(
writer, Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + PACKAGE_NAME + "/annexe.xml",
"annexe.xml", null, "application/xml", parameters, 0);
fileSpec.put(new PdfName("annexe"), new PdfName("Data"));
writer.addFileAttachment("annexe.xml", fileSpec);
PdfFileSpecification fileSpec1 = PdfFileSpecification.fileEmbedded(
writer, Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + PACKAGE_NAME + "/xml_def.xsd",
"xml_def.xsd", null, "application/xml", parameters, 0);
fileSpec.put(new PdfName("xml_def"), new PdfName("Data"));
writer.addFileAttachment("xml_def.xsd", fileSpec1);
PdfArray array = new PdfArray();
array.add(fileSpec.getReference());
array.add(fileSpec1.getReference());
writer.getExtraCatalog().put(new PdfName("AF"), array);
}
cb.addTemplate(page, 0, 0);
document.newPage();
}
document.close();
} catch (DocumentException | IOException e) {
e.printStackTrace();
}
}
try {
File file1 = new File(destPath);
file1.getParentFile().mkdirs();
Utilities.sealPdf(destPath1, destPath, insurersPasswords[Constat.getInstance().getAccidentList().get(finalI).getCar().getInsurerPosition()]);
} catch (DocumentException e) {
e.printStackTrace();
}
ArrayList<Uri> uris = new ArrayList<Uri>();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
uris.add(Uri.fromFile(new File(destPath)));
else
uris.add(FileProvider.getUriForFile(context, getApplicationContext().getPackageName() + ".provider", new File(destPath)));
str = new String(buffer);
str = str.replace("{#CAROWNER}", Constat.getInstance().getAccidentList().get(finalI).getCar().getOwner().getFirstName() + " " + Constat.getInstance().getAccidentList().get(finalI).getCar().getOwner().getLastName());
final int i1 = finalI;
final int lastInsurerPosition1 = lastInsurerPositionCopy;
final String[] toArray1 = toArray;
final String str1 = str;
final String subject1 = subject;
final ArrayList<Uri> uris1 = uris;
runOnUiThread(new Runnable() {
#Override
public void run() {
if (i1 != lastInsurerPosition1) {
Utilities.sendEmails(activityRef.get(), toArray1, null, str1, subject1, uris1, 3);
} else {
Utilities.sendEmails(activityRef.get(), toArray1, null, str1, subject1, uris1, 4);
}
while (!activityRef.get().hasWindowFocus()) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
};
LongOperation lo = new LongOperation(PdfActivity.this, progressRunnable, getResources().getString(R.string.generating),
getResources().getString(R.string.generating_email_n_for_insurer, Constat.getInstance().getAccidentList().get(i).getDriver().getFirstName()));
lo.execute();
}
}
} else {
// delete signature image file and redirect user to home screen
for (int j = 0; j < Constat.getInstance().getAccidentList().size(); j++) {
File file = new File(Constat.getInstance().getAccidentList().get(j).getSignatureFilePath());
file.delete();
}
// Reset Pdf instance
PdfController.destroyInstance();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(getResources().getString(R.string.send_success))
.setMessage(getResources().getString(R.string.emails_sended))
.setCancelable(false)
.setPositiveButton(getResources().getString(R.string.ok_button), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
Thanks in advance.
I can write an abstract snippet, cuz i don't understand your code, it can be a guide (if it helps) to alter your code to, i will post answer
Declare this class scope
Queue<MyItem> queue = new LinkedList<MyItem>();
//MyItem is a type i think it's what in 'Constat.getInstance().getAccidentList()'
//it should be the type you have to be processed (email and PDF)
your current code (onActivityResult) don't start processing, just add to Queue, and process first item in Queue:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
for(int i = 0; i < Constat.getInstance().getAccidentList().size(); i++){
//add to-be-processed items in the Queue
queue.add(Constat.getInstance().getAccidentList().get(i));
}//for loop
//when loop finish, start processing first item
MyItem item = queue.remove();
processItem(item);
}
each time onResume() is called, check queue size, if empty
that can be all items were processed, or this is the first time the activity is open, so no items to process yet
#Override
protected void onResume()
{
super.onResume();
if(queue.size() != 0){
processItem(queue.remove());
}//we still have items to process
}
your actual code is here, to create PDF, create email , send email.
private void processItem(MyItem item){
//start runnable ... to create PDF ...
//create email body, and start email sending action
}

How to read text dictionary from assests display on Candidate view in Softkeyboard

I'm developing softkeyboard in updateCandidates(). My question is how to retrieve each text when Composing have some words then load my dictionary in candidate view.
Image:
How to load words.
Image
like this
Thanks for share me any information about it.
Solution
You can make method like this :
public ArrayList<String> readFile() {
ArrayList<String> list = new ArrayList<String>();
try {
AssetManager am = getAssets();
InputStream data = am.open("data.txt");
InputStreamReader dataInputStream = new InputStreamReader(data);
BufferedReader reader = new BufferedReader(dataInputStream);
String each = null;
while ((each = reader.readLine()) != null) {
list.add(each);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
Edit Softkeyboard
pickSuggestionManually()
public void pickSuggestionManually(int index) {
if (mCompletionOn && mCompletions != null && index >= 0 && index < mCompletions.length) {
CompletionInfo ci = mCompletions[index];
getCurrentInputConnection().commitCompletion(ci);
if (mCandidateView != null) {
mCandidateView.clear();
}
updateShiftKeyState(getCurrentInputEditorInfo());
} else if (mComposing.length() > 0) {
// If we were generating candidate suggestions for the current
// text, we would commit one of them here. But for this sample,
// we will just commit the current text.
mComposing.setLength(index);
mComposing = new StringBuilder(mCandidateList.get(index) + " ");
commitTyped(getCurrentInputConnection());
}
}
Important here updateCandidates()
ArrayList<String> listData = readFile();
System.out.println("Sonu Kumar: " + listData);
if (!mCompletionOn) {
if (mComposing.length() > 0) {
ArrayList<String> list = new ArrayList<String>();
// list.add(mComposing.toString());
for (int j = 0; j < listData.size(); j++) {
String str = mComposing.toString().toLowerCase();
if (listData.get(j).startsWith(str)) {
list.add(listData.get(j));
}
}
mCandidateList = list;
setSuggestions(list, true, true);
} else {
setSuggestions(null, false, false);
}
}
Thank
To show the suggestion words in candidate view, please modify the updateCandidates() method of SoftKeyboard.java file:
ArrayList<String> list = new ArrayList<String>();
updateCandidate(){
if (!mCompletionOn) {
list.clear();
if (mComposing.length() > 0) {
String mStr= mComposing.toString().toLowerCase();
for (int i = 0; i < Dictionary.data.length; i++) {
String str = Dictionary.data[i];
if (str.startsWith(mStr)) {
list.add(str);
}
}
setSuggestions(list, true, true);
} else {
setSuggestions(null, false, false);
}
}
}
To enter the picked word from candidate view to input text, please modify following method in SoftKeyboard example project.
public void pickSuggestionManually(int index) {
if (mCompletionOn && mCompletions != null && index >= 0
&& index < mCompletions.length) {
CompletionInfo ci = mCompletions[index];
getCurrentInputConnection().commitCompletion(ci);
if (mCandidateView != null) {
mCandidateView.clear();
}
updateShiftKeyState(getCurrentInputEditorInfo());
} else if (mComposing.length() > 0) {
mComposing.setLength(index);
mComposing = new StringBuilder(mCandidateList.get(index) + " ");
commitTyped(getCurrentInputConnection());
}
}

Compare contents of two files line by line

public class MainActivity extends Activity {
FileOutputStream fos;
FileInputStream fOne, fTwo;
ArrayList<String> arr1 = new ArrayList<String>();
ArrayList<String> arr2 = new ArrayList<String>();
ArrayList<String> words = new ArrayList<String>();
ArrayList<String> wordsTwo = new ArrayList<String>();
int count = 0;
int countTwo = 0;
int countThree = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button fileOne = (Button)findViewById(R.id.file1);
Button fileTwo = (Button)findViewById(R.id.file2);
Button compare = (Button)findViewById(R.id.compare);
arr1.add("1");
arr1.add("2");
arr1.add("3");
arr1.add("4");
//arr1.add("3");
arr2.add("1");
arr2.add("2");
fileOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try
{
fos = openFileOutput("File1", Context.MODE_PRIVATE);
for(int temp = 0; temp< arr1.size(); temp++)
{
fos.write((arr1.get(temp).getBytes()) );
fos.write(System.getProperty("line.separator").getBytes());
}
fos.close();
fos.flush();
}
catch(Exception e)
{
}
}
});
fileTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try
{
fos = openFileOutput("File2", Context.MODE_PRIVATE);
for(int temp = 0; temp< arr2.size(); temp++)
{
fos.write((arr2.get(temp).getBytes()) );
fos.write(System.getProperty("line.separator").getBytes());
}
fos.close();
fos.flush();
}
catch(Exception e)
{
}
}
});
compare.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try
{
fOne = openFileInput("File1");
fTwo = openFileInput("File2");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Scanner scanFile = new Scanner(new DataInputStream(fOne));
Scanner scanFileT = new Scanner(new DataInputStream(fTwo));
words = new ArrayList<String>();
wordsTwo = new ArrayList<String>();
while (scanFile.hasNextLine())
{
if(scanFile.nextLine()!=null)
{
count++;
}
while(scanFileT.hasNextLine())
{
if(scanFileT.nextLine()!=null)
{
countTwo++;
}
}
}
try
{
fOne.close();
fTwo.close();
scanFile.close();
scanFileT.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(getBaseContext(), "One : " + count, 1000).show();
Toast.makeText(getBaseContext(), "Two : " + countTwo, 1000).show();
Toast.makeText(getBaseContext(), "Three : " + countThree, 1000).show();
count = 0 ;
countTwo = 0;
countThree = 0;
}
});
}
}
Above is the code to write and read the file. What I did here, write two files and read the contents..Now I have to compare contents of files line by line. What needs to be done?
Try following code. This will give you desired output. I took files from asset directory. So you need to replace that line of code if you are taking files from other directory.
private void compareFiles() throws Exception {
String s1 = "";
String s2 = "", s3 = "", s4 = "";
String y = "", z = "";
// Reading the contents of the files
BufferedReader br = new BufferedReader(new InputStreamReader(
getAssets().open("first.txt")));
BufferedReader br1 = new BufferedReader(new InputStreamReader(
getAssets().open("second.txt")));
while ((z = br1.readLine()) != null) {
s3 += z;
s3 += System.getProperty("line.separator");
}
while ((y = br.readLine()) != null) {
s1 += y;
s1 += System.getProperty("line.separator");
}
// String tokenizing
StringTokenizer st = new StringTokenizer(s1);
String[] a = new String[10000];
for (int l = 0; l < 10000; l++) {
a[l] = "";
}
int i = 0;
while (st.hasMoreTokens()) {
s2 = st.nextToken();
a[i] = s2;
i++;
}
StringTokenizer st1 = new StringTokenizer(s3);
String[] b = new String[10000];
for (int k = 0; k < 10000; k++) {
b[k] = "";
}
int j = 0;
while (st1.hasMoreTokens()) {
s4 = st1.nextToken();
b[j] = s4;
j++;
}
// comparing the contents of the files and printing the differences, if
// any.
int x = 0;
for (int m = 0; m < a.length; m++) {
if (a[m].equals(b[m])) {
} else {
x++;
Log.d("Home", a[m] + " -- " + b[m]);
}
}
Log.d("Home", "No. of differences : " + x);
if (x > 0) {
Log.d("Home", "Files are not equal");
} else {
Log.d("Home", "Files are equal. No difference found");
}
}
Input File 1
Hi
Hello
Chintan
Rathod
Input File 2
Hi
HellO
Chintan
RathoD
Output
08-26 12:07:58.219: DEBUG/Home(2350): Hello3. -- HellO3.
08-26 12:07:58.219: DEBUG/Home(2350): Rathod -- RathoD
08-26 12:07:58.229: DEBUG/Home(2350): No. of differences : 2
08-26 12:07:58.229: DEBUG/Home(2350): Files are not equal
Edit
To get Difference between two files
Use StringUtils library which is provide by Apache and check this Documentation for more about that library.
And modify following lines of code.
int x = 0;
for (int m = 0; m < a.length; m++) {
if (a[m].equals(b[m])) {
} else {
x++;
Log.d("Home", a[m] + " -- " + b[m]);
//to print difference
if (a[m].length() < b[m].length())
Log.d("Home", "" + StringUtils.difference(a[m], b[m]));
else
Log.d("Home", "" + StringUtils.difference(b[m], a[m]));
}
}
Output
08-26 17:51:26.949: DEBUG/Home(17900): 12 -- 123
08-26 17:51:26.949: DEBUG/Home(17900): Difference String : 3
08-26 17:51:26.949: DEBUG/Home(17900): No. of differences : 1
08-26 17:51:26.949: DEBUG/Home(17900): Files are not equal
Try using java.util.Scanner
while (sc1.hasNext() && sc2.hasNext()) {
String str1 = sc1.next();
String str2 = sc2.next();
if (!str1.equals(str2))
System.out.println(str1 + " != " + str2);
}
Change your while loop to the following:
while (scanFile.hasNextLine() && scanFileT.hasNextLine())
{
if(scanFileT.nextLine().equals(scanFile.nextLine()))
{
// The lines are equal.
} else {
// The lines are not equal.
}
}
if(scanFile.hasNextLine() || scanFileT.hasNextLine())
{
// If more lines remain in one of the files, they are not equal.
} else {
// If no content remains in both files, they are equal.
}
Depending on the size of your file, I would recommend some optimisation like checking the file sizes before you go through them line by line.
The overall logic reads as follows; if both have another line, compare it to see if it is equal. If they don't have another line, check if one of them has lines remaining, if so, they are not equal.
Update
After clarifying the objective of the comparison in chat, see the comments to this question, I have come to the conclusion that another comparison would be more effective and, as a matter of fact, correct. The comparison algorithm above works great if comparing the structure of text but not if comparing a data vector which may or may not be sorted. After some discussion, we came to the conclusion that data needs to be sorted or the comparison will blow the complexity to at least O(n^2)which could be done in O(2n) if the data is sorted. Here the algorithm's skeleton:
if(! scanGroupFriends.hasNextLine())
{
//simple sanity check to see if we need to compare at all. In this case, add all friends.
} else {
String nextFriend = scanGroupFriends.nextLine();
while(scanAllFriends.hasNextLine())
{
if(scanAllFriends.nextLine().equals(nextFriend))
{
// Friend already figures, do not add him and advance the list of group friends.
if(scanGroupFriends.hasNextLine())
{
nextFriend = scanGroupFriends.nextLine();
} else {
// There are no more friends in the group, add all remaining friends to list to show.
break; // Terminate the `while` loop.
}
}
}
}
However, I personally think it is bad to make to many assumptions. What I would suggest is that the friends be saved in a Set, a TreeSet for example. Then, serialize the object rather than manually writing it to file. Sets are neat because they hold several interesting objects. For example, you could easily use the following code to remove all friends in a group from the set of all friends:
allFriends.removeAll(groupFriends);
However, be aware that this removes it from the set completely so you should make a copy beforehand.

File won't read into multiple array positions

I'm trying to store each line from a file into an arraylist and then combine two arraylists into one. Currently, when I try this, all the different lines are being stored in one line. I want it to say something like User : Score . However, right now it is showing up like UseruserUsernamePerson : Score. (many different names and only one score). Can anyone see where I'm going wrong here? Also, pardon my poor naming practice. My array lists used to be Vectors, but I changed them into ArrayLists and forgot to change their titles.
public class DisplayScores extends ListActivity{
private ArrayList<String> scoreVector = new ArrayList<String>();
private ArrayList<String> userVector = new ArrayList<String>();
private ArrayList<String> comboVector = new ArrayList<String>();
private int c = 0;
File root = Environment.getExternalStorageDirectory();
File scores = new File(root, "scores.txt");
File users = new File(root, "names.txt");
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
String line = null;
try {
FileReader scoresFileReader = new FileReader(scores);
BufferedReader scoresReader = new BufferedReader(scoresFileReader);
while ((line = scoresReader.readLine())!= null)
{
scoreVector.add(line);
}
scoresFileReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String userLine = null;
try{
FileReader userFileReader = new FileReader(users);
BufferedReader userReader = new BufferedReader(userFileReader);
while((userLine = userReader.readLine())!= null)
{
userVector.add(userLine);
}
userReader.close();
} catch (IOException e){
e.printStackTrace();
}
for(String s : scoreVector)
{
comboVector.add(userVector.get(c) + ": " + s);
}
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, comboVector));
}
}
from the code it seems that the value of c is not incrementing..
c is always 0
for(String s : scoreVector)
{
comboVector.add(userVector.get(c) + ": " + s);
}

I'm not understanding the following code

I have to understand this code to create my own app(almost based on this function):
public static String[][] ReadFilePerLine(Context context, String nom) {
int i = 0;
try {
FileInputStream fIn = context.openFileInput(nom);
InputStreamReader ipsr = new InputStreamReader(fIn);
BufferedReader b = new BufferedReader(ipsr);
i = getLineNumber(context, nom);
String[][] s = new String[2][i/2];
i = 0;
String ligne;
int j = 0;
while ((ligne = b.readLine()) != null) {
if (i % 2 == 0)
s[0][j] = ligne;
else {
s[1][j] = ligne;
j++;
}
i++;
}
fIn.close();
ipsr.close();
return s;
}
catch (Exception e)
{}
I'm not understanding why the using of a 2D array? and with two rows ?(String[][] s = new String[2][i/2];)
here is the data that it will be stored in the file:
data = date + " : " + y + "L/100KM"+ " " + value1 + "L "+ value2 + "KM\n";
Necessary functions:
public void updatelv(Activity activity) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String fileName = getResources().getString(R.string.fileName);
fileDir = "" + preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
s = myIO.ReadFilePerLine(getApplicationContext(), fileDir+fileName);
ListView L = (ListView) findViewById(R.id.lv);
L.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, s[0]));
for (int i = 0; i< s[0].length; i++) {
Log.d("Saves",s[0][i]);
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.histo);
context = getApplicationContext();
activity = this;
final SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(context);
String fileName = getResources().getString(R.string.fileName);
fileDir = "" + preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
s = myIO.ReadFilePerLine(getApplicationContext(), fileDir + fileName);
updatelv(this);
ListView L = (ListView) findViewById(R.id.lv);
L.setTextFilterEnabled(true);
L.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
String tmp = s[1][position];
if (tmp == null)
tmp = "Aucun fichier trouvé!";
Toast.makeText(getApplicationContext(), tmp, Toast.LENGTH_SHORT)
.show();
}
});
ReadFilePerLine function:
public static String[][] ReadFilePerLine(Context context, String nom) {
int i = 0;
try {
FileInputStream fIn = context.openFileInput(nom);
InputStreamReader ipsr = new InputStreamReader(fIn);
BufferedReader b = new BufferedReader(ipsr);
i = getLineNumber(context, nom);
String[][] s = new String[2][i/2];
i = 0;
String ligne;
int j = 0;
while ((ligne = b.readLine()) != null) {
if (i % 2 == 0)
s[0][j] = ligne;
else {
s[1][j] = ligne;
j++;
}
i++;
}
fIn.close();
ipsr.close();
return s;
}
catch (Exception e)
{
}
Thank you for you help.
The code is clearly reading from a file whose format consists of pairs of lines; it puts the first line of each pair in s[0][...] and the second line of each pair in s[1][...]. If your format doesn't have that peculiarity -- which it doesn't sound as if it does -- then you don't need to do that. Just make an ordinary 1-dimensional array of Strings.
It appears that what they are doing is breaking the file down into two lists (or String arrays, in this case), one which contains all the even-numbered lines, and one which contains all the odd-numbered lines. I'll comment up the code for you:
public static String[][] ReadFilePerLine(Context context, String nom) {
int i = 0;
try {
//open the specified input file and create a reader
FileInputStream fIn = context.openFileInput(nom);
InputStreamReader ipsr = new InputStreamReader(fIn);
BufferedReader b = new BufferedReader(ipsr);
//get the total number of lines in the file, and allocate
//a buffer large enough to hold them all
i = getLineNumber(context, nom);
String[][] s = new String[2][i/2];
i = 0; //set the current line to 0
String ligne;
int j = 0; //set the section index to 0
//now read through the lines in the file, and place every
//even-numbered line in the first section ('s[0]'), and every
//odd-numbered line in the second section ('s[1]')
while ((ligne = b.readLine()) != null) {
if (i % 2 == 0)
//even-numbered line, it goes into the first section
s[0][j] = ligne;
else {
//odd-numbered line, it goes into the second section
s[1][j] = ligne;
j++; //increment the section index
}
i++; //increment the line count
}
//done, cleanup and return
fIn.close();
ipsr.close();
return s;
}
catch (Exception e) {
//should at least log an error here...
}
}
As to why they chose to use a String[][], I cannot say. Probably for convenience, since they want a single object that they can return from this function that contains both lists. Personally I would use a Map that has two List instances in it, but the String[][] works just as well and is probably marginally more efficient.
Judging from your example data it does not appear that you need to use this format. But if you want to use it, you need to structure your data so that the key is on one line, and its associated value is on the next, like:
date
2011-03-19
userName
someGuy
it seems to read from a file, split it into the two dimensional array (based on row count).
Why it does it? I have no idea why you'd want that. Check out the function that it returns s to and find out!

Categories

Resources