简体   繁体   English

如何从ListView获取数据和查看

[英]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. 大家好:我希望有人可以在这里为您提供帮助,由查询生成的游标会使用由simple_list_item_2定义的Layout填充ListView To create the Intent i need the first string of the clicked View but in the String to Go i have something like 要创建Intent,我需要单击视图的第一个字符串,但是在“要运行的字符串”中,我有类似以下内容

android.widget.TextView@411fbfb8 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. onItemClick内部,您可以通过AdapterView参数检索链接到adapter数据。 Calling getItemAtPosition(int) you can access data associated with the selected item 调用getItemAtPosition(int)可以访问与所选项目关联的数据

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 或者,如果您的情况是RelativeLayout而不是LinearLayout

then apply the follwing code to get the text of TextView based on its order: 然后应用以下代码以根据其顺序获取TextView的文本:

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

Hope this help 希望这个帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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