I have a simple list with 3 items including a button. I want to get the row number of the button. I am not able to get it work. Experts please help.
Here is my activity
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MyListActivity extends ActionBarActivity implements AdapterView.OnItemClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
ListView lv = (ListView) findViewById(R.id.listView);
lv.setOnItemClickListener(this);
String[] countries = new MyCountries().getCountries();
ListAdapter la = new MyListAdapter(this, countries);
lv.setAdapter(la);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this, "Clicked Position " + String.valueOf(position), Toast.LENGTH_SHORT).show();
}
public void onClick(View v)
{
View myView = v;
}
}
My list row
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_weight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="ClickMe"
android:id="@+id/button"
android:layout_gravity="right"
android:layout_weight="1"
android:onClick="onClick" />
</LinearLayout>
List of countries
package com.example.ananth.listviewexample;
import java.util.Locale;
public class MyCountries {
private String[] countries;
public String[] getCountries() {
String [] locales = Locale.getISOCountries();
countries = new String[locales.length];
for (int i = 0; i < locales.length; i ++) {
Locale obj = new Locale("", locales[i]);
countries[i] = obj.getDisplayCountry();
} return countries;
}
public MyCountries()
{
}
}
My list adapter
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;
public class MyListAdapter extends ArrayAdapter <String> {
private final Context context;
private final String[] values;
public MyListAdapter(Context context, String[] values) {
super(context, R.layout.list_row, values);
this.context = context;
this.values = values;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_row, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.editText);
textView.setText(values[position]);
textView = (TextView) rowView.findViewById(R.id.textView);
textView.setText(values[position]);
return rowView;
}
}
I need to get the position either in onItemClick() or onClick()
Thanks in advance.
Adding OnClickListener() for each button while inflating would be overkill as I will have hundreds of rows.