单击项目时如何将自定义列表视图中的 textview 的值传递给另一个 activity?

how pass value of textview in custom listview when click on item to another activity?

我有一个从数据库填充的自定义列表视图,我想在单击文本视图的任何项目值时传递给另一个 activity,我该怎么办? 请帮助我!

这是所有代码,谢谢你 源代码: 编辑:

    public class ListActivity extends Activity {

    String value = "";
    MovieDB myDbHelper;
    SQLiteDatabase db;
    ListAdapter adapter;
    ArrayList<HashMap<String, String>> data;


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


        final ListView lst=(ListView) findViewById(R.id.listView1);

        Load_Database();
        db = myDbHelper.getReadableDatabase();


        Cursor  c = db.rawQuery("select * from movie_list where product  = '"+value+"' Or name  like '%"+value+"%'" , null);

            data = new ArrayList<HashMap<String, String>>();

            for (; c.moveToNext();) {
                HashMap<String, String> map = new HashMap<String, String>();
                String img = c.getString(c.getColumnIndex("img"));
                String name = c.getString(c.getColumnIndex("name"));
                map.put("img", img);
                map.put("name", name);
                data.add(map);
            }
            adapter = new ListMovie(this, data);

            lst.setAdapter(adapter);

            lst.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {

                     final Intent GoToDisplay = new Intent(ListActivity.this,DisplayActivity.class);

                            GoToDisplay.putExtra("position", data.get(arg2));
                     startActivity(GoToDisplay);


                }

            });


    }


    private void Load_Database() throws Error {
        myDbHelper = new MovieDB(ListActivity.this);
        try {

            myDbHelper.createDataBase();

        } catch (IOException ioe) {

            throw new Error("Unable to create database");

        }

        try {

            myDbHelper.openDataBase();
        } catch (SQLException sqle) {
            throw sqle;
        }
    }
}

第 1 步:

只需使用 ListView 中的位置从适配器获取数据。

为了。例如

You have ArrayList<String> data;//

然后访问,

GoToDisplay.putExtra("position", data.get(arg2)); // as arg2 referees to clicked item position from ListView

第 2 步:

从视图中获得点击TextView

TextView clickedTextView = (TextView)arg1.findViewById(// Id of your TextView); // Or get child from view arg1

GoToDisplay.putExtra("position", clickedTextView.getText().toString());

注意:由于我们不知道您的 Adapter 数据,以上代码只是为了理解示例..