我只是想知道我是否能够通过单击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()方法中启动一个新活动。任何帮助都将受到赞赏,因为编译器不允许我开始新的意图。
答案 0 :(得分:1)
您可以从Context
检索所需的clickedView
。
Intent contactInfoIntent = new Intent(clickedView.getContext(), ContactInformation.class);
clickedView.getContext().startActivity(contactInfoIntent);
答案 1 :(得分:0)
您需要将click事件绑定到添加到listView的每个项目
答案 2 :(得分:0)
您可以通过在convertView.setTag(contactData)
方法中调用getView()
将数据(在您的情况下为ContactDate)作为ContactData元素放入您的View(每个单元格)。
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开发或者我错了吗?
答案 3 :(得分:0)
启动ListView项目上的活动单击为:
第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);
答案 4 :(得分:0)
Intent
构造函数的第一个参数需要Context
类的对象。但是您提供了listItemClickedListener.class
。
意图构造函数重新出现:
Intent(Context, Class<?>)
你提供了:
Intent(listItemClickedListener, Class<?>)