所以我很擅长接触。我将联系人分配给列表视图并且能够看到它们。当我单击一个联系人时,它会启动一个新活动,目前我只想显示该联系人的 ID。所以我所做的是将联系人 ID 设置为“set_id”,当您单击联系人时,它会调用“get_id”,将 ID 分配给 contactId,并将其传递给 putextra ...
导入 java.util.ArrayList;
导入android.app.Activity;导入 android.content.CursorLoader;导入android.content.Intent;导入android.database.Cursor;导入android.net.Uri;导入android.os.Bundle;导入 android.provider.ContactsContract;导入 android.view.Menu;导入android.view.View;导入 android.widget.AdapterView;导入 android.widget.ListView;
公共类 ListViewActivity 扩展 Activity {
ListItemDetails Detail; ListView mListView; ArrayList<ListItemDetails> details; Uri contacts; String contactName; int contactPic; int contactId; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mListView = (ListView)findViewById(R.id.listView); details = new ArrayList<ListItemDetails>(); contacts = ContactsContract.Contacts.CONTENT_URI; CursorLoader cursorLoader = new CursorLoader( this, contacts, null, null, null, null); Cursor c = cursorLoader.loadInBackground(); while (c.moveToNext()) { contactId = c.getColumnIndex(ContactsContract.Contacts._ID); contactName = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); Detail = new ListItemDetails(); Detail.setImage(R.drawable.ic_launcher); Detail.setName(contactName); Detail.set_id(contactId); details.add(Detail); } c.close(); mListView.setAdapter(new CustomListAdapter(details, this)); mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { Intent i = new Intent(ListViewActivity.this, ContactDetails.class); i.putExtra("contactId", contactId); startActivity(i); } }); }
现在,在新的 Activity 中,我只想让它暂时显示联系人 ID(仍在学习中)。所以我调用Bundle getInt,将int值分配给“contactId”并将其显示在屏幕上......无论我选择什么联系人,我都会显示id = 0。我做错了什么?这是第二个活动:
导入android.app.Activity;导入android.os.Bundle;导入 android.widget.TextView;
公共类 ContactDetails 扩展 Activity {
int contactId; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.contactinfo); Bundle extras = new Bundle(); contactId = extras.getInt("contactId"); TextView contactName = (TextView)findViewById(R.id.contactName); contactName.setText("You clicked " + contactId); } }
tl;博士无论我在列表视图中选择什么联系人,它都说联系人 ID = 0,为什么?