由Messagehandler创建的SimpleCursorAdapter ListView

时间:2012-12-14 12:38:55

标签: android listview simplecursoradapter

我已经阅读了有关simplecursoradapter和listview的所有帖子,但我仍然无法找到问题的答案。 我有一个活动,其中包含许多单选按钮,一个按钮和一个列表视图。 Activity首先向Web服务器发出http请求以检索某些数据。来自服务器的数据将写入设备上的sqlite数据库。将数据写入数据库后,Activity将重新获得控制权并从数据库创建一个Cursor,一个SimpleCursorAdapter并将适配器设置为listview适配器。所有数据都可以写入数据库,我已经循环遍历游标中的元素并且它包含所有预期的元素,但我的列表视图中仍然没有显示任何元素。

Activity.java:

public class MyActivity extends Activity {
private RadioButton rb1, rb2;
private Button addBut;
private ListView lv;
private LinearLayout statusLayout;
private ScrollView scroll;
private String username;
private DBHelper db;
private Cursor cursor;
private static final String userfk = "1";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylayout);

    rb1 = (RadioButton)findViewById(R.id.rb1);
    rb1.setChecked(true);
    rb2 = (RadioButton)findViewById(R.id.rb2);
    addBut = (Button)findViewById(R.id.addButton);
    statusLayout = (LinearLayout)findViewById(R.id.statusLayout);
    scroll = (ScrollView)findViewById(R.id.configscroll);
    lv = (ListView)findViewById(R.id.profilesll);
    //get data from server, made in asynchtask   
    getData();
}

从AsynchTask代码调用MessageHandler:

private class MyHandler extends Handler {
public MyHandler() {
}
public void handleMessage(Message msg) {
        final Bundle b = msg.getData();
        if (b.getString("result").equalsIgnoreCase(MyResult.ERROR) || b.getString("result").equalsIgnoreCase(MyResult.FAIL)) {
                 //Handle error
        } else {
            handleList();
            statusLayout.setVisibility(View.GONE);
            scroll.setVisibility(View.VISIBLE);
        }
    }
}

private void handleList() {
    if (db == null) {
        db = new DBHelper(this);
    }
    if (cursor == null) {
        try {
            cursor = db.getListCursor(userfk);
        } catch (Exception e) {
            err = e.getMessage();
        }
        if (cursor == null) {
       Toast.makeText(MyActivity.this, err != null ? err : getResources().getString(R.string.myerror), Toast.LENGTH_LONG).show();
        } else {
            try {
                SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, new String[] {DBHelper.COLUMN_ID}, new int[] {android.R.id.text1});
                lv.setAdapter(adapter);
            } catch (RuntimeException e) {
                Log.e("test", e.getMessage());
            } catch (Exception e) {
                Log.e("test", e.getMessage());
            }
        }
    }
}

DBHelper类

public Cursor getListCursor(String userfk) throws Exception {
    StringBuilder builder = new StringBuilder("select name as _id from mytable where  userfk=?");
    List<String> values = new ArrayList<String>();
    values.add(userfk);
    try {
        return this.getReadableDatabase().rawQuery(builder.toString(), values.toArray(new String[values.size()]));
    } catch (Exception e) {
        Log.d("test", e.getMessage());
        throw e;
    }
}

我希望有人可以暗示我做错了什么!谢谢你的帮助!

0 个答案:

没有答案