简体   繁体   中英

How to get data and View from a ListView

Hi everyone: I hope someone can help me here,The Cursor generated by a Query populate the ListView with the Layout defined by simple_list_item_2. To create the Intent i need the first string of the clicked View but in the String to Go i have something like

android.widget.TextView@411fbfb8

Now the code. I can't understand where is the error.

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, columns, ids, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

    final ListView myList=(ListView)findViewById(R.id.listView1);
    myList.setAdapter(adapter);
    myList.setClickable(true);

    /*
     * Click Listener
     */
    myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> l, View v, int position, long id)
        {
            Log.v("io", "start");
            Intent intent = new Intent(MainActivity.this, WorkActivity.class);

            View buff = myList.getChildAt(position);
            String toGo = buff.findViewById(android.R.id.text1).toString();

            Log.v("io",toGo);
            intent.putExtra("dname", toGo);
            startActivity(intent);
        }
    } );

尝试:

String toGo = ((TextView)buff.findViewById(android.R.id.text1)).getText().toString()

inside your onItemClick you can retrieve data linked to your adapter through the AdapterView argument. Calling getItemAtPosition(int) you can access data associated with the selected item

myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> l, View v, int position, long id)
        {
            Log.v("io", "start");
            Intent intent = new Intent(MainActivity.this, WorkActivity.class);

            View buff = myList.getChildAt(position);
            String toGo = l.getItemAtPosition(position);

            Log.v("io",toGo);
            intent.putExtra("dname", toGo);
            startActivity(intent);
        }
    } );

//

myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> l, View v, int position, long id)
    {
        Log.v("io", "start");
        Intent intent = new Intent(MainActivity.this, WorkActivity.class);

        //View buff = myList.getChildAt(position);
       // String toGo = buff.findViewById(android.R.id.text1).toString();
     String toGo = (TextView)v.getText();

        Log.v("io",toGo);
        intent.putExtra("dname", toGo);
        startActivity(intent);
    }
} );

To get the view do as following:

LinearLayout row = (LinearLayout) ((LinearLayout)v);

Or RelativeLayout instead of LinearLayout if that your case

then apply the follwing code to get the text of TextView based on its order:

TextView column = (TextView)row.getChildAt(0);
String text = column.getText().toString();

Hope this help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM