新手问题在这里。所以我试图将标题从1stPage listview传递给2ndPage EditText,这意味着如果我单击1stPage上的Title1,则2ndPage editText也将显示Title1。香港专业教育学院学会了把更多的意图,但我不知道如何从列表视图中获取数据。 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);
}

}

最佳答案

您应该在以下各行的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);


希望它能工作。

10-07 17:20