Android ListView SimpleCursorAdapter TextView 和 ImageButton
创始人
2024-08-14 16:30:20
0

下面是一个示例代码,演示了如何在Android ListView中使用SimpleCursorAdapter来显示TextView和ImageButton。

首先,我们需要在XML布局文件中定义ListView和一个自定义的item布局。item布局包含一个TextView和一个ImageButton,用于显示数据和执行某些操作。

activity_main.xml:



    


list_item.xml:



    

    


然后,在MainActivity.java中,我们使用SimpleCursorAdapter来绑定数据到ListView。在这个示例中,我们使用一个简单的SQLite数据库,其中包含一个名为"items"的表,该表有两列:"_id"和"name"。

MainActivity.java:

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private SimpleCursorAdapter adapter;
    private SQLiteDatabase database;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.listView);

        // 创建数据库和表
        database = openOrCreateDatabase("myDB", MODE_PRIVATE, null);
        database.execSQL("CREATE TABLE IF NOT EXISTS items (_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR);");

        // 插入一些示例数据
        database.execSQL("INSERT INTO items (name) VALUES ('Item 1');");
        database.execSQL("INSERT INTO items (name) VALUES ('Item 2');");
        database.execSQL("INSERT INTO items (name) VALUES ('Item 3');");

        // 查询数据
        Cursor cursor = database.rawQuery("SELECT * FROM items;", null);

        // 设置SimpleCursorAdapter
        String[] fromColumns = {"name"};
        int[] toViews = {R.id.textView};
        adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, fromColumns, toViews, 0);

        // 设置ListView的适配器
        listView.setAdapter(adapter);

        // 设置ImageButton的点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                // 获取被点击的Item的TextView和ImageButton
                TextView textView = view.findViewById(R.id.textView);
                ImageButton imageButton = view.findViewById(R.id.imageButton);

                // 执行某些操作,比如删除数据
                String itemName = textView.getText().toString();
                database.execSQL("DELETE FROM items WHERE name = '" + itemName + "';");

                // 更新ListView
                Cursor newCursor = database.rawQuery("SELECT * FROM items;", null);
                adapter.changeCursor(newCursor);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 关闭数据库
        database.close();
    }
}

这个示例演示了如何使用SimpleCursorAdapter来将数据绑定到ListView,并且在点击ImageButton时执行某些操作,比如删除数据。

相关内容

热门资讯

wepoker怎么设置盖牌!w... wepoker怎么设置盖牌!we-poker辅助器(透视)脚本-一贯总结真的有挂进入游戏-大厅左侧-...
wepoker底牌透视!wpk... wepoker底牌透视!wpk辅助(透视)神器-竟然曝光有挂1、任何wepoker底牌透视透视是真的...
hhpoker辅助软件!wej... hhpoker辅助软件!wejoker辅助器要钱玩吗(透视)教程-原来曝光真的是有挂一、wejoke...
德普之星辅助软件!wpk透视辅... 德普之星辅助软件!wpk透视辅助靠谱吗(透视)软件-果然总结真的有挂1、点击下载安装,德普之星辅助软...
德州机器人代打脚本!wepok... 德州机器人代打脚本!wepoker好友助力码(透视)攻略-原来科普真的有挂德州机器人代打脚本!wep...
hhpoker破解工具!aap... 您好,aapoker安装包怎么使用这款游戏可以开挂的,确实是有挂的,需要了解加去威信【1367043...
wepoker模拟器哪个!we... wepoker模拟器哪个!wepoker辅助器下载(透视)脚本-确实揭幕真的是有挂1、玩家可以在we...
wepoker私局代打!拱趴大... wepoker私局代打!拱趴大菠萝万能挂(透视)工具-好像推荐真的有挂亲,关键说明,拱趴大菠萝万能挂...
wpk透视工作室!pokemm... wpk透视工作室!pokemmo脚本(透视)技巧-切实解密真的是有挂该软件可以轻松地帮助玩家将wpk...
wepoker有脚本吗!pok... wepoker有脚本吗!pokemomo辅助工具(透视)神器-都是有挂是有挂;1、完成pokemom...