You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android MySQL数据库-ListView点击跳转Activity实现方案咨询

没问题,我来一步步教你实现这个需求,其实核心就是把点击项对应的商户数据完整传递过去,具体步骤如下:

1. 先创建商户数据模型类

首先得把从MySQL拉取的商户信息封装成一个实体类,这样不管是绑定ListView还是传递数据都更方便。记得要让这个类实现Serializable接口,这样才能通过Intent完整传递对象:

public class Merchant implements Serializable {
    private String merchantId;
    private String merchantName;

    // 构造方法,用来初始化商户数据
    public Merchant(String merchantId, String merchantName) {
        this.merchantId = merchantId;
        this.merchantName = merchantName;
    }

    // Getter方法,用于获取属性值
    public String getMerchantId() {
        return merchantId;
    }

    public String getMerchantName() {
        return merchantName;
    }
}
2. 给ListView绑定自定义适配器

假设你已经从MySQL获取到了商户列表,存在List<Merchant> merchantList里,接下来我们写一个自定义适配器把数据绑定到ListView上:

自定义适配器代码

public class MerchantAdapter extends BaseAdapter {
    private Context mContext;
    private List<Merchant> mMerchantList;

    public MerchantAdapter(Context context, List<Merchant> merchantList) {
        this.mContext = context;
        this.mMerchantList = merchantList;
    }

    @Override
    public int getCount() {
        return mMerchantList.size();
    }

    @Override
    public Object getItem(int position) {
        // 返回对应位置的商户对象,这一步很关键
        return mMerchantList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        // 复用布局优化性能
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_merchant, parent, false);
            holder = new ViewHolder();
            holder.tvMerchantName = convertView.findViewById(R.id.tv_merchant_name);
            holder.tvMerchantId = convertView.findViewById(R.id.tv_merchant_id);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        // 获取当前位置的商户数据,设置到控件上
        Merchant merchant = mMerchantList.get(position);
        holder.tvMerchantName.setText(merchant.getMerchantName());
        holder.tvMerchantId.setText(merchant.getMerchantId());

        return convertView;
    }

    // ViewHolder内部类,避免重复findViewById
    static class ViewHolder {
        TextView tvMerchantName;
        TextView tvMerchantId;
    }
}

这里的R.layout.item_merchant是你自己定义的列表项布局,里面需要两个TextView分别显示商户名称和ID。

在主Activity中设置适配器

ListView merchantListView = findViewById(R.id.list_view);
// 这里替换成你从MySQL获取的真实商户列表
List<Merchant> merchantList = loadMerchantsFromMySQL(); 
MerchantAdapter adapter = new MerchantAdapter(this, merchantList);
merchantListView.setAdapter(adapter);
3. 设置ListView点击事件,传递数据到目标Activity

现在给ListView添加点击监听器,点击时直接拿到对应位置的Merchant对象,通过Intent传递给目标Activity:

merchantListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // 获取点击项对应的商户对象
        Merchant clickedMerchant = (Merchant) parent.getItemAtPosition(position);

        // 创建跳转Intent
        Intent intent = new Intent(CurrentActivity.this, MerchantDetailActivity.class);
        // 把商户对象放入Intent,key可以自己定义,比如"MERCHANT_INFO"
        intent.putExtra("MERCHANT_INFO", clickedMerchant);

        // 启动目标Activity
        startActivity(intent);
    }
});

把CurrentActivity换成你当前的Activity类名,MerchantDetailActivity是你要跳转的目标Activity。

4. 目标Activity接收并展示数据

最后在目标Activity中取出传递的Merchant对象,把数据展示到对应控件上:

public class MerchantDetailActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_merchant_detail);

        // 获取Intent中的数据
        Intent intent = getIntent();
        if (intent != null) {
            Merchant merchant = (Merchant) intent.getSerializableExtra("MERCHANT_INFO");
            if (merchant != null) {
                // 找到布局中的展示控件,设置数据
                TextView tvDetailName = findViewById(R.id.tv_detail_name);
                TextView tvDetailId = findViewById(R.id.tv_detail_id);

                tvDetailName.setText("商户名称:" + merchant.getMerchantName());
                tvDetailId.setText("商户ID:" + merchant.getMerchantId());
            }
        }
    }
}

别忘了在AndroidManifest.xml中注册你的MerchantDetailActivity,否则跳转时会报错。

额外小提示
  • 如果你追求更高的传递效率,可以把Serializable换成Parcelable接口,不过对于这种简单的数据模型,Serializable已经足够好用。
  • 确保从MySQL获取数据时,每条记录都正确封装成Merchant对象存入List中,这样适配器才能正确绑定数据。

内容的提问来源于stack exchange,提问作者Jing Shan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:06:04