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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了传递listview数组字符串以查看另一个活动中的文本相关的知识,希望对你有一定的参考价值。

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

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);


    }

}
答案

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

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

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

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

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

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

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

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

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

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