0

i have a listview with different components (2 linear layouts and a button) , what i want to do is when i click on a button that's inside one of those linear layouts , i want to access a textview that's inside the other linearlayout , is it possible ?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/listV2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/icon"
            android:layout_width="85dp"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/nom"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/textCname"
                android:textSize="21dp"

                android:textStyle="bold" />

            <TextView
                android:id="@+id/numero"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="12dp"
                android:textColor="@color/textCother"
                android:textStyle="italic" />
            <TextView
                android:id="@+id/ville"
                android:layout_width="match_parent"
                android:textColor="@color/textCother"
                android:layout_height="wrap_content"
                android:textSize="12dp"
                android:textStyle="italic" />
        </LinearLayout>

        <Button
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:visibility="invisible" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            >
            <ImageView
                android:id="@+id/remove"
                android:onClick="contactRemove"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <ImageView
                android:id="@+id/edit"
                android:onClick="contactEdit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
                android:layout_marginLeft="22dp"/>

            <ImageView
                android:onClick="contactCall"
                android:id="@+id/call"
                android:layout_marginLeft="22dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

what i want to do is access the value of android:id="@+id/numero" when i click on one of the image views

Thenad
  • 155
  • 2
  • 11
  • what do you mean by access textview? what are you trying to achieve with textview. – Sahil Apr 30 '18 at 05:44
  • by having multiple elements since i'm using a listview , i want to use that textview's text to access the sqlite database and remove the value associated with it – Thenad Apr 30 '18 at 05:46

2 Answers2

1

You can set OnClickListener interface provided by View class. Something like this.

Implement listener on your activity class

public class SpinnerActivity extends AppCompatActivity implements
    View.OnClickListener

Declare class level variables for your views

private TextView tvNumero;
private ImageView ivRemove, ivEdit, ivContactCall;

Find initialise value in onCreate() method

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

    tvNumero = (TextView) findViewById(R.id.numero)
    ivRemove = (ImageView) findViewById(R.id.remove);
    ivEdit = (ImageView) findViewById(R.id.edit);
    ivContactCall = (ImageView) findViewById(R.id.contactCall);

    ivRemove.setOnClickListener(this);
    ivEdit.setOnClickListener(this);
    ivContactCall.setOnClickListener(this);
}

Implemented class from Onclickistener, where you get on click event for your registered views

@Override
public void onClick(View v) {

    switch (v.getId()) {
        case R.id.remove:
        case R.id.edit:
        case R.id.contactCall:
            Toast.makeText(this, tvNumero.getText().toString(), Toast.LENGTH_SHORT).show();
            break;
    }
}

NOTE : tvNumero.getText().toString() gives you the value of your desired TextView.

UPDATE :

  • Firstly replace your ListView with RecyclerView

Refer RecyclerView Example tutorial.

  • Secondly to achieve onClick for your listed items

Refer recyclerview-onclick tutorial.

buzzingsilently
  • 1,546
  • 3
  • 12
  • 18
1

Pass context when you create your adapter, Use that context to get the inflated view.

Adapter adapter = new Adapter(this);

Then in Adpater Class constructor :

public Adapter(Context context){
context.findViewById(R.id.textview);//consider this as numero textview
}

Then access it as per your requirement. Hope it helps!

Sahil
  • 952
  • 1
  • 7
  • 14