这是我的layuout main.xml;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/android:list" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1" android:visibility="visible" android:clickable="true"/>
</LinearLayout>
以下是扩展ListActivity的MainActivity;
public class MainActivity extends ListActivity {
private final List<SpinnerEntry> spinnerContent = new LinkedList<SpinnerEntry>();
private ListView contactListView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
contactListView=getListView();
queryAllRawContacts();
registerForContextMenu(getListView());
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this, ContactDetailsActivity.class);
i.putExtra("hello", id);
// Activity returns an result if called with startActivityForResult
startActivity(i);
}
private void queryAllRawContacts() {
final String[] projection = new String[] {
RawContacts.CONTACT_ID, // the contact id column
RawContacts.DELETED // column if this contact is deleted
};
final Cursor rawContacts = managedQuery(
RawContacts.CONTENT_URI, // the uri for raw contact provider
projection,
null, // selection = null, retrieve all entries
null, // not required because selection does not contain parameters
null); // do not order
final int contactIdColumnIndex = rawContacts.getColumnIndex(RawContacts.CONTACT_ID);
final int deletedColumnIndex = rawContacts.getColumnIndex(RawContacts.DELETED);
spinnerContent.clear();
if(rawContacts.moveToFirst()) { // move the cursor to the first entry
while(!rawContacts.isAfterLast()) { // still a valid entry left?
final int contactId = rawContacts.getInt(contactIdColumnIndex);
final boolean deleted = (rawContacts.getInt(deletedColumnIndex) == 1);
if(!deleted) {
spinnerContent.add(queryDetailsForContactSpinnerEntry(contactId));
}
rawContacts.moveToNext(); // move to the next entry
}
}
rawContacts.close();
final ContactsSpinnerAdapater adapter = new ContactsSpinnerAdapater(spinnerContent, this);
contactListView.setAdapter(adapter);
}
这是我的customadapter类,名为ContactsSpinnerAdapater;
public class ContactsSpinnerAdapater extends BaseAdapter implements
SpinnerAdapter{
private final List<SpinnerEntry> content;
private final Activity activity;
public ContactsSpinnerAdapater(List<SpinnerEntry> content,
Activity activity) {
super();
this.content = content;
this.activity = activity;
}
public int getCount() {
return content.size();
}
public SpinnerEntry getItem(int position) {
return content.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView,
ViewGroup parent) {
final LayoutInflater inflater = activity.getLayoutInflater(); // default layout inflater
final View spinnerEntry = inflater.inflate(
R.layout.row, null); // initialize the layout from xml
final TextView contactName = (TextView) spinnerEntry
.findViewById(R.id.cName);
final ImageView contactImage = (ImageView) spinnerEntry
.findViewById(R.id.userIcon);
final SpinnerEntry currentEntry = content.get(position);
contactName.setText(currentEntry.getContactName());
contactImage.setImageBitmap(currentEntry.getContactPhoto());
spinnerEntry.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
}});
return spinnerEntry;
}
}
问题在于,无论我尝试什么,我都无法在单击任何列表项时启动ContacDetailsActivity意图。基本上,听众有些如何不被激活。那么请你看看代码并告诉我我在哪里错过了它?
答案 0 :(得分:1)
您已将意图启动代码放在错误的位置。你需要输入这段代码:
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this, ContactDetailsActivity.class);
i.putExtra("hello", id);
// Activity returns an result if called with startActivityForResult
startActivity(i);
在你的适配器中的getView方法中
public View getView(int position, View convertView,
ViewGroup parent) {
final int myFinalPosition = position;
final LayoutInflater inflater = activity.getLayoutInflater(); // default layout inflater
final View spinnerEntry = inflater.inflate(
R.layout.row, null); // initialize the layout from xml
final TextView contactName = (TextView) spinnerEntry
.findViewById(R.id.cName);
final ImageView contactImage = (ImageView) spinnerEntry
.findViewById(R.id.userIcon);
final SpinnerEntry currentEntry = content.get(position);
contactName.setText(currentEntry.getContactName());
contactImage.setImageBitmap(currentEntry.getContactPhoto());
spinnerEntry.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
Intent i = new Intent(activity, ContactDetailsActivity.class);
i.putExtra("hello", getItemId(myFinalPosition));
// Activity returns an result if called with startActivityForResult
activity.startActivity(i);
}});
return spinnerEntry;
}
请注意使用activity变量来启动意图。