Click on listview item

Asked

Viewed 5,521 times

1

I have the following code, which shows the items in a list, on the screen:

Activity:

public class InviteActivity extends ListActivity implements InviteView
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invite);
        setListAdapter(new InvitePresenter(this));
    }

    @Override
    public Context getContext(){
        return this;
    }
}

Adapter:

public class InvitePresenter extends BaseAdapter {

    private InviteView inviteView;
    private List<UserCommunity> usersList = new ArrayList<UserCommunity>();

    public InvitePresenter(InviteView inviteView){
        this.inviteView = inviteView;
        RequestManager.Users(new FutureCallback<JsonObject>() {
            @Override
            public void onCompleted(Exception e, JsonObject result) {
                usersList =  new Gson().fromJson(result.get("data"), new TypeToken<ArrayList<UserCommunity>>() {}.getType());
                if (usersList != null) {
                    notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public int getCount() {
        return usersList.size();
    }

    @Override
    public Object getItem(int position) {
        return usersList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //recupera o estado da posição atual
        UserCommunity inviteUsers = usersList.get(position);

        //Cria uma instancia do layout .. na view
        LayoutInflater inflater = (LayoutInflater)inviteView.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.item_invite_listview,null);

        TextView txt_Nome = (TextView)view.findViewById(R.id.txt_nome_invite);
        TextView txt_Email = (TextView)view.findViewById(R.id.txt_email_invite);
        TextView txt_Distancia = (TextView)view.findViewById(R.id.txt_distancia_invite);

        txt_Nome.setText(inviteUsers.name);
        txt_Email.setText(inviteUsers.email);
        txt_Distancia.setText(Integer.toString(inviteUsers.distance));

        return view;
    }
}

I would like to implement a click on a list item. Then a new Activity will be opened.

1 answer

2


You’re missing the information from your listview id, and the rest of the Activity code, but here’s an example of how to implement the listview click, you reference it in onCreate and then "arrow" the onItemClickListener, inside you make the code to change activity using intents.

public class InviteActivity extends ListActivity implements InviteView {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invite);
        setListAdapter(new InvitePresenter(this));

        ListView listview = (Listview) findViewById(R.id.listview_forecast);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
             //Seu codigo aqui
             Intent intent = new Intent(this, NovaAtividade.class);
             startActivity(intent);
        }
    }

    @Override
    public Context getContext(){
        return this;
    }

Again, code is not copy and glue, because it depends on some of your variables, but it has a good foundation to get you started.

Link to Onitemclicklistener documentation: https://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html

Link to Intents documentation, so you can better understand how to change activity: https://developer.android.com/reference/android/content/Intent.html

  • Activity code is just this, listview is only xml, I can put it

  • Assemble everything in the presenter, q inherits from the Adapter, and return to the view(Activity)

  • @Henriquemendes but the Click System on the item is in Activity or Listview’s "father" Ragment. There was some doubt?

  • No, I do. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.