首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Android Studio中通过点击事件传参?

如何在Android Studio中通过点击事件传参?

提问于 2022-04-27 10:14:18
回答 0关注 0查看 78

我想做一个二手交易app,买家在商品详情页点击购买,商品的状态就会变为“待发货”,其他用户的该页面出现“该商品已售出”,卖家的该页面会出现一个发货按钮(本来没有),这个购买按钮的监听事件怎么设置?状态怎么传给卖家?以下是我的代码:

代码语言:javascript
复制
//item_info
public class item_info extends AppCompatActivity {
    String TABLENAME = "iteminfo";
    byte[] imagedata;
    Bitmap imagebm;
    private ImageView image;
    private TextView price;
    private TextView title;
    private TextView info;
    private TextView contact;
    private Intent intent;
    private DatabaseHelper dbtest;
    private SQLiteDatabase db;
    private ListView commentList;
    private Button submit;
    private Button submit1;
    private EditText comment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_info);
        submit1.setOnClickListener(new MyOnClickListener());
        init();

        dbtest = new DatabaseHelper(this);
        intent = getIntent();
        db = dbtest.getWritableDatabase();
        //查询商品表
        Cursor cursor = db.query(TABLENAME, null, "id=?", new String[]{intent.getStringExtra("id")}, null, null, null, null); // 根据接收到的id进行数据库查询
        Log.i("商品的id是", intent.getStringExtra("id"));
        if (cursor.moveToFirst()) {
            while (!cursor.isAfterLast()) {
                imagedata = cursor.getBlob(6);
                imagebm = BitmapFactory.decodeByteArray(imagedata, 0, imagedata.length);
                image.setImageBitmap(imagebm);
                title.setText(cursor.getString(2));
                price.setText(cursor.getString(5));
                info.setText(cursor.getString(4));
                contact.setText(cursor.getString(8));
                cursor.moveToNext();
            }
        }
        Map<String, Object> item;  // 列表项内容用Map存储
        final List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); // 列表
        //查询评论表
        Cursor cursor_ = db.query("comments", null, "itemId=?", new String[]{intent.getStringExtra("id")}, null, null, null, null); // 数据库查询
        if (cursor_.moveToFirst()) {
            while (!cursor_.isAfterLast()) {
                item = new HashMap<String, Object>();  // 为列表项赋值
                item.put("userId", cursor_.getString(0));
                item.put("comment", cursor_.getString(2));
                item.put("time", cursor_.getString(3));
                cursor_.moveToNext();
                data.add(item); // 加入到列表中
            }
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, R.layout.comment_item, new String[]{"userId", "comment", "time"},
                new int[]{R.id.userId, R.id.commentInfo, R.id.time});
        commentList.setAdapter(simpleAdapter);


        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取此商品评论的内容
                String submit_comment = comment.getText().toString();
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss ");
                Date curDate = new Date(System.currentTimeMillis());
                String time = formatter.format(curDate);
                ContentValues values = new ContentValues();
                values.put("userId", post_userid);
                values.put("itemId", intent.getStringExtra("id"));
                values.put("comment", submit_comment);
                values.put("time", time);
                db.insert("comments", null, values);
                Log.i("1", "评论成功");
                Toast.makeText(getApplicationContext(), "评论成功", Toast.LENGTH_SHORT).show();
                intent = new Intent(item_info.this, item_info.class);
                intent.putExtra("id", intent.getStringExtra("id"));
                startActivity(intent);
            }
        });
    }
    //设置购买按钮点击监听器
    private class MyOnClickListener implements View.OnClickListener {
        //按钮点击
        @Override
        public void onClick(View view) {

            if(view.getId()==R.id.submit1){//被点击的是确认按钮
                //显示提示框
                Toast.makeText(item_info.this,"购买成功",Toast.LENGTH_SHORT).show();
                
            }
        }

    }
    private void init() {
        image = findViewById(R.id.imageView);
        price = findViewById(R.id.item_price);
        title = findViewById(R.id.item_title);
        info = findViewById(R.id.item_info);
        contact = findViewById(R.id.contact);
        commentList = findViewById(R.id.commentList);
        submit = findViewById(R.id.submit);
        submit1 = findViewById(R.id.submit1);
        comment = findViewById(R.id.comment);
    }
}


//item_info.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/loginpage"
    android:orientation="vertical"

    tools:context="yixiaoyuan.page1.activity.shopActivity.item_info">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="268dp"
        android:layout_height="156dp"
        android:layout_gravity="center"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="20dp"
            android:paddingTop="10dp"
            android:paddingRight="20dp"
            android:paddingBottom="10dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:text="@string/con_items_name"
                    android:textColor="#000000"
                    android:textSize="25dp"/>

                <TextView
                    android:id="@+id/item_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="100dp"
                    android:gravity="center_vertical"
                    android:textColor="#000000"
                    android:textStyle="bold"
                    android:textSize="25dp"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:text="@string/con_price2"
                    android:textColor="#000000"
                    android:textSize="25dp"/>

                <TextView
                    android:id="@+id/item_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:textColor="#000000"
                    android:textSize="25dp"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:text="@string/cont_description"
                    android:layout_marginTop="15dp"
                    android:textColor="#000000"
                    android:textSize="25dp"/>

                <TextView
                    android:id="@+id/item_info"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:textColor="#000000"
                    android:textSize="25dp"
                    android:layout_marginTop="15dp"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:gravity="center_vertical"
                    android:text="@string/con_tele"
                    android:textColor="#000000"
                    android:textSize="25dp"/>

                <TextView
                    android:id="@+id/contact"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:text="@string/con_telenumber"
                    android:textColor="#000000"
                    android:textSize="20dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:gravity="center_vertical"
                    android:text="状态:"
                    android:textColor="#000000"
                    android:textSize="25dp"/>
                <TextView
                    android:id="@+id/state"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:text="上架"
                    android:textColor="#000000"
                    android:textSize="20dp"/>
            </LinearLayout>
            <Button
                android:id="@+id/submit1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="15dp"
                android:text="购买"
                android:textSize="25dp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:text="@string/con_add_comment"
                android:textColor="#000000"
                android:textSize="25dp" />

            <EditText
                android:id="@+id/comment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#000000" />

            <Button
                android:id="@+id/submit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bnt_submit_comment"
                android:layout_gravity="right"
                android:textSize="20dp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:layout_marginBottom="15dp"
                android:text="@string/con_our_comment"
                android:textColor="#000000"
                android:textSize="25dp" />

            <ListView
                android:id="@+id/commentList"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000000">

            </ListView>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>


//数据库表
//商品编号id,发布者账号userId,标题title,类别kind,内容info,价格price,图片image,状态state
        db.execSQL("create table if not exists iteminfo(" +
                "id integer primary key  AUTOINCREMENT," +
                "userId varchar(100)," +
                "title varchar(200)," +
                "kind varchar(100)," +
                "info varchar(1000)," +
                "price varchar(100)," +
                "image blob," +
                "time DATETIME," +
                "contact varchar(50),"+
                "state varchar(50))");

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档