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();
}
});
Related
Tooltip on Listview Item click listner don't work
This Library add my project
compile 'com.github.douglasjunior:android-simple-tooltip:0.2.0'
final SimpleTooltip tooltip = new SimpleTooltip.Builder(v.getContext())
.anchorView(v)
.text("Results")
.dismissOnOutsideTouch(true)
.dismissOnInsideTouch(false)
.contentView(R.layout.tooltip,R.id.tv_result_header)
.textColor(getResources().getColor(R.color.green))
.gravity(Gravity.BOTTOM)
.build();
tooltip.show();
ArrayList<Result_ModelList> Result_list = new ArrayList<Result_ModelList>();
Listview lv=(Listview)tooltip.findViewById(R.id.lv);
Result_list.add(new Result_ModelList("Alphabetically"));
Result_list.add(new Result_ModelList("By Code"));
Result_list.add(new Result_ModelList("Last created"));
Result_Adapter adapter = new Sort_Result_Adapter(getApplicationContext(), R.layout.raw_result,Result_list);
lv.setAdapter(adapter);
//**Do not work**
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "do not work" , Toast.LENGTH_SHORT).show();
}
});
Did you see the Usage section?
Listview lv=(Listview)tooltip.findViewById(R.id.lv);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
new SimpleTooltip.Builder(this)
.anchorView(view)
.text("Something")
.gravity(Gravity.END)
.animated(true)
.transparentOverlay(false)
.build()
.show();
}
});
If you want to show it on a particular item on ListView you can use
View v=lv.getChildAt(position);
and use it as anchor view
anchorView(v)
to show it on first item do this:
ArrayList<Result_ModelList> Result_list = new ArrayList<Result_ModelList>();
Listview lv=(Listview)tooltip.findViewById(R.id.lv);
Result_list.add(new
Result_ModelList("Alphabetically"));
Result_list.add(new Result_ModelList("By Code"));
Result_list.add(new Result_ModelList("Last created"));
Result_Adapter adapter = new Sort_Result_Adapter(getApplicationContext(), R.layout.raw_result,Result_list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "do not work" , Toast.LENGTH_SHORT).show();
}
});
View view=lv.getChildAt(0);
SimpleTooltip tooltip = new SimpleTooltip.Builder(v.getContext())
.anchorView(view) //if you want to display on whole listview do .anchorView(lv)
.text("Results")
.dismissOnOutsideTouch(true)
.dismissOnInsideTouch(false)
.contentView(R.layout.tooltip,R.id.tv_result_header)
.textColor(getResources().getColor(R.color.green))
.gravity(Gravity.BOTTOM)
.build();
tooltip.show();
Currently, the code i have only displays the data (on a new activity when clicked) at index 0 of that list view - not the item i actually clicked. i would like to somehow change my code so it can display the data at index n (n being the item i clicked in list view):
ResultsActivity.java
final ArrayList<String> searchResults = getValuesFromJSON(jsonResult);
final ListView listView = (ListView) findViewById(R.id.listView);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, searchResults);
listView.setAdapter(arrayAdapter);
//list items become clickable and open the movie detail page which displays the movie data of the specific movie
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent newActivity = new Intent(ResultsActivity.this, MoviePage.class);
newActivity.putStringArrayListExtra("movie data", searchResults);
startActivity(newActivity);
}
});
}
MoviePage.java
ArrayList<String> searchResults = getIntent().getStringArrayListExtra("movie data");
TextView t = (TextView)findViewById(R.id.textViewfinal);
t.setText(searchResults.get(9));
Just set a onclick listener on your listview so you can listen for item click events.
Put this in your onCreate method in your activity:
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
//you can move the textview to a class variable if you want
TextView t = (TextView)findViewById(R.id.textViewfinal);
t.setText(searchResults.get(position));
}
});
update: you'll want to pass the position to the next activity the same way your passing the searchResults to MoviePage so just do this:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent newActivity = new Intent(ResultsActivity.this, MoviePage.class);
newActivity.putStringArrayListExtra("movie data", searchResults);
newActivity.putExtra("position",position);//pass the position to the next activity
startActivity(newActivity);
}
});
now in MoviePage.java
ArrayList<String> searchResults = getIntent().getStringArrayListExtra("movie data");
int position = getIntent().getIntExtra("position",0);
TextView t = (TextView)findViewById(R.id.textViewfinal);
t.setText(searchResults.get(position));
Try this :
ResultsActivity.java
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent newActivity = new Intent(ResultsActivity.this, MoviePage.class);
newActivity.putExtra("movie data", searchResults.get(position));
startActivity(newActivity);
}
});
MoviePage.java
Bundle extras = getIntent().getExtras();
if (extras != null) {
TextView t = (TextView)findViewById(R.id.textViewfinal);
t.setText(extras.getString("movie data"));
}
Hi I am working with android.I had created a listview using array list. Now how can I add Action to each list item and also how to get the list position of that item. Please help me, thanks
here is my code
public class MainActivity extends Activity {
private ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv=(ListView)findViewById(R.id.listView1);
ArrayList<String> list=new ArrayList<String>();
list.add("aaaa");
list.add("bbbb");
list.add("cccc");
list.add("dddd");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
list);
lv.setAdapter(arrayAdapter);
}
}
You need to implement ListView setOnItemClickListener
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
}
});
For more information go to Documentation.
you can also get the selected item of ListView in setOnItemClickListener using following code:
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
int itemPosition = position;
String itemname = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), "Item: " + itemname + " is at position: " + itemPosition, Toast.LENGTH_LONG).show();
}
});
As the listview cells position and the index of arraylist are same. So we can easily get the item from arraylist by the position of cell of listview.
listView1.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
{
int position=position;
String item=marraylist.get(position).toString();
}
}
I want to create a ListView with a "Add new record" button. I've already created custom adapter for my ListVIew, I have the form of new record (i mean the xml), I've set onClick action on button.
Now I`m wondering how to change the view and the most important question. What should happen after changing view?
For example I have onCreate function:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
NewserDatabase ndb = new NewserDatabase(this);
DataInput di = new DataInput();
ArrayList news = di.getNews();
NewsArrayAdapter naa = new NewsArrayAdapter(NewserActivity.this, R.layout.row, news);
setListAdapter(naa);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View view,
int position, long id) {
Log.i("testy", "I Clicked on Row " + position + " and it worked!");
}
});
}
Now in what method should i manage the new view ? Should it be set in the onItemClick ? Should the activity class change ?
// edit -----------
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
NewserDatabase ndb = new NewserDatabase(this);
DataInput di = new DataInput();
ArrayList news = di.getNews();
NewsArrayAdapter naa = new NewsArrayAdapter(NewserActivity.this, R.layout.row, news);
setListAdapter(naa);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View view,
int position, long id) {
Log.i("testy", "I Clicked on Row " + position + " and it worked!");
Intent formIntent = new Intent(NewserActivity.this, NewsAddActivity.class);
NewserActivity.this.startActivity(formIntent);
}
});
}
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]);
}