Android将Fragment参数而不是List传递给ListView

时间:2014-09-01 11:35:19

标签: java android listview android-sqlite

来自WebServer的

我可以使用这个类获取数据并将其正确传递到ListView中:

ReceiveFields类结构:

public class ReceiveFields {
    public long lastId;
    public String smsNumber;
    public String mobileNumber;
    public String senderName;
    public String smsBody;
    public String receiveDate;
    private Context ctx;
}

ReceivedSMS从WebService获取数据的类:

public class ReceivedSMS extends ListFragment implements AbsListView.OnScrollListener {
    private List<ReceiveFields> rows;
    private int prevVisibleItem;
    private TSMS tsms;
    private String username;
    private String password;
    public  Long getLastReceivedSMSID;
    private boolean isFirstTime;
    private Context context;

    public ResivedSMS(Context context, String username, String password) {
        this.username = username;
        this.password = password;
        this.context = context;
    }

    public ResivedSMS(String username, String password, long start, long count,Context context) {
        this.username = username;
        this.password = password;
        this.context = context;
        tsms = new TSMS(context ,new User(username, password));
        getResivedSMS(start , count);
    }

    public ResivedSMS(List<ReceiveFields> receivedSMSList,Context context){
        rows = receivedSMSList;
        this.context = context;
    }

    public List<ReceiveFields> getResivedSMS(long start, long count) {
        tsms = new TSMS(context,new User(this.username, this.password));
        try {
            rows = tsms.getReceivedSMS(start, count);
        }
        catch (TException e) {
            e.printStackTrace();
            Log.e("ERROR IN Fetch SMS From WebService List<ReceiveFields> getResivedSMS(long start, long count) " , String.valueOf(e));
        }
        return rows;
    }

将ResivedSMS类用作:

ResivedSMS receivedSMSList = new ReceivedSMS("username","password",0,20,context);

并传入ListView:

getSupportFragmentManager().beginTransaction().replace(R.id.drawer, receivedSMSList).commit();

这一切都是正确的,我没有问题。但我想从DataBase中获取数据并将其作为以下内容传递给ReceivedSMS:

List<ReceiveFields> r1 = db.getAllReceivedSMSFromDatabase();

receivedSMSList = new ResivedSMS(r1,context);

getSupportFragmentManager().beginTransaction().replace(R.id.drawer, receivedSMSList).commit();

getAllReceivedSMSFromDatabase功能:

public List<ReceiveFields> getAllReceivedSMSFromDatabase() {
    SQLiteDatabase db = this.getReadableDatabase();
    String selectQuery = "SELECT * FROM " + this.RECEIVE_FIELDS_TABLE ;
    Cursor cursor = db.rawQuery(selectQuery, null);
    List<ReceiveFields> ListSMS = new ArrayList<ReceiveFields>();
    cursor.moveToFirst();
    while (cursor.moveToNext()) {
        ListSMS.add(new ReceiveFields(
                Long.valueOf(cursor.getString(cursor.getColumnIndex("lastId"))),
                cursor.getString(cursor.getColumnIndex("smsNumber")),
                cursor.getString(cursor.getColumnIndex("mobileNumber")),
                cursor.getString(cursor.getColumnIndex("senderName")),
                cursor.getString(cursor.getColumnIndex("smsBody")),
                cursor.getString(cursor.getColumnIndex("receiveDate"))));
    }

    cursor.close();
    db.close();
    return ListSMS;
}

注意:我的数据库不为空并且有20行

getAllReceivedSMSFromDatabase返回值与getResivedSMS类似,但我收到此错误:

enter image description here

getAllReceivedSMSFromDatabase使用`LogCat:

的函数结果
ID: 19  lastId: 29901991  smsNumber: 30007227  mobileNumber: 09000000892  senderName 09122510892  smsBody: tsssasaS  receiveDate: 2014/8/3

ID: 19  lastId: 29901992  smsNumber: 30007227  mobileNumber: 09000000892  senderName 09122510892  smsBody: tsssasaS  receiveDate: 2014/9/3

ID: 19  lastId: 29901993  smsNumber: 30007227  mobileNumber: 09000000892  senderName 09122510892  smsBody: tsssasaS  receiveDate: 2014/10/3

1 个答案:

答案 0 :(得分:2)

问题在于您将列表作为参数传递给replace(),并且它期望将Fragment作为参数。

  

public abstract FragmentTransaction replace(int containerViewId, Fragment fragment

您已将列表作为参数传递,而不是传递片段。

尝试传递 ReceivedSMS extends ListFragment)而不是 ListSMS 这是一个列表(List<ReceiveFields> ListSMS