0

I try to do a simple ListView but I can't click on it.

Everything else works fine.

Maybe I am doing something wrong in the setOnItemClickListener?

This is my code:

package com.roa.demolistview;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
private List<Project> mProject = new ArrayList<Project>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_demo_listview);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    populateCarList();
    populateListView();
    listClicked();
}


private void populateCarList() {
    mProject.add(new Project("cool car!!", "very nice", R.drawable.coolcar, Uri.parse("http://roish2.wix.com/roish-2#!אחלה מכונית.jpg/zoom/cy2g/image_1bj2")));
    mProject.add(new Project("cool poster", "nice job!", R.drawable.coolproj, Uri.parse("http://roish2.wix.com/roish-2#!/zoom/cy2g/imagecw0")));
    mProject.add(new Project("Red car!!", "smooth car!", R.drawable.coolredcar, Uri.parse("http://roish2.wix.com/roish-2#!/zoom/cy2g/image1y9x")));
    mProject.add(new Project("A ship!!", "smooth ship!", R.drawable.coolship, Uri.parse("http://roish2.wix.com/roish-2#!2015.jpg/zoom/cy2g/image_1mk3")));
    mProject.add(new Project("A plain!!", "smooth plain!", R.drawable.cooltransport, Uri.parse("http://roish2.wix.com/roish-2#!Transport 3.jpg/zoom/cy2g/image_12gg")));
    mProject.add(new Project("Yellow car!!", "smooth car!", R.drawable.coolyellowcar, Uri.parse("http://roish2.wix.com/roish-2#!/zoom/cy2g/image19cg")));
}

private void populateListView() {
    ArrayAdapter<Project> adapter = new MyListAdapter();
    ListView list = (ListView)findViewById(R.id.listView);
    list.setAdapter(adapter);
}

private void listClicked() {
    ListView list = (ListView)findViewById(R.id.listView);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Project proj = mProject.get(position);
            String message = "you clicked on: " + proj.getName();
            Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
        }
    });
}


private class MyListAdapter extends ArrayAdapter<Project> {
    public MyListAdapter(){
        super(MainActivity.this, R.layout.item_view, mProject);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View itemView = convertView;
        if(itemView == null){
            itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
        }
        //find the project to work with
        Project currentProject = mProject.get(position);

        //fill the view
        ImageView imageView = (ImageView)itemView.findViewById(R.id.imageView);
        TextView nameText  = (TextView)itemView.findViewById(R.id.item_nameText);
        TextView describe = (TextView)itemView.findViewById(R.id.item_descrText);

        imageView.setImageResource(currentProject.getIcon());
        nameText.setText(currentProject.getName());
        describe.setText(currentProject.getDescription());

        return itemView;
    }
 }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Codey
  • 460
  • 1
  • 8
  • 23

3 Answers3

2

You have duplicate of ListView.

Do like this without listClicked method:

private void populateListView() {
    ArrayAdapter<Project> adapter = new MyListAdapter();
    ListView list = (ListView)findViewById(R.id.listView);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Project proj = mProject.get(position);
                String message = "you clicked on: " + proj.getName();
                Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();


            }
        });
    list.setAdapter(adapter);
}
MilanNz
  • 1,323
  • 3
  • 12
  • 29
  • ok, do you see list of Projects and if you see, what's happen when you click on one of the list's items? – MilanNz Nov 10 '15 at 18:42
  • I fined the problem. all I need to do is change the line android:focusable="true" to false in the XML layout. thank man for your help. – Codey Nov 10 '15 at 18:47
0

Your click method doesn't get called, Update your populateListView() as follows and try,

private void populateListView() {
ArrayAdapter<Project> adapter = new MyListAdapter();
ListView list = (ListView)findViewById(R.id.listView);
list.setAdapter(adapter);


list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Project proj = mProject.get(position);
        String message = "you clicked on: " + proj.getName();
        Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();


    }
});
}
Heshan Sandeepa
  • 3,388
  • 2
  • 35
  • 45
0

ok. thank for all, I found the problem. all I need to do is change the line android:focusable="true" to false in the XML layout

thank to all of you!! :)

Codey
  • 460
  • 1
  • 8
  • 23