如何从 SQLite 获取列表片段中的 ListView 类?

发布于 2024-12-23 05:26:53 字数 6055 浏览 0 评论 0原文

我已经浏览了几个有关片段的教程,但无法让我的查询显示在列表片段中。

以下是我的原始类,需要转换以在片段活动中使用。我的 QueryDisplay 类中有很多内容,我不知道要从中删除什么以及将其放在哪里才能使其正常工作。主要是,我不知道如何在下面的 Fragment Activity 中调用我的 ListView 类。

提前感谢您的帮助。代码也会非常有帮助!

这是查询 SQLite 数据库的 ListActivity 下面是片段类:

public class QueryDisplay extends ListActivity {

protected TextView activityTitle;

boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;

String extStorageDirectory = Environment.getExternalStorageDirectory()
    .toString();
File dbfile = new File(extStorageDirectory
    + "/myco/myapp/dB/myapp.db");
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);

private static final String QUERY_KEY = "QUERY_KEY";
private static final String QUERY_ORDER = "QUERY_ORDER";

// private static final String NI = "NI";

/**
 * -- Called when the activity is first created
 * ===================================================================
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Bundle extras = getIntent().getExtras();
setContentView(R.layout.list_view2);

/**
 * Populate the ActionBar----------------------------------
 */
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
getActionBar().setBackgroundDrawable(
    getResources().getDrawable(R.drawable.actionbar_bg));
String sT = getIntent().getStringExtra("KEY_SUBTITLE");
actionBar.setSubtitle(sT);

/**
 * Get the query string from last activity and pass it to this
 * activity-----------------------------------------------------
 */
String q = null;
if (extras != null) {
    q = extras.getString(QUERY_KEY);
}
loadQuery(q);
}

/**
 * -- DB QUERY STUFF
 * ===============================================================>
 * 
 * Run the initial query from the previous activity---------------
 */
public void loadQuery(String q) {

if (Environment.getExternalStorageState().equals(
    Environment.MEDIA_MOUNTED)) {

    String qO = getIntent().getStringExtra("QUERY_ORDER");

    Cursor c = db.rawQuery(q + " ORDER BY `_id` " + qO, null);
    setListAdapter(new QueryAdapter(this, c));
    db.close();

} else {
    Alerts.sdCardMissing(this);
}
}

/**
 * The Query Adaptor --------------------------------------------
 */

private class QueryAdapter extends CursorAdapter {

public QueryAdapter(Context context, Cursor c) {
    super(context, c);
    LayoutInflater.from(context);
}

@Override
public void bindView(View v, Context context, final Cursor c) {

    int tvLabel = c.getColumnIndexOrThrow("label");
    String label = c.getString(tvLabel);
    final TextView labelTxt = (TextView) v.findViewById(R.id.label);

    if (labelTxt != null) {
    labelTxt.setText("(" + label + ")");
    }

    int tvTitle = c.getColumnIndexOrThrow("title");
    final String title = c.getString(tvTitle);
    TextView titleTxt = (TextView) v.findViewById(R.id.listTitle);

    if (titleTxt != null) {
    titleTxt.setText(title);
    }

    int tvDescription = c.getColumnIndexOrThrow("description");
    String description = c.getString(tvDescription);
    TextView descriptionTxt = (TextView) v.findViewById(R.id.caption);

    if (descriptionTxt != null) {
    descriptionTxt.setText(description);
    }

    int tvDate = c.getColumnIndexOrThrow("date");
    String date = c.getString(tvDate);
    TextView dateTxt = (TextView) v.findViewById(R.id.dateAdded);

    if (dateTxt != null) {
    dateTxt.setText(date);
    }

    int tvGoto = c.getColumnIndexOrThrow("gotoURL");
    final String gotoURL = c.getString(tvGoto);
    TextView gotoTxt = (TextView) v.findViewById(R.id.dummy);

    if (gotoTxt != null) {
    gotoTxt.setText(gotoURL);
    }

    gotoTxt.setVisibility(View.GONE);
    v.setTag(gotoURL);

    final ListView lv = getListView();
    lv.setEnabled(true);
    lv.setClickable(true);

    lv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View v, int arg2,
        long arg3) {

        // -- Set the domain name in the strings.xml file once the
        // DNS is established for the website.
        String mDomain = getResources().getString(R.string.domain);

        String url = "";
        url = mDomain + (String) v.getTag();

        int nI = c.getColumnIndexOrThrow("intent");
        String intent = c.getString(nI);
        Class<?> nIntent = null;
        try {
        nIntent = Class
            .forName("com.myco.myapp.utili."
                + intent);
        } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }

