单击项目到另一个活动时,如何在自定义列表视图中传递textview的值?

时间:2015-03-03 21:32:40

标签: android listview inflate

我有一个从数据库填充的自定义列表视图,我希望当点击textview的任何项目值传递给另一个活动时,我该怎么办? 请帮帮我!

这是所有代码,谢谢你 源代码:         修改:

    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 个答案:

答案 0 :(得分:0)

第1步:

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

有关。 e.g。

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

注意:由于我们不知道你的适配器数据,上面的代码只是理解的例子。