I have a CHOICE_MODE_SINGLE list with 3 rows. How do I place an onClick event that changes the value of an arbitrary variable, so that if Row 1 is clicked, say, the value of X = 1, and when Row 2 is clicked, the value of X = 2, etc.?
public class List extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, GENRES));
final ListView listView = getListView();
listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
private static final String[] GENRES = new String[] {"Barre", "Buffumville","Hodges"};
}
Its quite simple..rather than extending ListActivity extend it by Activity and just declare listview in your XML file as below:
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
initialize listview in your activity:
ListView listView=(ListView) findViewById(R.id.list);
And after setting adapter as you did,set the item click listener of the listview as below in your code:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
Toast.makeText(getBaseContext(), GENRES[position], Toast.LENGTH_SHORT).show();
}
});
By doing this you will get the desired value of that position of listview.
You will surely get the result you want.
You want to set the onItemClickListener on the list view.
listView.setOnItemClickListener(
new OnItemClickListener(
#Override
onItemClick(AdapterView<?> listView, View cell, int position, long id) {
// Code to change variables
}
});
You can implement protected void onListItemClick (ListView l, View v, int position, long id) in your ListActivity, and the position variable lets you know which item was clicked.
Related
I have generated a list using array adapters and listview. Now i want a XML layout to open up each time i click on a list item. The layout format of all XMLs should be same,but the data in each XML should be different(for different list items).
How do i go about it?
Here is the code
public class handgunsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_handguns);
ArrayList<String> hg=new ArrayList<String>();
hg.add("M1911");
hg.add("Desert Eagle .50");
hg.add("Glock 17");
hg.add("Sig P226");
hg.add("Browning High-Power");
ArrayAdapter<String> item=new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,hg);
final ListView list=(ListView)findViewById(R.id.root);
list.setAdapter(item);
Add item click listener to your list, then get the selected item and pass it where ever you want to
list.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// Here is the data, pass it any where
String item = (String) list.getItemAtPosition(position);
}
});
I am having a layout consisting of ListView and a label. Look at the image below.
I implemented listview using base adapter in a seperate .java file.
Could anyone suggest me how can i set text to the label on click of list item?
EDITED :
The text of the label should be number of list items clicked.
Suppose i clicked a button in a list item, the label should be set to 1.
Similarly in the next attempt if i clicked another list item's button it should be set to 2 and so.. on..
Sir,
What you are saying is this, you have your adapter in one class and the activity in another file. Well you could do this, to update the textview.
pass the context to the activity, and if its in the adapter you are maintaining the count then once the count has been updated,
then, assume you have this method in the activity
public void updateTextView(int count) {
// enter your code here to set the count in the textview
}
and from the baseAdapter call the above method like this:
if(mContext != null) {
((YourActivity)mContext).updateTextView(mCount);
}
and the textview in the activity will be updated!
I hope it helps.
In your OnItemClickListener call adapter.getItem(int position) to retrieve the object from the collection backing your BaseAdapter. From there, you should be able to retrieve any fields you need.
Edit:
Your edit clears up the question. Updated answer:
private int mCounter = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ListView listView = getYourListView();
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mCounter++;
updateTextView();
}
});
if(savedInstanceState != null) {
mCounter = savedInstanceState.getInt("counter", 0);
}
updateTextView();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("counter", mCounter);
}
private void updateTextView() {
// TextView textView = getYourTextView();
textView.setText(String.valueOf(mCounter));
}
Just OnItemClickListener in your ListView then in the onClick you will get a value arg2 which is the position of the item which is clicked.
Just get that value from the ArrayList from which you are displaying the ListView and show it...
Hope this is what you need if I have not misunderstood your question.
try this code
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String itemText=(String) arg0.getItemAtPosition(arg2);
yourLable.setText(itemText);
}
});
In onListItemClick call list.getItemAtPosition(position) to retrieve item text
then set this text to textview1.setText(listText).
First Implement your onItemClickListener, where you will get int arg2 parameter, which is position, so get that position and do your stuff whatever you want like below.
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
LABLE.setText(""+arg2);
}
});
Check this code
public class ListA extends ListActivity {
private TextView selection;
private static final String[] items={"Item 1", "Item 2", "Item 3"};
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_list);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
items));
selection=(TextView)findViewById(R.id.selection);
}
#Override
public void onListItemClick(ListView parent, View v, int position,
long id) {
selection.setText(position);
}
}
I used list view to list the items of my SQLite database as follows:
listView = (ListView) findViewById(R.id.listView1);
db.open();
Cursor c = db.getAllHistory();
ArrayList<String> temparr = new ArrayList<String>();
if (c.moveToFirst()) {
do {
temparr.add(c.getString(0)+'\t'+c.getString(1)+'\t'+c.getString(2)+'\t'+c.getString(3)+'\t'+c.getString(4)+'\t');
} while(c.moveToNext());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, temparr);
listView.setAdapter(adapter);
} else {
db.close();
}
Now I want to access the specific list item by using OnItemClickListener. How should i do it?
To get your perticular data-
Implement setOnItemClickListener()-
and position will return list's item position number:-
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String yourData = temparr.get(position);
}
});
Just call listView.setOnItemClickListener() with your implementation of the listener.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// your code
// Toast.makeText(context,temparr.get(position),Toast.LENGTH_SHORT).show();
}
});
parent -> The AdapterView where the click happened.
view -> The view within the AdapterView that was clicked (this will be a view provided by the adapter)
position -> The position of the view in the adapter.
id->The row id of the item that was clicked.
You can set the listener like this:
Put this code in onCreate()
TextView disp = (TextView) findViewById(R.id.textView1);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String temp = (String) listView.getItemAtPosition(position);
disp.setText(temp);// display on screen
}
});
EDIT:
As you want to display the listItem on screen in a permenant sort of way rather than displaying a toast, the easiest way that you can do this is by adding a TextView in your layout. See the updated code
Hope this helps.
Usually I set my ItemClickListener using
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
long x = MyActivity.this.myAdapter.getItemId(position); //to get the clicked item position
}
});
After setting my ListView etc.
Now I have to work on a code that use the list with default Android list adapter in this way
public class ListDefs extends ListActivity {
private Defs defs;
private ArrayAdapter<String> adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
defs = Note.defs;
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, defs.lines);
setListAdapter(adapter);
}
}
How to set a OnItemClickListener() in this scenario?
ListActivity has both a getListAdapter and getListView methods.
In your onCreate, after you call
setContentView(R.layout.list_activity);
where R.layout.list_activity is your layout file for the activity that has a ListView tagged with the android list id like so (according to the docs)
<ListView
android:id="#android:id/list"
... />
you can call
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
...
}
});
From the docs
ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "#android:id/list" (or list if it's in code)
I want to get the text element that makes up my ListView items.
myList = (ListView) findViewById(R.id.list_view);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, partialNames);
myList.setAdapter(adapter);
I am not extending ListActivity so I can't call a simple onListItemClick function.
I have tried using the following:
myList.setOnItemClickListener(adapter.?)
, but I don't know what parameters I need for the constructor
How can I capture the string value in a particular ListView item when the user clicks on the row?
Make your Activity implement OnItemClickListener, and then:
myList.setOnItemClickListener(this);
or if you dont want to implements OnItemClickListener, you can do:
myList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
// TODO Auto-generated method stub
}
});