0

我只是想知道我是否能够通过单击 ListView 中的项目来启动新活动。

到目前为止我写的代码:

package awad865.project.ContactManager1;


import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.example.contactmanager1.R;
import android.widget.AdapterView;

public class MainActivity extends Activity {

    private ListView listView;
    private ImageButton button1;
    private ImageButton button2;
    private ImageButton button3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getActionBar().setDisplayShowTitleEnabled(false);
        getActionBar().setDisplayShowHomeEnabled(false);

        listView = (ListView)findViewById(R.id.main_contact_listview);
        button1= (ImageButton)findViewById(R.id.button_search);
        button2= (ImageButton)findViewById(R.id.button_addcontact);
        button3= (ImageButton)findViewById(R.id.button_options);



        setUpListView();

    }

    private void setUpListView(){
        List <Contact> displayList = new ArrayList<Contact>();
        displayList.add(new Contact("Anmol","Wadhwa","53743632"));
        displayList.add(new Contact("Juhi","Goswami","4234232"));
        displayList.add(new Contact("Laurence","Baldwick","243232"));



        ListAdapter listAdapter = new CustomListAdapter(MainActivity.this,displayList);
        listView.setAdapter(listAdapter);

    }

    private class CustomListAdapter extends ArrayAdapter<Contact>{

        private Context _context;
        private List<Contact> _contacts;

        public CustomListAdapter(Context context, List<Contact> contacts){
            super(context,android.R.layout.simple_list_item_1,contacts);

            _context = context;
            _contacts = contacts;

        }

        public View getView(int position, View convertView,ViewGroup parent){
            //Create a layout inflater to inflate our xml layout for each item in the list
            LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            //Inflate the list item layout. Keep a reference to the inflated view.
            //No root view specified
            View listItemView = inflater.inflate(R.layout.custom_list_item_layout,null);

            //Access TextView elements inside the view (note we must specify the parent view
            //to look in)
            TextView firstName =  (TextView)listItemView.findViewById(R.id.list_item_firstname);
            TextView lastName =  (TextView)listItemView.findViewById(R.id.list_item_lastname);
            TextView number =  (TextView)listItemView.findViewById(R.id.list_item_number);

            //Set the text for each textview (use the position arugment to find the appropriate element in the list)
            firstName.setText(_contacts.get(position).getFirstName());
            lastName.setText(_contacts.get(position).getLastName());
            number.setText(_contacts.get(position).getNumber());

            return listItemView;
        }

    }

    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
        case R.id.action_groups:
            Intent groupIntent = new Intent(this,Groups.class);
            startActivity(groupIntent);
            return true;

        case R.id.action_favourites:
            Intent favouriteIntent = new Intent(this,Favourites.class);
            startActivity(favouriteIntent);
        default:
            return super.onOptionsItemSelected(item);

        }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_activity_actions, menu);
        return true;
    }



    public void addContact(View view){
        Intent intent = new Intent(this,AddContact.class);
        startActivity(intent);

    }
}



class listItemClickedListener implements AdapterView.OnItemClickListener{

    @Override
    public void onItemClick(AdapterView<?> parentView, View clickedView, int clickedViewPosition, long id) {
        // TODO Auto-generated method stub
        Intent contactInfoIntent = new Intent(this, ContactInformation.class);

    }
}

我只是想知道我是否能够在 listItemClickListener 类的 onItemClick() 方法中启动一个新活动。任何帮助将不胜感激,因为编译器不允许我开始新的意图。

4

5 回答 5

1

您可以从中检索Context您需要的内容clickedView

Intent contactInfoIntent = new Intent(clickedView.getContext(), ContactInformation.class);
clickedView.getContext().startActivity(contactInfoIntent);
于 2013-09-22T11:03:15.167 回答
0

您需要将点击事件绑定到添加到 listView 的每个项目

于 2013-09-22T10:59:37.637 回答
0

convertView.setTag(contactData)您可以通过调用您的getView()方法将数据(在您的情况下为 ContactDate)作为 ContactData 元素放到您的视图(每个单元格)中。

onItemClick wpuld 然后执行以下操作:

ContactData cd = (ContactData)clickedView.getTag();
Bundle b = new Bundle();
// put data from cd in that Bundle
Intent contactInfoIntent = new Intent(this, ContactInformation.class);
startActivity(contactInfoIntent);

