2011-09-06 17 views
0

我正在使用ListView來顯示名稱數組。 代碼它看起來像這樣:將ListView中的多個名稱放入單個視圖中

public class Imenik extends ListActivity { 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    final String[] seznam = getResources().getStringArray(R.array.seznam_array); 
    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, seznam)); 

    final ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 

    lv.setOnItemClickListener(new OnItemClickListener() { 

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {    
      if ("Baj And".equals(seznam[position])) 
      { Intent i = new Intent(Imenik.this,Bajzelj.class); startActivity(i); lv.getItemIdAtPosition(position); } 
      else if ("Bes Jaz".equals(seznam[position])) 
      { Intent i = new Intent(Imenik.this,Bajzelj.class); startActivity(i); }    
     } 
     }); 
    } 

} 

現在我想顯示該名逼到一個視圖(每個選擇只有一個名字)不同的數據(一個XML文件)。

public class Bajzelj extends Activity { 

int position; 

    /** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    this.setTitle("Interni imenik"); 
    setContentView(R.layout.main); 

    final String[] seznam = getResources().getStringArray(R.array.seznam_array); 

    { 
    if ("Bajželj Andrej".equals(seznam[position])) 
    { 
     TextView ime = (TextView) findViewById(R.id.ime); 
     ime.setText("And Baj"); 
     TextView telefon = (TextView) findViewById(R.id.telefon); 
     telefon.setText("Telefon: +386 1 476"); 
     TextView gsm = (TextView) findViewById(R.id.mobitel); 
     gsm.setText("GSM: +386 41 "); 
     TextView skype = (TextView) findViewById(R.id.skype); 
     skype.setText("Skype: ba+"); 
     TextView email = (TextView) findViewById(R.id.email); 
     email.setText("E-pošta: [email protected]"); 
     ImageView slika = (ImageView) findViewById(R.id.slika); 
     slika.setImageResource(R.drawable.andbaj);   
    } 
    else if ("Bes Jan".equals(seznam[position])) 
    { 
     TextView ime = (TextView) findViewById(R.id.ime); 
     ime.setText("Jan Bes"); 
     TextView telefon = (TextView) findViewById(R.id.telefon); 
     telefon.setText("Telefon: +386 1 4"); 
     TextView gsm = (TextView) findViewById(R.id.mobitel); 
     gsm.setText("GSM: +386 41 75"); 
     TextView skype = (TextView) findViewById(R.id.skype); 
     skype.setText("Skype: sf"); 
     TextView email = (TextView) findViewById(R.id.email); 
     email.setText("E-pošta: Jan.Besej.si"); 
     ImageView slika = (ImageView) findViewById(R.id.slika); 
     slika.setImageResource(R.drawable.besjaz);   
    }  
    } 
} 

}

是否可以這樣顯示出來?我不是程序員,所以我遇到了很多麻煩。我知道我應該以某種方式檢查位置,但我不知道該怎麼做?

在此先感謝任何願意幫助我的人。

+0

我不清楚這個問題...你可以請指定更清楚。您正在數組的列表視圖中顯示數據。當點擊每個項目時,你想要執行什麼......? – Noby

+0

我試圖做到這一點 - 當我從listView中選擇一個名字時,我希望它打開一個活動Bajzelj。然後根據我選擇的名字,我想填寫個人日期(每個人有不同的數據)。對於活動Bajzelj,我只想使用一個具有空TextViews和ImageView的XML。 – user925185

+0

好吧,我明白你的要求。你在哪裏得到問題..在準備XML文件或獲取特定的數據。 – Noby

回答

1

遵循這一更有效的方式。

  1. 使用Map對象來存儲單個用戶數據。
  2. 將這些地圖對象添加到列表中。
  3. 使用自定義適配器顯示在ListView中。
  4. 無論何時用戶點擊特定列表項目,都意圖傳遞其位置。
  5. 從Bundle獲取位置並從List的映射中檢索特定的用戶數據。
  6. 顯示用戶的所有數據。

如果你想要一個代碼片段發送給我的用戶數據字段,我可以給你發送代碼片段。

這裏是代碼。

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> 

然後運行和享受......!

+0

感謝一個代碼片段就是我所需要的。用戶數據字段爲:姓名和姓氏,電話號碼,手機號碼,Skype用戶名,電子郵件。 – user925185

+0

@ user925185從哪裏得到這些數據..?從一個數組爲每個領域或一些其他手段..? – Noby

+0

我把它們自己放進去。我爲每個人都有一個包含該人所有數據的函數。我只需要弄清楚如何記住意圖決定(我選擇哪個名稱)並打開正確的決定。 – user925185

1

您將ID傳遞給新活動的方式很奇怪。嘗試使用這樣的:

Intent i = new Intent(Imenik.this,Bajzelj.class); 
i.putExtra("id", id); 
startActivity(i); 

然後,一旦你在你的Bajzelj活動的時候,得到的ID是這樣的:

Bundle bundle = getIntent().getExtras(); 
long _id = bundle.getLong("id");