简体   繁体   中英

android: How to opem item from list view in new activity?

here is the code, thats handele the list view ! thanks! **i want to get info from the row in the list and open it in new activity
that code handele the listView and there is xml code that hold the single item view

public class HelpFriends extends Activity
{
    ListView dilemasListView;
    MyCustomListAdapter customListAdapter;
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.help_friends); 

        dilemasListView = (ListView) findViewById(R.id.dilemasListView);

        HMCApplication myApp = (HMCApplication)getApplication();
        myApp.getAllDilemasFromServer();
        ArrayList<Dilema> myDilemas = myApp.getDilemas();

        //dilemasListView.setOnItemClickListener(this);


        customListAdapter = new MyCustomListAdapter(this,
                                R.layout.dilema_list_layout,        // layout of a single item in the list
                                myDilemas);
        dilemasListView.setAdapter(customListAdapter);
    }

    // the adapter handles the list
    public class MyCustomListAdapter extends ArrayAdapter<Dilema> 
    {
        // internal list of data 
        ArrayList<Dilema> mItems;

        public MyCustomListAdapter(HelpFriends theActivity,
                int viewResourceId, ArrayList<Dilema> objects)
        {
            super((Context) theActivity, viewResourceId, objects);
            mItems = objects;
        }


         @Override         
         public int getCount()
         {            
             return mItems.size(); 
        }       
         @Override        
         public Dilema getItem(int position)
         {             
             return mItems.get(position);
        }         
         @Override       
         public int getPosition(Dilema item)
         {           
             return mItems.indexOf(item);  
         }  

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

         // getView returns / creates the view for a SINGLE item in the list
        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {

            // theUser will be the logic object with all data
            Dilema theDilema = mItems.get(position);

            // row will be the visual object
            LayoutInflater inflater = getLayoutInflater();
            // single_fruit_item_layout is the layout of a single row
            View row = inflater.inflate(R.layout.dilema_list_layout, parent, false);


            // populate the row object from theUser object
            TextView TextVieUsername = (TextView) row.findViewById(R.id.usernameText);
            TextVieUsername.setText(theDilema.getUsername());   

            TextView TextViewQuestion = (TextView) row.findViewById(R.id.questionText);
            TextViewQuestion.setText(theDilema.getQuestion());  

            TextView TextViewTime = (TextView) row.findViewById(R.id.timeText);
            TextViewTime.setText(theDilema.getTime());

            TextView TextViewCategory = (TextView) row.findViewById(R.id.categoryText);
            TextViewCategory.setText(theDilema.getCategory());

            return row;
        }
    }



}

add this to your on create....

dilemasListView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {


                Intent myIntent = new Intent(activityname.this, nextactivity.class);
                startActivityForResult(myIntent, 0);
            }
        }
    });

Use setOnItemClickListener. inside of this onitemclick you can write code for new activity call.

 dilemasListView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
Intent i = new Intent();
i.setclass(current_activity_name.this, next_activity.class);
startActivity(i);
}
}

your info of the selected item is in the view Class in the listener:

onItemClick(AdapterView<?> parent, View view,int position, long id) 

you can use the view to get the values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM