How to acces variable in arrayadapter getView from onItemClick

与世无争的帅哥 提交于 2020-01-05 02:53:09

问题


I have an ArrayAdapter whose getView sets the items in the listview with data. When i click a row in the listview i'd like to start another activity, (which i can do), but send that activity some of the data to present in another view.

How can i make the data from getView accessible in the onItemClick method?

@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);

    }

回答1:


By Using setTag() and getTag() methods you can get data from the adapter in onItemClick(). like this

in adapter class

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

    rowView.setTag(yourdata_object);
    return rowView;
}

in on item click

@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);
}



回答2:


Try this.

You will get position of clicked item in the onItemClick method and a view which is clicked in list. So you can set the tag to view in getView and in onitemClick you can get that view tag and pass to intent.

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);

    }


来源:https://stackoverflow.com/questions/12619022/how-to-acces-variable-in-arrayadapter-getview-from-onitemclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!