我如何得到我的ListView类从SQLite的列表片段

我如何得到我的ListView类从SQLite的列表片段

本文介绍了我如何得到我的ListView类从SQLite的列表片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经经历几个教程上了碎片,我不能让我的查询要在列表片段显示。

下面是我需要在一个片段活动转化为利用原有的类。我的 QueryDisplay 类中有这么多,我不知道该怎么从中取出并把它放在哪里得到它的工作。主要是,我不知道该怎么称呼我的ListView类在下面的片段活动。

感谢,您的您的帮助提前。 code将是非常有益太!

下面是查询SQLite数据库和ID片断类ListActivity:

 公共类QueryDisplay扩展ListActivity {保护的TextView activityTitle;布尔mExternalStorageAvailable = FALSE;
布尔mExternalStorageWriteable = FALSE;字符串extStorageDirectory = Environment.getExternalStorageDirectory()
    的ToString();
文件DBFILE =新的文件(extStorageDirectory
    +/myco/myapp/dB/myapp.db);
SQLiteDatabase DB = SQLiteDatabase.openOrCreateDatabase(DBFILE,NULL);私有静态最后弦乐QUERY_KEY =QUERY_KEY;
私有静态最后弦乐QUERY_ORDER =QUERY_ORDER;//私有静态最后弦乐NI =NI;/ **
 * - 当第一次创建活动调用
 * ================================================= ==================
 * /
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);捆绑额外= getIntent()getExtras()。
的setContentView(R.layout.list_view2);/ **
 *填充动作条----------------------------------
 * /
动作条动作条= getActionBar();
actionBar.setDisplayHomeAsUpEnabled(真);
getActionBar()。setBackgroundDrawable(
    。getResources()getDrawable(R.drawable.actionbar_bg));
串ST = getIntent()getStringExtra(KEY_SUBTITLE);
actionBar.setSubtitle(ST);/ **
 *从上次活动的查询字符串,并将其传递给该
 *活动------------------------------------------------ -----
 * /
串Q = NULL;
如果(临时演员!= NULL){
    Q = extras.getString(QUERY_KEY);
}
loadquery子(Q);
}/ **
 * - DB查询STUFF
 * ================================================= ==============>
 *
 *从previous活动运行初始查询---------------
 * /
公共无效loadquery子(串Q){如果(Environment.getExternalStorageState()。等于(
    Environment.MEDIA_MOUNTED)){    串QO = getIntent()getStringExtra(QUERY_ORDER);    光标C = db.rawQuery(Q +ORDER BY`_id`+ QO,NULL);
    setListAdapter(新QueryAdapter(这一点,C));
    db.close();}其他{
    Alerts.sdCardMissing(本);
}
}/ **
 *查询适配器--------------------------------------------
 * /私有类QueryAdapter扩展的CursorAdapter {公共QueryAdapter(上下文的背景下,光标C){
    超(背景下,C);
    LayoutInflater.from(上下文);
}@覆盖
公共无效bindView(视图V,上下文的背景下,最终的光标C){    INT tvLabel = c.getColumnIndexOrThrow(标签);
    字符串标签= c.getString(tvLabel);
    最终的TextView labelTxt =(TextView中)v.findViewById(R.id.label);    如果(labelTxt!= NULL){
    labelTxt.setText((+标签+));
    }    INT tvTitle = c.getColumnIndexOrThrow(标题);
    最后弦乐标题= c.getString(tvTitle);
    TextView的titleTxt =(TextView中)v.findViewById(R.id.listTitle);    如果(titleTxt!= NULL){
    titleTxt.setText(职称);
    }    INT tvDescription = c.getColumnIndexOrThrow(说明);
    字符串描述= c.getString(tvDescription);
    TextView的descriptionTxt =(TextView中)v.findViewById(R.id.caption);    如果(descriptionTxt!= NULL){
    descriptionTxt.setText(介绍);
    }    INT tvDate = c.getColumnIndexOrThrow(日期);
    字符串日期= c.getString(tvDate);
    TextView的dateTxt =(TextView中)v.findViewById(R.id.dateAdded);    如果(dateTxt!= NULL){
    dateTxt.setText(日期);
    }    INT tvGoto = c.getColumnIndexOrThrow(gotoURL);
    最后弦乐gotoURL = c.getString(tvGoto);
    TextView的gotoTxt =(TextView中)v.findViewById(R.id.dummy);    如果(gotoTxt!= NULL){
    gotoTxt.setText(gotoURL);
    }    gotoTxt.setVisibility(View.GONE);
    v.setTag(gotoURL);    最终的ListView LV = getListView();
    lv.setEnabled(真);
    lv.setClickable(真);    lv.setOnItemClickListener(新OnItemClickListener(){    @覆盖
    公共无效onItemClick(适配器视图<>为arg0,视图V,INT ARG2,
        长ARG3){        // - 在strings.xml档案设置域名一旦
        // DNS,建立了网站。
        。字符串mDomain = getResources()的getString(R.string.domain);        字符串URL =;
        URL = mDomain +(字符串)v.getTag();        INT NI = c.getColumnIndexOrThrow(意图);
        字符串意图= c.getString(NI);
        类<> nIntent = NULL;
        尝试{
        nIntent =类
            .forName(com.myco.myapp.utili。
                +意向);
        }赶上(ClassNotFoundException的E){
        // TODO自动生成catch块
        e.printStackTrace();
        }        INT tvTitle = c.getColumnIndexOrThrow(标题);
        字符串title = c.getString(tvTitle);        INT tvLabel = c.getColumnIndexOrThrow(标签);
        字符串标签= c.getString(tvLabel);        字符串queryKey =SELECT * FROM+标签;
        c.close();
        db.close();        意图I =新意图(QueryDisplay.this,nIntent);
        i.putExtra(QUERY_KEY,queryKey);
        i.putExtra(KEY_URL,URL);        Log.e(标签,网址);        i.putExtra(KEY_SUBTITLE称号);
        i.putExtra(KEY_LABEL标签);
        i.putExtra(KEY_INTENT,意图);
        i.putExtra(QUERY_ORDER,ASC);
        i.putExtra(KEY_YPOS,0.0);
        QueryDisplay.this.startActivity(ⅰ);
    }
    });
}/ **
 *充气并返回查询到的视图
 * --------------------------------
 * /
@覆盖
公共查看NewView的(上下文的背景下,光标C,父母的ViewGroup){
    最后的视图V = LayoutInflater.from(上下文).inflate(
        R.layout.list_item,父母,假);
    返回伏;
}
}
}

