使用自定义视图将字符串数组传递给另一个活动

Posted

技术标签:

【中文标题】使用自定义视图将字符串数组传递给另一个活动【英文标题】:Passing String array to another Activity with Custom View 【发布时间】:2017-05-26 15:04:57 【问题描述】:

我在将数组传递到列表视图时遇到问题 我可以将图像传递给第二个活动,但是当我 尝试将数组传递给列表视图我得到一个 lang.NullPointerException 错误 并且应用崩溃了。

任何有用的意见都会非常感谢

这里是代码 主要活动

package com.example.androidlistview;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;



public class MainActivity extends Activity 



    ListView listView;
    // Defined the text values to show in ListView
    String[] values = new String[]"Barack Obama","Donald Trump","Bill Clinton",
            "Hillary Clinton","Joe Biden";
    // Defined the image ids to show in ListView
    Integer[] images = 
            R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,
            R.drawable.image5   
    ;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // create the custom view
        CustomView adapter = new CustomView(MainActivity.this, values, images);

        // Get ListView object from xml
        listView = (ListView) findViewById(R.id.container);
        //set adapter
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 

            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) 


                if(position == 0)

                    Intent contact = new Intent(MainActivity.this, Details.class);
                    String[] details= new String[]"Barack Obama","The White House","1-234-567-8900",
                            "Barack.Obama@whitehouse.com","note";
                    Integer image = images[0];
                    contact.putExtra("image", image);
                    contact.putExtra("details", details);
                    startActivity(contact);   
                
                if(position == 1)
                    Intent contact = new Intent(MainActivity.this, Details.class); 
                    String[] details= new String[] "Donald Trump","Trump Tower","1-234-567-8901",
                            "Donald.Trump@whitehouse.com","note";
                    Integer image = images[1];
                    contact.putExtra("image", image);
                    contact.putExtra("details", details);
                    startActivity(contact);
                
                 if(position == 2)
                    Intent contact = new Intent(MainActivity.this, Details.class);
                    String[] details = new String[] "Bill Clinton","New York","1-234-567-8902",
                            "Bill.Clinton@whitehouse.com","note";
                    Integer image = images[2];
                    contact.putExtra("image", image);
                    contact.putExtra("details", details);
                    startActivity(contact);                 
                 
                 if(position == 3)
                    Intent contact = new Intent(MainActivity.this, Details.class);
                    String[] details = new String[] "Hillary Clinton","New Jersey","1-234-567-8903",
                            "Hillary.Clinton@whitehouse.com","note";
                    Integer image = images[3];
                    contact.putExtra("image", image); 
                    contact.putExtra("details", details);
                    startActivity(contact);

                 
                 if(position == 4)
                    Intent contact = new Intent(MainActivity.this, Details.class);
                    String[] details= new String[] "Joe Biden","OHIO","1-234-567-8903",
                            "Joe.Biden@whitehouse.com","note";
                    Integer image = images[4];
                    contact.putExtra("image", image);
                    contact.putExtra("details", details);
                    startActivity(contact);
                
            
        );
    

第二个活动我想将数组传递给

package com.example.androidlistview;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Details extends Activity 

    ListView list;

    String []web="Name:","Address:","Phone Number:","Email:","Note:";
    int image;
    String[] details;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) 


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        // create the custom view
        CustomList Adapter= new CustomList(Details.this, web, details);//, details should go here
        // Get ListView object from xml
        list=(ListView)findViewById(R.id.list);
        //set adapter
        list.setAdapter(Adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener()


        @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) 
            

        );

        Bundle bundle = getIntent().getExtras();
        if (bundle != null)        
            image= bundle.getInt("image");
            ImageView imageView = (ImageView) findViewById(R.id.imageView1);    
            imageView.setImageResource(image);

        

    

以及我用于第二个活动详细信息的自定义视图

package com.example.androidlistview;

import android.app.Activity; 
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;


public class CustomList extends ArrayAdapter<String>

    private final Activity mContext;
    private final String[] web;
    private final String[] details;


        public CustomList(Activity mContext,String[] web, String[] details) //, String[] details should go here
        super(mContext, R.layout.list_single, web);
        this.mContext=mContext;
        this.web=web;
        this.details=details;

    

    public View getView(int position, View view, ViewGroup parent) 
        LayoutInflater inflater = mContext.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.list_single, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
        txtTitle.setText(web[position]);

        TextView txtTitle1 = (TextView)rowView.findViewById(R.id.txt1);
        txtTitle1.setText(details[position]);

        return rowView;

    

【问题讨论】:

【参考方案1】:

