0

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

活动.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;
    }
}

我希望有人能给我提示我做错了什么!谢谢你的帮助!

4

0 回答 0