I have designed a custom listview. In that view i have one textview and two edittext fields. when i am clicking on the individual rows in listview that perform another activity(i.e another page will open). But i cannot perform onclick action on my listview.
i have wrote some code but its not working. i have shown that code as below.
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.sharefolioedit);
getList();
lv.setAdapter(new EfficientAdapter(this));
lv = getListView();
lv.setClickable(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentView, View childView,
int position, long id) {
Intent intent = new Intent(CategoryList.this,AddSubCategoryList.class);
startActivity(intent);
}
}
This is working fine for me :
this.setListAdapter(new EfficientAdapter(this));
this.getListView().setOnItemClickListener ( new AdapterView. OnItemClickListener ( )
{
public void onItemClick ( AdapterView <?> aView, View v, int position, long id )
{
Intent intent = new Intent(CategoryList.this,AddSubCategoryList.class);
startActivity(intent);
}
});
Related
Im stuck. I have a listView item that when pressed calls an other activity. But the activity is being called twice. Once when the list item is pressed and once when it is released.
I looked for examples for filtering the Motion event on a listView item but couldn't find any. I tried to manually add MotionEvent event to the onItemClick() arguments, but that failed.
Can anyone provide an example?
Current Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
generateListView();
}
public void generateListView(final JSONArray relevantGames, ArrayList listViewItems, String teamName) {
ArrayAdapter<String> adapterList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listViewItems);
final ListView lv = findViewById(R.id.gameList);
lv.setVisibility(View.VISIBLE);
lv.setAdapter(adapterList);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Want To Put a motion down check
selectedGame = relevantGames.getJSONObject(position);
//Start new intent with putExtra(selectedGame)
});
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_campaign_list);
Campaign campaign_data[] = new Campaign[]
{
new Campaign(R.drawable.hlbb, "MSIG HLBB PA Extra"),
new Campaign(R.drawable.hlbb, "MSIG HLBB SSP Plus")
};
CampaignAdapter adapter = new CampaignAdapter(this, R.layout.listview_header_row, campaign_data);
listView1 = (ListView)findViewById(R.id.listView1);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
I can run the code above without setonitemclicklistener function, when i add the setOnItemClickListener, it stopped working.What is the error?? I'm new in Android..Thx
I don't know how is your CampaignAdapter code, but you are trying to cast the row view which you are inflating into TextView. You should use your adapter to access the item using the position of element:
CampaignAdapter adapter = new CampaignAdapter(this, R.layout.listview_header_row, campaign_data);
listView1 = (ListView)findViewById(R.id.listView1);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Campaign item = (Campaign) adapter.getItem(position);
// Now you can access to the campaing value that you want
// For instance, item.getText()
Toast.makeText(getBaseContext(), item.getText(), Toast.LENGTH_LONG).show();
}
});
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.
I use the below code to create listview without listactivity.I need to know how to get the selected list item from this.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] myList = new String[] {"Hello","TEST","TEST","TEST4"};
ListView lv = new ListView(this);
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myList));
setContentView(lv);
}
Use onItemClickListener
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
http://d.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener%28android.widget.AdapterView.OnItemClickListener%29
set listener on the list
lv.setOnItemSelectedListener(this);
then implement it
public void onListItemClick(ListView parent, View v, int position, long id) {
selection.setText(items[position]);
}
I have a list view with 15 items. When I click on any item I want to change the screen(Intent). how can I change the activity on item selected in android?
any tutorial or source code?
You can use ListView's setOnItemClickListener, and start an new Activity in your implementation of this method. Following is sample code:
myListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id){
// Start your Activity according to the item just clicked.
}
});
final ListView list = (ListView) findViewById(R.id.SCHEDULE);
protected void onCreate(Bundle savedInstanceState) {
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
Toast.makeText(getApplicationContext(),"hiihih",Toast.LENGTH_SHORT).show();
}
});
}
Check the selected answer in ListView OnItemClickListener Not Responding?
If you also need code examples to change activity, head to https://developer.android.com/guide/index.html and start reading.
// Prepare intent
Intent newActivity = new Intent(this, NewActivity.class);
// start activity
startActivity(newActivity);