I have implemented an activity with a simple ListView
. In the java
file, I have implemented OnItemClickListener()
to respond to ListView
item clicked.
public class MainActivity extends Activity
{
private String value;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.home_screen);
value = "Hello World";
final ListView listview = (ListView)findViewById(R.id.listview);
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ListValues);
listview.setAdapter(aa);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
{
// Get Item Clicked
String ItemSelected = (String)listview.getItemAtPosition(position);
// Access "value" containing "Hello World"
}
}
}
I need to access member variable value
of my MainActivity
class in onItemClick()
. Kindly tell me how to do it.
Simply use:
MainActivity.this.value;
Or, even simpler, just as if it was a member of the OnClickListener:
value
Will also work.
you declared it globally so you can use it directly
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
{
//Get Item Clicked
String ItemSelected = (String)listview.getItemAtPosition(position);
Toast.makeText(getApplicationContext,value,Toast.LENGTH).show();
^
here
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.