【问题标题】:How to show data from database when you click item in a listview单击列表视图中的项目时如何显示数据库中的数据
【发布时间】:2017-12-07 18:00:07
【问题描述】:

当我单击列表视图中的项目时尝试访问数据库中的数据时遇到问题。

这是我在数据库处理程序中的方法:

    public Match getMatchFromId(int matchId)
    {
        SQLiteDatabase db = this.getReadableDatabase();
        Match m = null;
        Cursor cursor;

        cursor = db.rawQuery("select * from " + MATCH_TABLE + " where " +
        MATCH_ID_COL + "='" + matchId + "'" , null);

        while(cursor.moveToNext())
        {
            m = new Match();
            m.setHome(cursor.getString(cursor.getColumnIndex(MATCH_HOME_TEAM_COL)));
            m.setAway(cursor.getString(cursor.getColumnIndex(MATCH_AWAY_TEAM_COL)));
            m.setHomeScore(cursor.getString(cursor.getColumnIndex(MATCH_HOMESCORE_COL)));
            m.setAwayScore(cursor.getString(cursor.getColumnIndex(MATCH_AWAYSCORE_COL)));
            m.setDate(cursor.getString(cursor.getColumnIndex(MATCH_DATE_COL)));
            m.setTime(cursor.getString(cursor.getColumnIndex(MATCH_TIME_COL)));
            m.setRedCard(cursor.getString(cursor.getColumnIndex(MATCH_REDCARD_COL)));
            m.setBookings(cursor.getString(cursor.getColumnIndex(MATCH_BOOKINGS_COL)));
            m.setTypeOfMatch(cursor.getString(cursor.getColumnIndex(MATCH_TYPEOFMATCH_COL)));
//            m.setGroundId(cursor.getInt(cursor.getColumnIndex()));
        }
        cursor.close();

        return m;
    }

我只是在如何通过列表视图访问数据方面遇到问题。我希望能够单击该项目,并将其带到另一个活动中进行编辑。

public class ViewMatchesActivity extends AppCompatActivity {
DBHandler myDB;
ArrayList<Match> matches = new ArrayList<Match>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_matches);
    myDB = new DBHandler(this);
    final ListView listView = (ListView) findViewById(R.id.listview);

    matches = myDB.getAllMatches();
    MyAdapter myAdapter = new MyAdapter(ViewMatchesActivity.this, R.layout.list2col, matches);

    listView.setAdapter(myAdapter);

任何帮助都会很好,因为我是 Android Studio 的新手。

【问题讨论】:

    标签: android sqlite listview


    【解决方案1】:

    您需要添加一个侦听器来侦听相应的事件(可能是项目单击)。然后提取id(或任何可用于唯一标识表id或rowid(通常相同的东西)经常使用的行),在创建Intent后将该数据放入Intent Extra,然后调用其他活动使用 Intent。

    您可以按照以下方式编写一些代码:-

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //>>>> position is the position of the clicked item, 0 for the first item.
                //>>>> position will also be the respective offset into matches
                //>>>> position SHOULD NOT BE USED to try to perform arithmetic
                //>>>> calculation of rowid/id of the row in the table
                //>>>> So get rowid via matches.get(position).
                Intent intent = new Intent(getApplicationContext(),your_other_activity.class);
                intent.putExtra("KEYFORDATA",row_id_to_so_selected_item_can_be_edited);
                StartActivity(intent);
            }
        });
    

    注意!显然以上内容不会按原样工作,需要对其进行调整以适应。

    【讨论】:

    • 谢谢迈克,我需要多尝试一下,但这很有帮助。我也很感激你的帮助,这个网站上的人太固执了,无法帮助刚刚起步的人。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2016-07-15
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    相关资源
    最近更新 更多