MayurCM
MayurCM

Reputation: 701

When clicking on a row Cannot find the index or position in listview using arrayadapter

I have created a listview using following tutorial link http://www.ezzylearning.com/tutorial.aspx?tid=1763429

Outcome of this is, List of Activity as below image enter image description here

Now, I want to navigate to different activity screen by clicking on individual row. I do have idea of navigating to next screen, However I'm not able to find the correct position of each row. I don't have much knowledge of android so please bare with my question if it is too silly.

although the code is available on the above link I'm attaching the .xml files and .java. here as well.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"> 

     <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

listview_item_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp">

     <ImageView android:id="@+id/imgIcon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="15dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />

     <TextView android:id="@+id/txtTitle"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:textStyle="bold"
        android:textSize="22dp"
        android:textColor="#000000"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />

</LinearLayout>

Weather.java

public class Weather {
    public int icon;
    public String title;
    public Weather(){
        super();
    }

    public Weather(int icon, String title) {
        super();
        this.icon = icon;
        this.title = title;
    }
}

WeatherAdapter.java

public class WeatherAdapter extends ArrayAdapter<Weather>{

    Context context; 
    int layoutResourceId;    
    Weather data[] = null;

    public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        WeatherHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new WeatherHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

            row.setTag(holder);
        }
        else
        {
            holder = (WeatherHolder)row.getTag();
        }

        Weather weather = data[position];
        holder.txtTitle.setText(weather.title);
        holder.imgIcon.setImageResource(weather.icon);

        return row;
    }

    static class WeatherHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
    }
}

MainActivity.java

public class MainActivity extends Activity {

    private ListView listView1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Weather weather_data[] = new Weather[]
        {
            new Weather(R.drawable.weather_cloudy, "Cloudy"),
            new Weather(R.drawable.weather_showers, "Showers"),
            new Weather(R.drawable.weather_snow, "Snow"),
            new Weather(R.drawable.weather_storm, "Storm"),
            new Weather(R.drawable.weather_sunny, "Sunny")
        };

        WeatherAdapter adapter = new WeatherAdapter(this, 
                R.layout.listview_item_row, weather_data);


        listView1 = (ListView)findViewById(R.id.listView1);

        View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
        listView1.addHeaderView(header);

        listView1.setAdapter(adapter);
    }

any help will be appreciated Thanks

Upvotes: 0

Views: 677

Answers (5)

Vipul
Vipul

Reputation: 28093

You need to attach OnItemClickListener

listView1.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView <?> adapterView, View view, int position, long id) {
        // position is index of the row that was clicked
    }
});

Upvotes: 4

Md Abdul Gafur
Md Abdul Gafur

Reputation: 6201

Try this way...

listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> arg0, View arg1,
                                int position, long arg3) {


    Toast.makeText(context, position+"", Toast.LENGTH_LONG).show();

                                Intent go_detail_Activity_Intent= new Intent(context,Target_Activity.class);
                                go_detail_Activity_Intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                context.startActivity(go_detail_Activity_Intent);


                        }
                    });

I think it help you.

Upvotes: 1

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

use setOnItemSelectedListener with list http://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html

listView1.addHeaderView(header);
listView1.setAdapter(adapter);

listView1.setOnItemSelectedListener(
    new OnItemSelectedListener() {
        @Override
        public void onItemSelected(
            AdapterView<?> parent, View view, int pos, long id
        ) {
             //where pos is your index 
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {}

    }
);

Upvotes: 1

Nermeen
Nermeen

Reputation: 15973

You should do something like this

listView1.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //do something
        }
    });

Upvotes: 1

Samir Mangroliya
Samir Mangroliya

Reputation: 40416

where is listView1.setOnItemClickListener ? have you set setOnItemClickListener to listView1?.and in below code onItemClick have position.

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

            }
        });

Upvotes: 1

Related Questions