更新:

试试这个:

package awad865.project.ContactManager1;


import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.example.contactmanager1.R;
import android.widget.AdapterView;

public class MainActivity extends Activity implements OnItemClickListener{

    private ListView listView;
    private ImageButton button1;
    private ImageButton button2;
    private ImageButton button3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getActionBar().setDisplayShowTitleEnabled(false);
        getActionBar().setDisplayShowHomeEnabled(false);

        listView = (ListView)findViewById(R.id.main_contact_listview);
        button1= (ImageButton)findViewById(R.id.button_search);
        button2= (ImageButton)findViewById(R.id.button_addcontact);
        button3= (ImageButton)findViewById(R.id.button_options);



        setUpListView();

    }

    private void setUpListView(){
        List <Contact> displayList = new ArrayList<Contact>();
        displayList.add(new Contact("Anmol","Wadhwa","53743632"));
        displayList.add(new Contact("Juhi","Goswami","4234232"));
        displayList.add(new Contact("Laurence","Baldwick","243232"));



        ListAdapter listAdapter = new CustomListAdapter(MainActivity.this,displayList);
        listView.setAdapter(listAdapter);

    }

    private class CustomListAdapter extends ArrayAdapter<Contact>{

        private Context _context;
        private List<Contact> _contacts;

        public CustomListAdapter(Context context, List<Contact> contacts){
            super(context,android.R.layout.simple_list_item_1,contacts);

            _context = context;
            _contacts = contacts;

        }

        public View getView(int position, View convertView,ViewGroup parent){
            //Create a layout inflater to inflate our xml layout for each item in the list
            LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            //Inflate the list item layout. Keep a reference to the inflated view.
            //No root view specified
            if (convertView == null)
                View convertView = inflater.inflate(R.layout.custom_list_item_layout,null);

            //Access TextView elements inside the view (note we must specify the parent view
            //to look in)
            TextView firstName =  (TextView)convertView.findViewById(R.id.list_item_firstname);
            TextView lastName =  (TextView)convertView.findViewById(R.id.list_item_lastname);
            TextView number =  (TextView)convertView.findViewById(R.id.list_item_number);

            //Set the text for each textview (use the position arugment to find the appropriate element in the list)
            firstName.setText(_contacts.get(position).getFirstName());
            lastName.setText(_contacts.get(position).getLastName());
            number.setText(_contacts.get(position).getNumber());

            //TODO add your data to the View

            convertView.setTag(yourData)


            return convertView;
        }

    }

    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
        case R.id.action_groups:
            Intent groupIntent = new Intent(this,Groups.class);
            startActivity(groupIntent);
            return true;

        case R.id.action_favourites:
            Intent favouriteIntent = new Intent(this,Favourites.class);
            startActivity(favouriteIntent);
        default:
            return super.onOptionsItemSelected(item);

        }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_activity_actions, menu);
        return true;
    }



    public void addContact(View view){
        Intent intent = new Intent(this,AddContact.class);
        startActivity(intent);

    }

    Override
    public void onItemClick(AdapterView<?> parentView, View clickedView, int clickedViewPosition, long id) {
        ContactData cd = (ContactData)clickedView.getTag();
        Bundle b = new Bundle();
        // TODO put data from cd in that Bundle
        Intent contactInfoIntent = new Intent(this, ContactInformation.class);
        startActivity(contactInfoIntent);

    }
}

此外,你通常做 iOS 开发还是我错了?

于 2013-09-22T11:04:05.053 回答
0

在 ListView 项目上启动 Activity 点击为:

第1步:

通过将 listItemClickedListener 类的实例传递为 OnItemClickListener 添加:

....
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new listItemClickedListener());

第2步:

从 onItemClick 启动 Activity 为:

 Intent contactInfoIntent = new Intent(clickedView.getContext(), 
                                               ContactInformation.class);
  clickedView.getContext().startActivity(contactInfoIntent);
于 2013-09-22T11:06:02.737 回答
0

构造函数的第一个参数Intent需要Context类的对象。但是你提供了一个listItemClickedListener.class.

意图构造函数要求:

Intent(Context, Class<?>)

您提供:

Intent(listItemClickedListener, Class<?>)
于 2013-09-22T11:06:18.993 回答