Click on listview item

0

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 an item in the list. Then a new activity will open.

    
asked by anonymous 24.11.2016 / 18:12

1 answer

1

The id information of your listview is missing, and the rest of the activity code, but here is an example of how to implement the click on the listview, you reference it in onCreate and then "onItemClickListener" arrow, there 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 paste, because it depends on your variables, but it has a good base for you to start.

Link to the documentation of the OnItemClickListener: link

Link to Intents documentation, so you can better understand how to switch activity: link

    
24.11.2016 / 18:19