2

我有一个 ArrayAdapter,它的 getView 用数据设置列表视图中的项目。当我单击列表视图中的一行时,我想开始另一个活动(我可以这样做),但向该活动发送一些数据以呈现在另一个视图中。

如何在 onItemClick 方法中使 getView 中的数据可访问?

@Override
        public View getView(int position, View convertView, ViewGroup parent) {


            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View rowView = inflater.inflate(R.layout.rotarowlayout, parent,
                    false);

            TextView startTime = (TextView) rowView
                    .findViewById(R.id.rowstarttime);
            TextView duration = (TextView) rowView
                    .findViewById(R.id.rowduration);
            TextView status = (TextView) rowView.findViewById(R.id.rowstatus);
            TextView name = (TextView) rowView.findViewById(R.id.rowclientname);
            TextView actualTimeIn = (TextView) rowView.findViewById(R.id.rowactualtimein);
            TextView actualTimeOut = (TextView) rowView.findViewById(R.id.rowactualtimeout);

            String record = list.get(position).toString();
            String[] itemsInRecord = record.split(",");
            Log.e(TAG, "itemin record = " + itemsInRecord.length);
            String[] recordItem = new String[itemsInRecord.length];

            for (int x = 0; x < itemsInRecord.length; x++) {

                recordItem[x] = itemsInRecord[x];

            }

            //set date above listview

            textViewDate.setText("Rota For " + recordItem[0].substring(1, 11));


            String withoutBraket = recordItem[0].substring(11);
            String withoutSecs = withoutBraket.substring(0, 6);

            Log.e(TAG, "recordItem = " + recordItem[2]);

            String _statusString = recordItem[2].toString().trim();

            if(_statusString.equalsIgnoreCase("Completed")){

                statusField = "c";

            }else if(_statusString.equalsIgnoreCase("NCR")){

                statusField = "NCR";

            }else if(_statusString.equalsIgnoreCase("Waiting")){

                statusField = "W";
            }

            Log.e(TAG, "statusField = " + statusField);

            startTime.setText(withoutSecs+"     " );
            duration.setText( recordItem[1]+" mins     ");
            status.setText( statusField);
            name.setText( "     " + recordItem[3] + recordItem[4]);
            callID = recordItem[5];
        //  needName = recordItem[6];
            actualTimeIn.setText("This call was completed at " + recordItem[6] + " - ");

            if( ! recordItem[7].toString().trim().equalsIgnoreCase(  "]")){


            String stripBracket = recordItem[7].toString().trim();
            Log.e(TAG, "stripBracket = " + stripBracket);
            String stripBracket2 = stripBracket.substring(0,5);
            Log.e(TAG, "SB after split = " + stripBracket2 );
            actualTimeOut.setText(stripBracket2);
            }

            return rowView;

        }

.

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        Log.e(TAG, "inside onItemClick");
Intent i = new Intent(GetRota.this, someOtherActivity.class);
    i.putsomedata;
    startActivity(i);

    }
4

2 回答 2

3

通过 UsingsetTag()getTag()方法,您可以从onItemClick(). 像这样

在适配器类中

@Override
public View getView(int position, View convertView, ViewGroup parent) {           

    rowView.setTag(yourdata_object);
    return rowView;
}

在项目点击

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

   YourDataObject item = (YourDataObject ) view.getTag();
   Intent i = new Intent(GetRota.this, someOtherActivity.class);
   i.putsomedata;
   startActivity(i);
}
于 2012-09-27T10:23:11.103 回答
0

试试这个。

您将在 onItemClick 方法中获得单击项目的位置以及在列表中单击的视图。因此,您可以将标签设置为在 getView 和 onitemClick 中查看,您可以获得该视图标签并传递给意图。

rowView.setTag(YOURDATA);  // in getView

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        Log.e(TAG, "inside onItemClick");
       Intent i = new Intent(GetRota.this, someOtherActivity.class);
       (yourData) = view.getTag();   // get the tag value and set to putextra
       i.putsomedata;
       startActivity(i);

    }
于 2012-09-27T10:23:25.707 回答