-3

What i already done?

I want to get a call history in a array and want to pass that array to MySQL database so i have created pojo class and set that variable in getter and setter method. here's my java code for pojo class..

public class DataBean {
    String number;
    String type;
    String date;
    String duration;

    public DataBean(String number, String type, String date, String duration) {
        this.number = number;
        this.type = type;
        this.date = date;
        this.duration = duration;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getDuration() {
        return duration;
    }

    public void setDuration(String duration) {
        this.duration = duration;
    }
}

from this pojo class i want set call detail into that initialized variable. here's my java code for MainActivity.and from this code i am making every time a new instance of databean so i'm getting each time a new arraylist but i want just one array of whole data..

public class MainActivity extends AppCompatActivity {

ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = (ListView) findViewById(R.id.list);
    getCallDetails();
}

public void getCallDetails() {

    String strOrder = android.provider.CallLog.Calls.DATE + " DESC";

    Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null, null, null, strOrder);
    int number1 = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
    int type1 = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
    int duration1 = managedCursor.getColumnIndex(CallLog.Calls.DURATION);


    while (managedCursor.moveToNext()) {

        String number = managedCursor.getString(number1);
        String type2 = managedCursor.getString(type1);
        String date11 = managedCursor.getString(managedCursor.getColumnIndexOrThrow("date")).toString();
        java.util.Date date1 = new java.util.Date(Long.valueOf(date11));
        String duration = managedCursor.getString(duration1);
        String type = null;

        String date = date1.toString();

        int callcode = Integer.parseInt(type2);
        switch (callcode) {
            case CallLog.Calls.OUTGOING_TYPE:
                type = "Outgoing";
                break;
            case CallLog.Calls.INCOMING_TYPE:
                type = "Incoming";
                break;
            case CallLog.Calls.MISSED_TYPE:
                type = "Missed";
                break;

        }
        DataBean dataBean = new DataBean(number, type, date, duration);
        ArrayList<DataBean> arrayList = new ArrayList<>();

        arrayList.add(dataBean);
        Log.d("tag", arrayList.toString());

    }
    managedCursor.close();


    /*//ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
    listView.setAdapter(adapter);*/

}

}

from this i can get number, duration ,date and call type in a logcat.

What i want?

i want to add that in a arraylist of databean don't why but every time databean shows following text..

12-02 15:18:50.636 8034-8034/com.example.hiren.callhistory D/Arrat: [com.example.hiren.callhistory.DataBean@46540ec]

i'm not getting arraylist value and i want insert that array into mysql table.

Hiren Gujarati
  • 55
  • 1
  • 10
  • Please read this article about `toString` http://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java It will explain you how to print values properly – jakubbialkowski Dec 02 '16 at 10:24
  • You need to generate a new dataBean in the loop for each line maybe ? And add it IN the loop. Here you are settting the value in the same instance. So this will be override each time and only the last values will be kept. For the list, well you add after the loop so you will only have on value. – AxelH Dec 02 '16 at 10:24
  • thanks for your response @jakubbialkowski but i don't want string i want arraylist or array that i can insert into database – Hiren Gujarati Dec 02 '16 at 10:26
  • thanks for response @AxelH i am not able to initialize the Databean into loop it gives error of unreachable statement – Hiren Gujarati Dec 02 '16 at 10:28
  • That can't be, you don't have break, return or any statement that could provide an unreachable error. Show how you have done it. Anyway, if you don't generate new instance in it. You will have only ONE instance. You should follow some course in Java if this is not clear – AxelH Dec 02 '16 at 10:35

1 Answers1

0

Here you have a while loop to read a cursor.

while (managedCursor.moveToNext()) {

In there, you want to generate your Bean an insert those in a list.

For now, you only save the value in ONE instance. You should build a new instance each time :

while (managedCursor.moveToNext()) {
    dataBean = new DataBean(...);
}

but for that, you need the value, so save the values of the cursor into the variable you have, not in the instance. And of course. Add the instance in the list after it is instanciate

ArrayList<DataBean> list = new ArraysList<>();
while (managedCursor.moveToNext()) {
    number = managedCursor.getString("number");
    type = managedCursor.getString("type");
    ...
    dataBean = new DataBean(number, type, date, duration);
    list.add(dataBean);
}
Log.d("tag", arrayList.toString());
AxelH
  • 14,325
  • 2
  • 25
  • 55
  • yaah it's working .. and it gives values like '[com.example.hiren.callhistory.DataBean@1ffec3a9]' how can i get string of that value ?? – Hiren Gujarati Dec 02 '16 at 10:38
  • @HirenGujarati read jakubbialkowski comment. `toString` need to be implemented to return the String format you want. This is some basic Java, you really should learn it before even trying to code this. There are lot of bad coding here. – AxelH Dec 02 '16 at 10:41
  • by taking new object every time i'm getting array each time but i want just one array of all data that i can insert into MySQL database. plz help me to solve this @AxelH – Hiren Gujarati Dec 03 '16 at 05:19
  • @Hiren what do you mean? Please share your code and explain this better... This is not clear – AxelH Dec 03 '16 at 08:39
  • i have edited my code as per your answer and it's really works. but it is giving so many arraylist i just want a 1 array or 1 arraylist of whole data plz help to do this. – Hiren Gujarati Dec 04 '16 at 13:05
  • @Hiren Of course it give you a lot of List since you recreate it each time and you print it in the loop. You need to create the list outside of the loop, then create you bean on each loop to be add in the unique list. Then you can print it outside the loop to see the result. See the last code edited with this explanation. This is a basic loop you know, you might need to take so course of Java before even thinking of coding a Android App, the API is not as easy as it looks! – AxelH Dec 05 '16 at 06:16
  • by taking outside a unique loop it gives me only last entry would you plz explain briefly or edit your answer. – Hiren Gujarati Dec 05 '16 at 06:44