遵循這一更有效的方式。
- 使用Map對象來存儲單個用戶數據。
- 將這些地圖對象添加到列表中。
- 使用自定義適配器顯示在ListView中。
- 無論何時用戶點擊特定列表項目,都意圖傳遞其位置。
- 從Bundle獲取位置並從List的映射中檢索特定的用戶數據。
- 顯示用戶的所有數據。
如果你想要一個代碼片段發送給我的用戶數據字段,我可以給你發送代碼片段。
這裏是代碼。
Test.java
package com.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class Test extends Activity{
public Map<String,Object> userData;
public static List<Object> userList;
String[] name = {"name_one","name_two","name_three","name_four","name_five"};
String[] lastName = {"lastName_one","lastName_two","lastName_three","lastName_four","lastName_five"};
String[] telephoneNumber = {"telephoneNumber_one","telephoneNumber_two","telephoneNumber_three","telephoneNumber_four","telephoneNumber_five"};
String[] mobileNumber = {"mobileNumber_one","mobileNumber_two","mobileNumber_three","mobileNumber_four","mobileNumber_five"};
String[] skypeUserName = {"skypeUserName_one","skypeUserName_two","skypeUserName_three","skypeUserName_four","skypeUserName_five"};
String[] email = {"email_one","email_two","email_three","email_four","email_five"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
userList = new ArrayList<Object>();
for(int i=0;i<5;i++){
userList.add(getUserData(i));
}
ListView lv = (ListView)findViewById(R.id.list);
ListCustAdapter adapter = new ListCustAdapter(this, userList);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Intent i = new Intent(Test.this,Test2.class);
i.putExtra("position", position);
startActivity(i);
}
});
}
public Map<String,Object> getUserData(int i){
userData = new HashMap<String,Object>();
userData.put("name", name[i]);
userData.put("lastName", lastName[i]);
userData.put("telephoneNumber", telephoneNumber[i]);
userData.put("mobileNumber", mobileNumber[i]);
userData.put("skypeUserName", skypeUserName[i]);
userData.put("email", email[i]);
return userData;
}
}
Test2.java
package com.test;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Test2 extends Activity{
TextView name,lname,phono,cellno,sky,email;
@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
Bundle bundle = getIntent().getExtras();
int position = bundle.getInt("position");
name = (TextView)findViewById(R.id.name);
lname = (TextView)findViewById(R.id.last_name);
phono = (TextView)findViewById(R.id.telephoneNumber);
cellno = (TextView)findViewById(R.id.mobileNumber);
sky = (TextView)findViewById(R.id.skypeUserName);
email = (TextView)findViewById(R.id.email);
name.setText((String)((Map<String,Object>)Test.userList.get(position)).get("name"));
lname.setText((String)((Map<String,Object>)Test.userList.get(position)).get("lastName"));
phono.setText((String)((Map<String,Object>)Test.userList.get(position)).get("telephoneNumber"));
cellno.setText((String)((Map<String,Object>)Test.userList.get(position)).get("mobileNumber"));
sky.setText((String)((Map<String,Object>)Test.userList.get(position)).get("skypeUserName"));
email.setText((String)((Map<String,Object>)Test.userList.get(position)).get("email"));
}
}
ListCustAdapter.java
package com.test;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class ListCustAdapter extends BaseAdapter {
public Activity context;
private List<Object> list;
public ListCustAdapter(Activity context, List<Object> list) {
super();
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@SuppressWarnings("unchecked")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
TextView tv = new TextView(context);
tv.setText((String) ((Map<String, Object>) list.get(position)).get("name"));
return tv;
}
}
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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id = "@+id/list"
/>
</LinearLayout>
test2.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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id = "@+id/name"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id = "@+id/last_name"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id = "@+id/telephoneNumber"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id = "@+id/mobileNumber"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id = "@+id/skypeUserName"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id = "@+id/email"/>
</LinearLayout>
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Test"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Test2"/>
</application>
</manifest>
然後運行和享受......!
我不清楚這個問題...你可以請指定更清楚。您正在數組的列表視圖中顯示數據。當點擊每個項目時,你想要執行什麼......? – Noby
我試圖做到這一點 - 當我從listView中選擇一個名字時,我希望它打開一個活動Bajzelj。然後根據我選擇的名字,我想填寫個人日期(每個人有不同的數據)。對於活動Bajzelj,我只想使用一個具有空TextViews和ImageView的XML。 – user925185
好吧,我明白你的要求。你在哪裏得到問題..在準備XML文件或獲取特定的數據。 – Noby