【问题标题】:Implement onListItemClick in ListFragment在 ListFragment 中实现 onListItemClick
【发布时间】:2016-01-10 19:03:59
【问题描述】:

我正在尝试一个赞美诗应用程序,并且我使用了 SQLiteAssetHelper 和 SimpleCursorAdapter。我已经能够使用 ListFragement 在 ListView 中加载赞美诗的标题。我坚持在 ListFragment 中实现 onListItemClick。有人可以帮我吗?谢谢。以下是我的代码;

public class HymnsFragment extends ListFragment {

private Cursor hymns;
private DataBase db;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    db = new DataBase(getContext());
    hymns = db.getHymns();// you would not typically call this on the main thread

    ListAdapter adapter = new SimpleCursorAdapter(getContext(),
            android.R.layout.simple_list_item_2,
            hymns,
            new String[] {"_id","title"}, //table values
            new int[] {android.R.id.text1,android.R.id.text2});

    setListAdapter(adapter);
}

@Override
public void onDestroy() {
    super.onDestroy();
    hymns.close();
    db.close();
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    // WHAT TO DO HERE TO GET VALUE FROM DATABASE TO DISPLAY IN TEXTVIEW?
}
} 

还有我的数据库;

public class DataBase extends SQLiteAssetHelper {

private static final String DATABASE_NAME = "mydatabase";
private static final int DATABASE_VERSION = 1;

public DataBase(Context context) {

    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public Cursor getHymns() {

    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    String [] sqlSelect = {"0 _id","_id","title"};
    String sqlTables = "hymns";

    qb.setTables(sqlTables);
    Cursor c = qb.query(db, sqlSelect, null, null,
            null, null, null);

    c.moveToFirst();
    return c;

}
}

感谢您的帮助

【问题讨论】:

    标签: java android textview android-listfragment simplecursoradapter


    【解决方案1】:

    首先,您需要获取所选项目,然后通过Cursor 对象检索其数据。以下内容应该得到预期的行为:

    @Override
    public void onListItemClick(ListView listview, View view, int position, long id) {
        super.onListItemClick(listview, view, position, id);
    
        // get the item selected directly here
        Cursor c = (Cursor) listview.getAdapter().getItem(position);
    
        // get specific hymn text from database item result
        String hymnText = c.getString(c.getColumnIndex("hymn_text"));
        // or whatever you want to retrieve from database: authors, songs...
        String hymnAuthor = c.getString(c.getColumnIndex("hymn_author"));
    
        // do some stuff with the previous values as updating a TextView...
    }
    

    无需再次请求,just use the existing Cursor

    【讨论】:

    • @Fllo 很抱歉再次打扰您,但我的TextView 似乎不起作用。这就是我所做的TextView textView = (TextView) findViewById(R.id.textView1); textView.setText(hymnText); return; 正确的做法是什么?谢谢!
    • 你在哪里调用文本视图?如果它在 listfragment 中,则必须附加视图(布局)才能找到其 id 为getView().findViewById(...)。然后,在setText() 方法上方以Log.v("", "HymnText is "+hymnText); 的形式发布一个logcat,以查看在控制台中返回hymnText 的内容。 return的目的是什么?
    • @Fllo 我在DataBase.java 中将列名添加到String [] sqlSelect = {"0 _id","_id","title"};,因此它不再给我column -1 异常。这就是现在所说的java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference。请帮我看看。谢谢!
    • 我可以看到@HazardPJ,您的文本视图未附加到 setContentView 中调用的布局。您应该在 hymn_text.xml 中的 xml 中创建一个 TextView 并使用 id 标签。然后,而不是创建一个新的 textview 通过 findViewById 检索它,你不会有一个 nullpointerexception 。
    • this thread 对您有帮助吗?这个你有the official documentation
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多