I am trying to send an item position from a Fragment to an Activity, but I always get invoiceId 0 every time I select an item in listView.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent seperateView = new Intent(rootView.getContext(),SeperateViewForDeliveryList.class);
seperateView.putExtra("invoiceId", listView.getItemAtPosition(position).toString());
startActivity(seperateView);
}
});
listView.setAdapter(deliveryListAdapter);
return rootView;
In activity class
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seperate_view_for_delivery_list);
invoiceId = getIntent().getExtras().getString("invoiceId");
Toast.makeText(this, "invoiceID " + invoiceId, Toast.LENGTH_SHORT).show();
}
Use this
seperateview.putExtra("invoiceId",String.valueOf(position));
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent seperateview=new Intent(rootView.getContext(),SeperateViewForDeliveryList.class);
seperateview.putExtra("invoiceId",parent.getItemAtPosition(position).toString());
startActivity(seperateview);
}
});
listview.setAdapter(deliveryListAdapter);
return rootView;
Try this
getItemAtPosition returns an object of the data associated with the specified position in the list, while position itself is what you want.
Change your code to
seperateView.putExtra("invoiceId", String.valueOf(position));
We can also pass integer value in an Intent. Try this:
seperateview.putExtra("invoiceId",listview.getItemAtPosition(position));
getIntent().getIntExtra("invoiceId",0);
Related
error screenshot
I am trying to make clickable items in ListView, and recivieng this kind of error.
ListView chHE = (ListView) findViewById(R.id.lvHE);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.support_simple_spinner_dropdown_item, cities);
chHE.setAdapter(adapter);
chHE.getOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long l) {
if (position == 0) {
toast.show();
}
}
});
Error:(52, 13) error: method getOnItemClickListener in class AdapterView
cannot be applied to given types;
required: no arguments
found:
reason: actual and formal argument lists differ in length
where T is a type-variable:
T extends Adapter declared in class AdapterView
chHE.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
if(position==0){
toast.show();
}
}
});
maybe you have write in a mastake?change get=>set
you should remplace getOnItemClickListener to setOnItemClickListener
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
String value = (String)adapter.getItemAtPosition(position);
// assuming string and if you want to get the value on click of list item
// do what you intend to do on click of listview row
}
});
Try doing this
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//doSomeThing
}
});
I have a fragment which extends ListFragment.
In it I use an ArrayAdapter
public class MyFragment extends ListFragment {
public void onStart() {
super.onStart();
List<String> list = new ArrayList<String>();
list.add("Superman");
list.add("Batman");
arrayAdapter = new ArrayAdapter<OptionToStringAdapter>(
getActivity(),
android.R.layout.simple_list_item_1,
list);
getListView().setAdapter(arrayAdapter);
getListView().setOnItemClickListener(new ModifyOption(this));
setListShown(true);
}
When the user taps an item I want to modify the text.
How do I modify the text?
public class ModifyOption implements OnItemClickListener {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
???What goes here????
}
}
Assuming you want to change the content of the list item you clicked.
First, change the value using a dialog or widget of your choice.
Second, change the value of the item at the position in the list by using 'position' argument.
Third, call notifyDataSetChanged() method of the adapter object.
public class ModifyOption implements OnItemClickListener {
public void onItemClick(AdapterView parent, View view, int position, long id) {
// create a dialog or other widget
String newValue = "your_new_value";
list.set(position,newValue);
arrayAdapter.notifyDataSetChanged(); //informs views that data has been changed
}
}
try with below code:
public class ModifyOption implements OnItemClickListener { public void onItemClick(AdapterView parent, View view, int position, long id) {
arrayAdapter.remove(arrayAdapter.getItem(position));
arrayAdapter.insert("new text",position);
} }
This seems to do the trick. I don't know if this is an appropriate way though.
public void onItemClick(AdapterView parent, View view, int position, long id) {
TextView textview = (TextView) view.findViewById(android.R.id.text1);
textview.setText("Spiderman");
}
how to make listView clickable in one list and not clickable in other list if value is null or empty
ListView lv = getListView();
if (TAG_UID.isEmpty() && TAG_UID == null)
{
lv.setEnabled(false);
}
else
{
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
Intent in = new Intent(AllProductsActivity.this,
MyActivity2.class);
startActivity(in);
}
});
value get from mysql database, thanks for help
For the position you want make it non-clickable handle it in this manner using return statement
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position==2){
return;
}else{
//put your code here
}
}
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 have created one listview of some names,what i need is when i will click selected row it will go to that page only,on click on different row it will move to the same class but different content.I think it will move by question id.could anybody help me how to pass the question id Or any other method to do this..
here is my code..
private OnItemClickListener mlist = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
}
};
You can try something like this -
private OnItemClickListener mlist = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
if(Some condition)
{
Intent i= new Intent(YourActivity.this,ActivityOne.class);
// To pass data
i.putExtra("SomeId", someValue);
startActivity(i);
}
else if(Some other condition)
{
Intent i= new Intent(YourActivity.this,SecondActivityTwo.class);
startActivity(i);
}
else
{
// Do something else--
}
}
};
And in the other activity -
String identifier = getIntent().getExtras().getString("SomeId");
Here I've given an example assuming you have user list and clicking on item you want to show user profile...
In List_Act activity...
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = mInflater.inflate(R.layout.rowitem,parent,false);
convertView.setTag(UserId);
}
private OnItemClickListener mlist = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent i=new Intent(List_Act.this, Profile_Act.class);
int UserId = ((View)v.getParent()).getTag();
i.putExtra("UserId", UserId); //Setting variable you want to pass to another activity
startActivity(i);
}
};
in Profile_Act activity in onCreate()
String UserId = getIntent().getExtras().getString("UserId"); //retrieving value in another activity
now you'll have UserId variable set and you can use it...