0

我对 Android 编程相当陌生,并且已经完成了我的第一个应用程序,除了一个关于从选定的表行传递数据的问题 (onClick)。我在这里进行了研究并尝试了大部分建议,但我显然遗漏了一些关键的东西。我对 getTag/Id 和 setTag/Id 有点困惑,所以也许这就是我出错的地方。

一点背景知识,该表可以从数据库动态填充 1 到 N 行,每一行都是可点击的。单击一行后,我需要将该行的唯一数据传递给下一个活动(使用 Bundle 可以正常工作)。不起作用的是,无论我点击哪一行,我只会将最后一行的数据传递给下一个活动,而不是点击的行的数据。

这是我的一些相关代码,用于将数据添加到行/表并单击...我做错了什么只能获取最后一行的数据?非常感谢任何人帮助我指出正确的方向。

    public void addData(ArrayList<Auctions> auctions) {

        prevCity = "";
        currCity = "";
        prevDate = "";
        currDate = "";
        prevTime = "";
        currTime = "";
        prevUnit = "";
        currUnit = "";

        for (Iterator i = auctions.iterator(); i.hasNext();) {
            Auctions p = (Auctions) i.next();

            if (p.getCity().equals(prevCity) && p.getDate().equals(prevDate) && p.getTime().equals(prevTime) && p.getSunit().equals(prevUnit)) {
                //skip to next unique record
            } else {

                /** Create a City TableRow dynamically **/
                row2 = new TableRow(this);
                row2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

                /** Creating a TextView to add to the row **/
                tvcity = new TextView(this);
                currCity = p.getCity();
                if (currCity.toString().equals(prevCity)) {
                    tvcity.setText("");
                } else {
                    //create a blank row to separate from previous city entries
                    row1 = new TableRow(this);
                    row1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
                    tvdummy = new TextView(this);
                    tvdummy.setText("");
                    tvdummy.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
                    row1.addView(tvdummy);
                    auctions_table.addView(row1, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                    // work on the new city data
                    tvcity.setText(p.getCity());
                }

                tvcity.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
                tvcity.setTextColor(getResources().getColor(R.color.red));

                //add TextView data to row
                row2.addView(tvcity);

                // Add the TableRow to the TableLayout
                auctions_table.addView(row2, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                // Create date, time, and unit name row dynamically
                row3 = new TableRow(this);
                row3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

                // Create TextViews to add to the Row -- Date and Time
                tvdate = new TextView(this);
                tvtime = new TextView(this);
                tvsunit = new TextView(this);

                currDate = p.getDate();
                currTime = p.getTime();
                currUnit = p.getSunit();

                if ((currDate.toString().equals(prevDate)) && (currTime.toString().equals(prevTime))) {
                    tvdate.setText("");
                    tvtime.setText("");
                } else {
                    tvdate.setText(p.getDate());
                    tvtime.setText(p.getTime());
                }

                if (currUnit.toString().equals(prevUnit)) {
                    tvsunit.setText("");
                } else {
                    tvsunit.setText("Dynamic UNIT");
                    tvsunit.setText(p.getSunit());
                }

                tvdate.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
                tvdate.setTextColor(getResources().getColor(R.color.blue));

                tvtime.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
                tvtime.setTextColor(getResources().getColor(R.color.blue));

                tvsunit.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
                tvsunit.setTextColor(getResources().getColor(R.color.blue));                

                //add TextView data to row
                row3.addView(tvdate);
                row3.addView(tvtime);
                row3.addView(tvsunit);

                row3.setId(t);
                row3.setFocusable(false);
                //row3.setClickable(true);
                row3.setOnClickListener(new View.OnClickListener() {    
                    //public OnClickListener tablerowOnClickListener = new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // Get the selected row's data
                            //row3.getId();
                            Bundle basket2 = new Bundle();
                            basket2.putString("ustate", state.getText().toString());
                            basket2.putString("abbrev", abbrev);
                            basket2.putString("ucity", currCity);
                            basket2.putString("uname", currUnit);
                            basket2.putString("auctiondate", currDate);
                            basket2.putString("auctiontime", currTime);

                            Intent StAuctions = new Intent(getApplicationContext(), StorageAuctionDetails.class);
                            StAuctions.putExtras(basket2);
                            StAuctions.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(StAuctions);
                            // Closing screen
                            //finish();
                        }
                    });  
                row3.setBackgroundResource(drawable.list_selector_background);

                // Add the TableRow to the TableLayout
                auctions_table.addView(row3, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));                
            }

            prevCity = currCity;
            prevDate = currDate;
            prevTime = currTime;
            prevUnit = currUnit;
            t++;
        }
    };
4

2 回答 2

0

无法充分解密您的代码。但我建议的是,在您的onClick(), 中放入Log.v("Row id is: ", Integer.toString(v.getId()))并在 LogCat 中查看是否每次单击时,它都会返回相同的值。

此外,如果我正在编写类似的功能,我可能希望在 中执行行信息的提取onClick(),因此信息会从行中提取并即时放入 bundle 中,而不是在迭代器期间将值传递到 bundle 中。就像是:

public void onClick(View v) {
    String a;    //extras for bundle
    Bundle basket2 = new Bundle();
    //iterate row's children and assign values to string here
    for(int i = 0; i < v.getChildSize(); i++){
        TextView t = (TextView)v.getChildAt(i);
        //assignment
        basket2.putString("someKey", a);
    }
    //send your intent
}
于 2013-04-24T01:13:22.293 回答
0

您将根据激活 onClick 时的变量来获取这些变量:

 basket2.putString("ustate", state.getText().toString());
                            basket2.putString("abbrev", abbrev);
                            basket2.putString("ucity", currCity);
                            basket2.putString("uname", currUnit);
                            basket2.putString("auctiondate", currDate);
                            basket2.putString("auctiontime", currTime);

那些被重视的将是你最后一次通过你的迭代。

您需要为每个行条目创建一个新的“篮子”。不是在点击侦听器中,而是在迭代循环中,并让该篮子成为该行的标签,如下所示:

row3.setTag(basket2);
于 2013-04-24T01:46:39.653 回答