如何将数据从listview传递到另一个活动页面?

时间:2016-02-20 09:01:53

标签: java android listview

这里有新手问题。所以我试图将标题从1stPage listview传递到2ndPage EditText,这意味着如果我点击1stPage上的Title1,第2页editText也会显示Title1。香港专业教育学院学会了如何将额外的意图加入,但我不知道如何从listview获取数据。 tqvm在先进。

1.MainActivity.java

public class MainActivity extends AppCompatActivity{

ListView l1;
String[] t1={"TITLE 1","TITLE 2"};
String[] d1={"Detail 1","Detail 2"};
String[] s1={"Content 1", "Content 2"};

TextView textView;

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

    textView = (TextView) findViewById(R.id.title);

    l1=(ListView)findViewById(R.id.list);
    l1.setAdapter(new dataListAdapter(t1, d1, s1));


}

class dataListAdapter extends BaseAdapter {
    String[] Title, Detail, Content;

    dataListAdapter() {
        Title = null;
        Detail = null;
        Content=null;
    }

    public dataListAdapter(String[] text, String[] text1, String[] text3) {
        Title = text;
        Detail = text1;
        Content = text3;

    }

    public int getCount() {
        // TODO Auto-generated method stub
        return Title.length;
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = getLayoutInflater();
        View row;
        row = inflater.inflate(R.layout.custom, parent, false);
        final TextView title, detail, detail2;
        title = (TextView) row.findViewById(R.id.title);
        detail = (TextView) row.findViewById(R.id.detail);
        detail2 = (TextView) row.findViewById(R.id.detail2);
        title.setText(Title[position]);
        detail.setText(Detail[position]);
        detail2.setText(Content[position]);

        l1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String selected = title.getText().toString();
                Intent i = new Intent(getApplicationContext(), Main2Activity.class);
                i.putExtra("name", selected);
                startActivity(i);
            }
        });

        return (row);
    }

}


}

2.Main2Activity.java

公共类Main2Activity扩展了AppCompatActivity {

EditText editText;

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

    editText = (EditText)findViewById(R.id.et);

    Intent in = getIntent();
    String name = in.getStringExtra(("name"));
    editText.setText(name);
}

}

2 个答案:

答案 0 :(得分:1)

您应该在行

之后的MainActivity中声明此方法
    l1=(ListView)findViewById(R.id.list);
    l1.setAdapter(new dataListAdapter(t1, d1, s1));

    l1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            String selected = t1[position];
            Intent i = new Intent(getApplicationContext(), Main2Activity.class);
            i.putExtra("name", selected);
            startActivity(i);
        }
    });

并从dataListAdapter

中删除

在另一项活动中,使用以下方法获取标题

Bundle extras = getIntent().getExtras();
String name = extras.getString("name");
editText.setText(name);

希望它能运作。

答案 1 :(得分:0)

在第一个活动中,您应该为这样的意图添加额外的参数。存储&#34;论证&#34;也很好。 MainActivity中的字符串为常量,并始终在其他位置引用它。

public static final String ARG_FROM_MAIN = "argument";

//我假设B.class是你的第二个活动

Intent intent = new Intent(MainActivity.this, B.class); 
intent.putExtra("argument", "Title1");
startActivity(intent);

然后在 B 活动中,您可以检索这样的参数:

String passedArg = getIntent().getExtras().getString("argument");
enteredValue.setText(passedArg);