这就是我为解决这个问题所做的 主要活动

package com.example.androidcontacts;



import android.app.ListActivity;
import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends ListActivity 

    ListView mylist;
    SearchView searchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Uri allContacts = Uri.parse("content://"+"com.example.androidcontacts.Books" + "/Contact");

        Cursor c;
        CursorLoader cursorLoader = new CursorLoader(
        this,allContacts,null,null,null,null);
        c = cursorLoader.loadInBackground();

        String[] columns = new String[] Contact._ID,Contact.TITLE,Contact.AUTHOR,Contact.YEAR,Contact.IMAGE;

        int[] views = new int[] R.id.contactID,R.id.contactName,R.id.contactAuthor,R.id.contactYear,R.id.Image;

        SimpleCursorAdapter adapter;
        adapter = new SimpleCursorAdapter(this, R.layout.activity_main, 
                c, columns, views,CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        this.setListAdapter(adapter); 
        





     @Override
     protected void onListItemClick(ListView list, View view, int position, long id) 

         //Intent i=new Intent(MainActivity.this,Details.class);
         //startActivity(i);
 

     @Override
        public boolean onCreateOptionsMenu(Menu menu) 

            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.main, menu);
              return true;
     

    @Override
    public boolean onOptionsItemSelected(MenuItem item)

        int id = item.getItemId();

        if(id == R.id.action_search)
            Intent intent = new Intent(this, Search.class);
            startActivity(intent);
            finish();
            return true;
        

        if(id == R.id.action_add)
            Intent intent = new Intent(this, Add.class);
            startActivity(intent);
            finish();
            return true;
        


        return super.onOptionsItemSelected(item);
    




第二个活动详情

package com.example.androidlistview;


import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Details extends Activity 


    //Variables for Bundle
    int image;
    String name;
    String address;
    String phone;
    String email;
    String note;
    @Override
    protected void onCreate(Bundle savedInstanceState) 


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);

        Button SMS = (Button) findViewById(R.id.button1);
        SMS.setOnClickListener(new OnClickListener() 
            //when button1 is clicked open messenger
            @Override
            public void  onClick(View v) 

                startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", phone, null)));
            

        );

        Button Email = (Button) findViewById(R.id.button2);
        Email.setOnClickListener(new OnClickListener() 
            //when button2 is clicked open email
            @Override
            public void  onClick(View v) 
                Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
                emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]  email );
                startActivity(Intent.createChooser(emailIntent, "Choose Email Client"));

            

        );

        Button Phone = (Button) findViewById(R.id.button3);
        Phone.setOnClickListener(new OnClickListener() 
            //when button3 is clicked start call
            @Override
            public void  onClick(View v) 

                startActivity(new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null)));
            

        );


        // get details passed from MainActivity
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) 

            image= bundle.getInt("image");
            ImageView imageView = (ImageView) findViewById(R.id.imageView1);    
            imageView.setImageResource(image);

            name = bundle.getString("name");
            TextView nameView = (TextView) findViewById(R.id.name1);    
            nameView.setText(name);

            address = bundle.getString("address");
            TextView addressView = (TextView) findViewById(R.id.address1);    
            addressView.setText(address);

            phone = bundle.getString("phone");
            TextView phoneView = (TextView) findViewById(R.id.phone1);    
            phoneView.setText(phone);

            email = bundle.getString("email");
            TextView emailView = (TextView) findViewById(R.id.email1);    
            emailView.setText(email);

            note = bundle.getString("note");
            TextView noteView = (TextView) findViewById(R.id.note1);    
            noteView.setText(note);

        

    

我的自定义视图

package com.example.androidlistview;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomView extends ArrayAdapter<String>

    private final Activity context;
    private final String[] values;
    private final Integer[] images;

    //Constructor
    public CustomView(Activity context,String[] values, Integer[] images) 

            super(context, R.layout.single_row, values);
            this.context = context;
            this.values = values;
            this.images = images;
    

    @Override
    public View getView(int position, View view, ViewGroup parent) 
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.single_row, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
        txtTitle.setText(values[position]);

        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
        imageView.setImageResource(images[position]);
        return rowView;
    

【讨论】:

以上是关于使用自定义视图将字符串数组传递给另一个活动的主要内容,如果未能解决你的问题,请参考以下文章

如何将数组传递给另一个活动?

如何在ios中自定义推送和弹出动画时将视图传递给另一个控制器? [关闭]

传递 listview 数组字符串以查看另一个活动中的文本

将自定义 ListView 项传递给 Android 中的其他活动

无法将 json 传递给另一个活动的意图

将列表视图选定的文本传递给另一个活动