开发者

Diplaying multiple names from ListView into a single view

I'm using a ListView to display an array of names. Code for it looks like this:

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 onI开发者_如何转开发temClick(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); }             
        }
      });
    } 

}

Now I would like to display this names into just one view (Only one name per choice) with different data (one xml file).

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: and.baj@som.org");
        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);          
    }       
    }  
}

}

Is it possible to display them like this? I'm not a programmer so I'm having a lot of troubles with this one. I know that I should check for position somehow but I don't know how to do this?

Thanks in advance to anybody who is willing to help me.


follow this more effective way.

  1. Use a Map object to store individual user data.
  2. Add these map objects to the List.
  3. Use custom adapter to display in the ListView.
  4. Whenever user clicks on particular list item, pass its position in intent.
  5. get position from Bundle and retrieve the particular user data from map of List.
  6. display all the data of the user.

If you want a code snippet send me the user data fields i can send you the code snippet.

here is the code.

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>

Then run and enjoy......!


The way in which you are passing the ID to the new activity is strange. Try using this:

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

Then, once you're in your Bajzelj activity, get the id like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