如何检索pojo初始化类的数据?

时间:2016-12-02 10:20:56

标签: java android mysql pojo

我已经做了什么?

我想在数组中获取调用历史记录并希望将该数组传递给MySQL数据库,因此我创建了pojo类并在getter和setter方法中设置该变量。这是我的pojo类的java代码..

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;
    }
}

从这个pojo类我希望将调用详细信息设置到该初始化变量中。这是我的MainActivity的java代码。从这段代码我每次都是databean的新实例,所以我每次都会得到一个新的arraylist,但我只想要一个整数数组...

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);*/

}

}

从这里我可以在logcat中获得数字,持续时间,日期和呼叫类型。

我想要什么?

我想在databean的arraylist中添加它不是为什么,但每次databean都显示以下文本..

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

我没有获得arraylist值,我希望将该数组插入mysql表。

1 个答案:

答案 0 :(得分:0)

这里有一个while循环来读取游标。

while (managedCursor.moveToNext()) {

在那里,你想要生成你的Bean插入列表中的那些。

目前,您只在一个实例中保存该值。您应该每次都构建一个新实例:

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

但为此,您需要该值,因此将光标的值保存到您拥有的变量中,而不是实例中。而且当然。在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());