Access an element of an XML that is not the one of the activity_main

0

I happen to have a ListView in the main, and within that ListView another ListView with its corresponding xml, I would like to apply an event to the internal ListView from the main but I have problems when locating the element, then I show you

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    ListView listV2;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.row_adapter, null);

        listV2 = view.findViewById(R.id.listViewJugador);

        listV2.setOnItemClickListener(this);

        }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Toast.makeText(getApplicationContext(),
                "HOLAAAAAAA?" , Toast.LENGTH_LONG)
                .show();
    }
  

XML WHOM I WANT TO ACCESS

<ListView
    android:id="@+id/listViewJugador"...

The event does not respond, does absolutely nothing

  

UPDATE (The element is captured correctly by the Inflate Layout but does not recognize the event)

package com.example.mtx.listviewlugares;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ArrayList<ciudad> ciudades = new ArrayList<>();
    TextView txt;
    Button bt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txt = (TextView) findViewById(R.id.editText) ;
        bt = (Button)findViewById(R.id.button);


        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ciudades.add(new ciudad(txt.getText().toString()));
                adapterListView adapter = new adapterListView(getApplicationContext(), ciudades);
                ListView listV = (ListView)findViewById(R.id.lv);
                listV.setAdapter(adapter);
                adapter.notifyDataSetChanged();

            }
        });

        View v = LayoutInflater.from(this).inflate(R.layout.row_view, null);
        Button b = (Button)v.findViewById(R.id.button3);

       b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "This is a message displayed in a Toast", Toast.LENGTH_SHORT).show();
                TextView tx =(TextView) v.findViewById(R.id.editText4);
                tx.setText("LOLLLL");
            }
        });
    }
}
    
asked by Rafael Valls 25.11.2018 в 09:33
source

1 answer

1

The OnClickListener does not work because you are setting it in an instance of the layout that is never displayed on the screen. You do this:

View v = LayoutInflater.from(this).inflate(R.layout.row_view, null);
Button b = (Button)v.findViewById(R.id.button3);

but that "v" view does not use it later to show it or add it anywhere.

I suppose that within the Adapter you should also be using the layout row_view , but it is a different instance to which you set the OnClickListener.

To solve this problem, what you should do is to pass the OnClickListener as a parameter to your adapter:

//Instancia este onClickListener dentro del MainActivity:
mOnClickListener = new View.OnClickListener() {
   @Override
   public void onClick(View v) {
      Toast.makeText(getApplicationContext(), "This is a message displayed in a Toast", Toast.LENGTH_SHORT).show();
      TextView tx =(TextView) v.findViewById(R.id.editText4);
      tx.setText("LOLLLL");
   }
};

//En donde creas el adapter pasale el mOnClickListener como un nuevo parametro
adapterListView adapter = new adapterListView(getApplicationContext(), ciudades, mOnClickListener);

Now edit your adapter's constructor to receive this OnClickListener:

ChatAdapter(Context context,ArrayList<ciudad> ciudades,View.OnClickListener mOnClickListener) {
   ...
   //Guarda el mOnClickListener como una propiedad del adapter
   this.mOnClickListener = mOnClickListener;
}

Finally inside the Adapter in the onCreateViewHolder set the OnClickListner to the correct view:

@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View v = inflater.inflate(R.layout.row_view, parent,false);
    Button b = (Button)v.findViewById(R.id.button3);
    b.setOnClickListener(mOnClickListener);
    return new MyHolder(item);
}

I also recommend that you start with capital letters the name of the classes as ciudad or adapterListView since the instances are written in lowercase.

    
answered by 25.11.2018 в 19:36