QueryDisplayFRAGMENT 类:

 公共类QueryDisplayFRAGMENT扩展ListFragment {
布尔mDualPane;
INT mCurCheckPosition = 0;@覆盖
公共无效onActivityCreated(捆绑savedState){
super.onActivityCreated(savedState);      ****我该怎么办/把这里得到上面的列表在此列表中的片段展示?}
}


解决方案

尝试是这样的:

 公共类QueryDisplayFRAGMENT扩展ListFragment {
    布尔mDualPane;
    INT mCurCheckPosition = 0;
    保护的TextView activityTitle;    布尔mExternalStorageAvailable = FALSE;
    布尔mExternalStorageWriteable = FALSE;    字符串extStorageDirectory = Environment.getExternalStorageDirectory()
        的ToString();
    文件DBFILE =新的文件(extStorageDirectory
        +/myco/myapp/dB/myapp.db);
    SQLiteDatabase DB = SQLiteDatabase.openOrCreateDatabase(DBFILE,NULL);    私有静态最后弦乐QUERY_KEY =QUERY_KEY;
    私有静态最后弦乐QUERY_ORDER =QUERY_ORDER;    私人视图布局;    @覆盖
        公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                捆绑savedInstanceState){
            布局= inflater.inflate(R.layout.list_view2,NULL);
            返回布局;        }    @覆盖
    公共无效onActivityCreated(捆绑savedState){        / **
     *填充动作条----------------------------------
     * /
    动作条动作条= getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(真);
    getActionBar()。setBackgroundDrawable(
        。getResources()getDrawable(R.drawable.actionbar_bg));
    串ST = getIntent()getStringExtra(KEY_SUBTITLE);
    actionBar.setSubtitle(ST);    / **
     *从上次活动的查询字符串,并将其传递给该
     *活动------------------------------------------------ -----
     * /
    串Q = NULL;
    如果(临时演员!= NULL){
        Q = extras.getString(QUERY_KEY);
    }
    loadquery子(Q);    super.onActivityCreated(savedState);
    }公共无效loadquery子(串Q){如果(Environment.getExternalStorageState()。等于(
    Environment.MEDIA_MOUNTED)){    串QO = getIntent()getStringExtra(QUERY_ORDER);    光标C = db.rawQuery(Q +ORDER BY`_id`+ QO,NULL);
    setListAdapter(新QueryAdapter(getActivity(),C));
    db.close();}其他{
    Alerts.sdCardMissing(getActivity());
}
}
    }

如果您有问题,给我讲讲吧。

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???

}
}
解决方案

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.

这篇关于我如何得到我的ListView类从SQLite的列表片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:24