        int tvTitle = c.getColumnIndexOrThrow("title");
        String title = c.getString(tvTitle);

        int tvLabel = c.getColumnIndexOrThrow("label");
        String label = c.getString(tvLabel);

        String queryKey = "SELECT * FROM " + label;
        c.close();
        db.close();

        Intent i = new Intent(QueryDisplay.this, nIntent);
        i.putExtra("QUERY_KEY", queryKey);
        i.putExtra("KEY_URL", url);

        Log.e("tag", url);

        i.putExtra("KEY_SUBTITLE", title);
        i.putExtra("KEY_LABEL", label);
        i.putExtra("KEY_INTENT", intent);
        i.putExtra("QUERY_ORDER", "ASC");
        i.putExtra("KEY_YPOS", "0.0");
        QueryDisplay.this.startActivity(i);
    }
    });
}

/**
 * Inflate and returned the query to the view
 * --------------------------------
 */
@Override
public View newView(Context context, Cursor c, ViewGroup parent) {
    final View v = LayoutInflater.from(context).inflate(
        R.layout.list_item, parent, false);
    return v;
}
}
}

QueryDisplayFRAGMENT 类:

public class QueryDisplayFRAGMENT extends ListFragment {
boolean mDualPane;
int mCurCheckPosition = 0;

@Override
public void onActivityCreated(Bundle savedState) {
super.onActivityCreated(savedState);

      ****What do I do/put here to get the above list to show in this list fragment???

}
}

I have gone through several tutorials on fragments and I can't get my queries to display in a List Fragment.

Below are my original classes that need converting for use in a fragment activity. My QueryDisplay class has so much in it, I don't know what to remove from it and where to put it to get it working. Mainly, I don't know how to call my ListView Class in the below Fragment Activity.

Thank-Your for your help in advance. Code would be very helpful too!!

Here is the ListActivity that queries a SQLite database And below id the fragment class:

public class QueryDisplay extends ListActivity {

protected TextView activityTitle;

boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;

String extStorageDirectory = Environment.getExternalStorageDirectory()
    .toString();
File dbfile = new File(extStorageDirectory
    + "/myco/myapp/dB/myapp.db");
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);

private static final String QUERY_KEY = "QUERY_KEY";
private static final String QUERY_ORDER = "QUERY_ORDER";

// private static final String NI = "NI";

/**
 * -- Called when the activity is first created
 * ===================================================================
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Bundle extras = getIntent().getExtras();
setContentView(R.layout.list_view2);

/**
 * Populate the ActionBar----------------------------------
 */
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
getActionBar().setBackgroundDrawable(
    getResources().getDrawable(R.drawable.actionbar_bg));
String sT = getIntent().getStringExtra("KEY_SUBTITLE");
actionBar.setSubtitle(sT);

/**
 * Get the query string from last activity and pass it to this
 * activity-----------------------------------------------------
 */
String q = null;
if (extras != null) {
    q = extras.getString(QUERY_KEY);
}
loadQuery(q);
}

/**
 * -- DB QUERY STUFF
 * ===============================================================>
 * 
 * Run the initial query from the previous activity---------------
 */
public void loadQuery(String q) {

if (Environment.getExternalStorageState().equals(
    Environment.MEDIA_MOUNTED)) {

    String qO = getIntent().getStringExtra("QUERY_ORDER");

    Cursor c = db.rawQuery(q + " ORDER BY `_id` " + qO, null);
    setListAdapter(new QueryAdapter(this, c));
    db.close();

} else {
    Alerts.sdCardMissing(this);
}
}

