I want to extract only any item from ListView that contains multiple items.

Asked 3 months ago, Updated 3 months ago, 6 views

I'm making an Android app, but I don't know how to take out any items from any location from my customized ListView.

item.xml

<TextView
    android: id="@+id/id_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25sp"/>

<TextView
    android:id="@+id/name_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<TextView
    android:id="@+id/relationship_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<TextView
    android:id="@+id/date_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<TextView
    android: id="@+id/place_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

activity_main.xml

<ListView
         android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false"
        android:layout_weight="1">
    </ListView>

Part of MainActivity

@Override
    protected void onResume() {
        super.onResume();
        setContentView(R.layout.activity_main);
        SQLiteDatabase db=helper.getReadableDatabase();

        ListView listView1=(ListView) findViewById (R.id.listView);

        // Get DB
        cursor=db.query("persons", null, null, null, null, null);
        cursor.moveToFirst();

        listView1.setAdapter(newSimpleCursorAdapter(this,R.layout.item,cursor,newString[]{
                "_id", "name", "relationship", "date", "place"}, new int [ ] {R.id.id_item, R.id.name_item, R.id.relationship, R.id.date_item, R.id.place_item}, 0);

        listView1.setOnItemClickListener(newAdapterView.OnItemClickListener(){
            public void onItemClick(AdapterView<?>parent, View view, int position, longid) {

            }

        });

    }

When ListView is clicked, I would like to take out the contents of R.id.id_item and substitute the variables, but I don't know how to take out the contents of TextView in any position.What should I do?
Up until now, I have moved to the position with cursor.moveToPosition(position), but I'm having trouble finding that I can't use it when I rearrange ListView.

android java

2022-09-30 11:41

2 Answers

Use cursor.getColumnIndex("name") to extract items from Cursor:

String currentId=cursor.getString(cursor.getColumnIndex("_id"));
String currentName=cursor.getString(cursor.getColumnIndex("name"));

For example:

listView1.setOnItemClickListener(newAdapterView.OnItemClickListener(){
    public void onItemClick(AdapterView<?>parent, View view, int position, longid) {

        String currentId = cursor.getString( cursor.getColumnIndex("_id"));
        String currentName=cursor.getString(cursor.getColumnIndex("name"));
        Log.v(TAG, "Click id/name is:"+currentId+"=>"+currentName);

    }

});


2022-09-30 11:41

If it is good to get it from TextView of the clicked item,
The view argument of onItemClick() has been passed a view of the item, so

TextView textView=(TextView) view.findViewById(R.id.id_item);
String text=(String)textView.getText();

How about


2022-09-30 11:41

If you have any answers or tips


© 2023 OneMinuteCode. All rights reserved.