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

Posted

技术标签:

【中文标题】传递 listview 数组字符串以查看另一个活动中的文本【英文标题】:Passing listview array strings to viewtext in another activity 【发布时间】:2018-06-12 12:40:40 【问题描述】:

我有一个列表视图活动,它显示来自我的自定义类 (ml_lists) 和适配器 (ml_my_adapter) 的数组 (ml_main)。该数组是通过另一个活动添加的。我有第三个活动,我想在选择适当的列表项时在文本视图中显示项目和子项。这是我正在努力解决的最后一部分,我的意图打开了第三个活动,但文本视图是空的(甚至没有出现),任何帮助都非常感谢,下面的代码......

package com.example.adam.mylists;

public class ml_lists 


    // Store the name of the item
    private String mItem;
    // Store the name of the subitem
    private String mSubItem;

    // Constructor that is used to create an instance of the list_my_list  object
    public ml_lists(String mItem, String mSubItem) 

        this.mItem = mItem;
        this.mSubItem = mSubItem;
    

    public String getmItem() 
        return mItem;
    

    public void setmItem(String mItem) 
        this.mItem = mItem;
    

    public String getmSubItem() 
        return mSubItem;
    

    public void setmSubItem(String mSubItem) 
        this.mSubItem = mSubItem;
    



package com.example.adam.mylists;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

/*The code is now stable to generate a list which is predefined, now need to amend so that
it is populated via user input*/

public class ml_main extends AppCompatActivity 


    String item;
    String subitem;
    ArrayList<ml_lists> userlist = new ArrayList<>();
    private ListView listView;
    private ml_my_adapter mAdapter;

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

        final Context context = getApplicationContext();
        final FloatingActionButton addnewitem = (FloatingActionButton) findViewById(R.id.floatingActionButton);
        listView = findViewById(R.id.listview_list);
        userlist.add(new ml_lists("Item 1 example", "Sub item 1 example"));
        mAdapter = new ml_my_adapter(this, userlist);
        listView.setAdapter(mAdapter);

        addnewitem.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                Intent launchactivity = new Intent(context, ml_create_new_item_screen.class);
                startActivityForResult(launchactivity, 1);
                overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up);
            
        );

        /*BELOW CODE TO SELECT ITEM FROM LISTVIEW AND OPEN IT BACK UP IN THE CREATE ITEM ACT*/
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) 
                /*intent used to open selected activity into editor*/
                Intent edititem = new Intent(context, ml_edit_existing_item_screen.class);
                edititem.putExtra("item",item);
                edititem.putExtra("subitem",subitem);
                startActivity(edititem);
            
        );


    

    @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        if (requestCode == 1) 
            if (resultCode == Activity.RESULT_OK) 
                item = data.getStringExtra("tempitem");
                subitem = data.getStringExtra("tempsubitem");
                userlist.add(new ml_lists(item, subitem));
                mAdapter.notifyDataSetChanged();


            
            if (resultCode == Activity.RESULT_CANCELED) 
                Toast replacewithcode = Toast.makeText(ml_main.this, "replace with code", Toast.LENGTH_SHORT);
                replacewithcode.show();
            
        
    





package com.example.adam.mylists;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/*Created by Adam Garnham*/

public class ml_my_adapter extends ArrayAdapter<ml_lists> 

    private Context mContext;
    private List<ml_lists> mList = new ArrayList<>();

    public ml_my_adapter(@NonNull Context context, @LayoutRes ArrayList<ml_lists> list) 
        super(context, 0, list);
        mContext = context;
        mList = list;
    

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) 
        View listItem = convertView;
        if (listItem == null)
            listItem = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);

        ml_lists currentItem = mList.get(position);

        TextView mItem = (TextView) listItem.findViewById(R.id.textView_item);
        mItem.setText(currentItem.getmItem());

        TextView mSubItem = (TextView) listItem.findViewById(R.id.textView_subitem);
        mSubItem.setText(currentItem.getmSubItem());

        return listItem;
    


package com.example.adam.mylists;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

/**
 * Created by Adam on 02/01/2018.
 */

public class ml_edit_existing_item_screen extends AppCompatActivity 

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


        Intent edititem = getIntent();
        String item = edititem.getStringExtra("item");
        String subitem = edititem.getStringExtra("subitem");

        TextView itemtextview = findViewById(R.id.itemtextview);
        TextView subitemtextview = findViewById(R.id.subitemtextview);

        itemtextview.setText(item);
        subitemtextview.setText(subitem);


    


【问题讨论】:

【参考方案1】:

您的代码的问题是您没有在点击监听器中为“item”和“subitem”分配值。他们是空的。您可以通过将所选位置传递给用户列表来获取当前所选项目的值。

很可能你可以做这样的事情。

ml_lists selectedItem=userlist.get(i);
item=selectedItem.getmItem();
subItem=selectedItem.getmSubItem();

【讨论】:

以上是关于传递 listview 数组字符串以查看另一个活动中的文本的主要内容,如果未能解决你的问题,请参考以下文章

使用 Bundle 将数组数据传递给 listview click 上的另一个活动

ListView 中的隐藏字段以传递变量

将 listView 数据从一个活动发送到另一个活动

在另一个 Activity 中将字符串(或数组)传递给 ListView(NPE 错误)

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

使用 ListView 从另一个 Activity 打印 ArrayList