/**
 * The Query Adaptor --------------------------------------------
 */

private class QueryAdapter extends CursorAdapter {

public QueryAdapter(Context context, Cursor c) {
    super(context, c);
    LayoutInflater.from(context);
}

@Override
public void bindView(View v, Context context, final Cursor c) {

    int tvLabel = c.getColumnIndexOrThrow("label");
    String label = c.getString(tvLabel);
    final TextView labelTxt = (TextView) v.findViewById(R.id.label);

    if (labelTxt != null) {
    labelTxt.setText("(" + label + ")");
    }

    int tvTitle = c.getColumnIndexOrThrow("title");
    final String title = c.getString(tvTitle);
    TextView titleTxt = (TextView) v.findViewById(R.id.listTitle);

    if (titleTxt != null) {
    titleTxt.setText(title);
    }

    int tvDescription = c.getColumnIndexOrThrow("description");
    String description = c.getString(tvDescription);
    TextView descriptionTxt = (TextView) v.findViewById(R.id.caption);

    if (descriptionTxt != null) {
    descriptionTxt.setText(description);
    }

    int tvDate = c.getColumnIndexOrThrow("date");
    String date = c.getString(tvDate);
    TextView dateTxt = (TextView) v.findViewById(R.id.dateAdded);

    if (dateTxt != null) {
    dateTxt.setText(date);
    }

    int tvGoto = c.getColumnIndexOrThrow("gotoURL");
    final String gotoURL = c.getString(tvGoto);
    TextView gotoTxt = (TextView) v.findViewById(R.id.dummy);

    if (gotoTxt != null) {
    gotoTxt.setText(gotoURL);
    }

    gotoTxt.setVisibility(View.GONE);
    v.setTag(gotoURL);

    final ListView lv = getListView();
    lv.setEnabled(true);
    lv.setClickable(true);

    lv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View v, int arg2,
        long arg3) {

        // -- Set the domain name in the strings.xml file once the
        // DNS is established for the website.
        String mDomain = getResources().getString(R.string.domain);

        String url = "";
        url = mDomain + (String) v.getTag();

        int nI = c.getColumnIndexOrThrow("intent");
        String intent = c.getString(nI);
        Class<?> nIntent = null;
        try {
        nIntent = Class
            .forName("com.myco.myapp.utili."
                + intent);
        } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }

        int tvTitle = c.getColumnIndexOrThrow("title");
        String title = c.getString(tvTitle);

        int tvLabel = c.getColumnIndexOrThrow("label");
        String label = c.getString(tvLabel);

        String queryKey = "SELECT * FROM " + label;
        c.close();
        db.close();

        Intent i = new Intent(QueryDisplay.this, nIntent);
        i.putExtra("QUERY_KEY", queryKey);
        i.putExtra("KEY_URL", url);

        Log.e("tag", url);

        i.putExtra("KEY_SUBTITLE", title);
        i.putExtra("KEY_LABEL", label);
        i.putExtra("KEY_INTENT", intent);
        i.putExtra("QUERY_ORDER", "ASC");
        i.putExtra("KEY_YPOS", "0.0");
        QueryDisplay.this.startActivity(i);
    }
    });
}

/**
 * Inflate and returned the query to the view
 * --------------------------------
 */
@Override
public View newView(Context context, Cursor c, ViewGroup parent) {
    final View v = LayoutInflater.from(context).inflate(
        R.layout.list_item, parent, false);
    return v;
}
}
}

QueryDisplayFRAGMENT Class:

public class QueryDisplayFRAGMENT extends ListFragment {
boolean mDualPane;
int mCurCheckPosition = 0;

@Override
public void onActivityCreated(Bundle savedState) {
super.onActivityCreated(savedState);

      ****What do I do/put here to get the above list to show in this list fragment???

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏雨凉 2024-12-30 05:26:53

尝试这样的事情:

   public class QueryDisplayFRAGMENT extends ListFragment {
    boolean mDualPane;
    int mCurCheckPosition = 0;
    protected TextView activityTitle;

    boolean mExternalStorageAvailable = false;
    boolean mExternalStorageWriteable = false;

    String extStorageDirectory = Environment.getExternalStorageDirectory()
        .toString();
    File dbfile = new File(extStorageDirectory
        + "/myco/myapp/dB/myapp.db");
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);

    private static final String QUERY_KEY = "QUERY_KEY";
    private static final String QUERY_ORDER = "QUERY_ORDER";

    private View layout;

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            layout = inflater.inflate(R.layout.list_view2, null);
            return layout;

        }

    @Override
    public void onActivityCreated(Bundle savedState) {

        /**
     * Populate the ActionBar----------------------------------
     */
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    getActionBar().setBackgroundDrawable(
        getResources().getDrawable(R.drawable.actionbar_bg));
    String sT = getIntent().getStringExtra("KEY_SUBTITLE");
    actionBar.setSubtitle(sT);

    /**
     * Get the query string from last activity and pass it to this
     * activity-----------------------------------------------------
     */
    String q = null;
    if (extras != null) {
        q = extras.getString(QUERY_KEY);
    }
    loadQuery(q);

    super.onActivityCreated(savedState);
    }

public void loadQuery(String q) {

if (Environment.getExternalStorageState().equals(
    Environment.MEDIA_MOUNTED)) {

    String qO = getIntent().getStringExtra("QUERY_ORDER");

    Cursor c = db.rawQuery(q + " ORDER BY `_id` " + qO, null);
    setListAdapter(new QueryAdapter(getActivity(), c));
    db.close();

} else {
    Alerts.sdCardMissing(getActivity());
}
}
    }

如果您有问题,请告诉我。

Try something like this:

   public class QueryDisplayFRAGMENT extends ListFragment {
    boolean mDualPane;
    int mCurCheckPosition = 0;
    protected TextView activityTitle;

    boolean mExternalStorageAvailable = false;
    boolean mExternalStorageWriteable = false;

    String extStorageDirectory = Environment.getExternalStorageDirectory()
        .toString();
    File dbfile = new File(extStorageDirectory
        + "/myco/myapp/dB/myapp.db");
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);

    private static final String QUERY_KEY = "QUERY_KEY";
    private static final String QUERY_ORDER = "QUERY_ORDER";

    private View layout;

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            layout = inflater.inflate(R.layout.list_view2, null);
            return layout;

        }

    @Override
    public void onActivityCreated(Bundle savedState) {

        /**
     * Populate the ActionBar----------------------------------
     */
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    getActionBar().setBackgroundDrawable(
        getResources().getDrawable(R.drawable.actionbar_bg));
    String sT = getIntent().getStringExtra("KEY_SUBTITLE");
    actionBar.setSubtitle(sT);

    /**
     * Get the query string from last activity and pass it to this
     * activity-----------------------------------------------------
     */
    String q = null;
    if (extras != null) {
        q = extras.getString(QUERY_KEY);
    }
    loadQuery(q);

    super.onActivityCreated(savedState);
    }

public void loadQuery(String q) {

if (Environment.getExternalStorageState().equals(
    Environment.MEDIA_MOUNTED)) {

    String qO = getIntent().getStringExtra("QUERY_ORDER");

    Cursor c = db.rawQuery(q + " ORDER BY `_id` " + qO, null);
    setListAdapter(new QueryAdapter(getActivity(), c));
    db.close();

} else {
    Alerts.sdCardMissing(getActivity());
}
}
    }

If you have problems tell me about it.

未蓝澄海的烟 2024-12-30 05:26:53

我在这里遇到了类似的问题 setCacheColorHint on listfragments ,我解决了这个问题,让片段膨胀自定义布局使用自己制作的...

如果您自己膨胀布局,则可以访问视图。希望这有帮助,

费德里科

I had a similar problem here setCacheColorHint on listfragments , and I solved it letting the fragment inflate a custom layout instead of using its own made...

If you inflate the layout by yourself, you can then access to the view. Hope this helps,

Federico

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文