When looking through the list of the given position is not correct. How to convey that I needed the position?
method getItemId(position) did not help!
There is some sample code:
lv1 = (ListView) findViewById(R.id.list);
inputSearch = (EditText) findViewById(R.id.inputSearch);
final String listcode[] = getResources().getStringArray(R.array.list);
adapter = new ArrayAdapter<String>(this, R.layout.list_item,
R.id.product_name, listcode);
lv1.setAdapter(adapter);
lv1.setTextFilterEnabled(true);
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
long line = adapter.getItemId(position);
Integer itemname = new Integer((int) line);
Intent intent = new Intent();
intent.setClass(Main.this, ShowKnot.class);
Bundle b = new Bundle();
b.putString("defStrID", itemname.toString());
b.putString("defStrName", listcode[itemname]);
intent.putExtras(b);
startActivity(intent);
}
});
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
Main.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
Instead of
long line = adapter.getItemId(position);
you can use "position" in the method arguement returns the list item position number
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
a.getItemAtPosition(position);
here "int position" parameter is the index of the listitem.
Related
I was looking to filter a listView by an editText.
My ListView is populate by an ArrayString, and when I filter it everything goes right. But I have a problem to recognize the element clicked. When I filter the listView and I click on the item filtered I want to have the number of the row in the original arrayString in order to make an Intent. Whit my code when I click the item in the filtered arrayString the row is 0 and it's not correct. If I search "Anche Se Pesa" I want to return the row 2 like the position in the original ArrayString.
<string-array name="titoli">
<item>An Val Dondona</item>
<item>Anche Mio Padre</item>
<item>Anche Se Pesa</item>
<item>Andremo In Francia</item>
<item>Aprite Le Porte</item>
<item>Arda Berghem</item>
<item>Ascoltate Amici Cari</item>
<item>Ave, O Vergine</item>
<item>Aver na Figlia Sola Soletta</item>
<item>Ancora sti quatro</item>
<item>Andouma Prou</item>
</string-array>
Here Is the code
public class activity_titoli extends AppCompatActivity {
Button btt_backHome;
ListView lV_titoli;
EditText eT_search;
private ArrayAdapter<CharSequence> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_titoli);
btt_backHome = (Button) findViewById(R.id.btt_backHome);
lV_titoli = (ListView) findViewById(R.id.lV_titoli);
eT_search = (EditText) findViewById(R.id.eT_search);
adapter = ArrayAdapter.createFromResource(this, R.array.titoli, android.R.layout.simple_list_item_1);
lV_titoli.setAdapter(adapter);
final Intent to_Home = new Intent (this , Activity_Main.class);
eT_search.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
(activity_titoli.this).adapter.getFilter().filter(arg0);
}
});
btt_backHome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(to_Home);
}
});
lV_titoli.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.e("Element: " , Integer.toString(i));
}
});
}
}
Try this code :
lV_titoli.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Resources res = getResources();
List<CharSequence> list = Arrays.asList(res.getStringArray(R.array.titoli));
Log.e("Element: " , Integer.toString(list.indexOf(adapter.filgetItem(i))));
}
});
Hope this helps
I have created a list view in android and add edit text above the list and when the user enter text the list will be filtered according to user input but notworking :
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
MainActivity.this.myadpt.getFilter().filter(arg0);
}
});
try this one:
adapter = new ListViewAdapter(this, arraylist);
list.setAdapter(adapter);
editsearch = (EditText) findViewById(R.id.search);
editsearch.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable arg0) {
String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
adapter.filter(text);
}
});
see this one: List View Filter Android
I have a spinner in my code and I want to get position of element in the array when selected. Here is my code which is running perfectly. In selection I am storing the string value of element but I also want the position count of element
public class MainActivity extends Activity implements OnItemSelectedListener {
final Context context = this;
private Button button;
private String selection;
private String[] states = new String[]{
"Gujrat","Jammu and Kashmir","Kerala","Karnataka","Lakshadweep","Maharashtra","Manipur","Mizoram",
"Nagaland","New Delhi","Rajasthan","Tami Nadu","West Bengal"
};
ArrayList<String> categoryList = new ArrayList<String>();
private static final String TAG = "MyActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int currentMonth = Calendar.getInstance().get(Calendar.MONTH)+1;
//make it fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//fix portrait orientation
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.menu);
Button dialogButton = (Button) dialog.findViewById(R.id.btncross);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
Spinner spin = (Spinner)dialog.findViewById(R.id.spinState);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, states);
adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter_state);
spin.setOnItemSelectedListener(MainActivity.this);
}
});
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
TextView tv = (TextView)arg1;
selection = tv.getText().toString();
Log.v(TAG, "index=" + selection);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
In the code:
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
TextView tv = (TextView)arg1;
selection = tv.getText().toString();
Log.v(TAG, "index=" + selection);
}
arg2 is the position of spinner
int arg2 of `onItemSelected()` is the position of selected items.
So you can use it like
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
selection = states[arg2];
Log.v(TAG, "index=" + arg2);
Log.v(TAG, "selction=" + selection);
}
spin.getSelectedItemPosition();
Here in your setOnItemSelectedListener(new OnItemSelectedListener()
public abstract void onItemSelected (AdapterView<?> parent, View view, int position, long id)
Parameters
parent The AdapterView where the selection happened
view The view within the AdapterView that was clicked
position The position of the view in the adapter
id The row id of the item that is selected
int arg2 to get Selected Item Id
Update only this method,
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Log.v(TAG, "index=" + spin.getItemIdAtPosition(int2));
}
this is my mainactivity
TestAdapter mdh = new TestAdapter(this);
mdh.open();
ArrayList<String> songs = mdh.getAllSongs();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, songs);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.autocomplete_songs);
textView.setAdapter(adapter);
textView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
As you can see.. adapter has all the songs and pass down to the autocompletetextview
what i want is when i click an item on the autocompletetextview..i will get its index number and store it on an integer..i want its index from the arraylist and not its index from the dropdown..please help me...
TestAdapter mdh = new TestAdapter(this);
mdh.open();
ArrayList<String> songs = mdh.getAllSongs();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, songs);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.autocomplete_songs);
textView.setAdapter(adapter);
textView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.i("GiveTag", arg2); // it will give you what ever item clicked position /index
}
});
Try this way
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
String str = songs.get(pos);
}
});
Check if this works
textView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String text = textView.getText().toString();
int position = songs.indexOf(text);
}
});
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Object data = arg0.getItemAtPosition(arg2);
int pos = java.util.Arrays.asList(songs).indexOf(data);
}
});
I've managed to implement a great listview that I found here http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/comment-page-1/
but I can't seem to add an onclicklistener
I just want to be able to do an action when I click on the row, with the data that the row contains of course
any ideas?
thanks
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.liste);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Setup the list view
final ListView prestListView = (ListView) findViewById(R.id.list);
final prestationAdapterEco prestationAdapterEco = new prestationAdapterEco(this, R.layout.prestation);
prestListView.setAdapter(prestationAdapterEco);
// Populate the list, through the adapter
for(final prestationEco entry : getPrestations()) {
prestationAdapterEco.add(entry);
}
prestListView.setClickable(true);
prestListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Object o = prestListView.getItemAtPosition(position);
String str=(String)o;//As you are using Default String Adapter
Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();
}
});
}
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object o = prestListView.getItemAtPosition(position);
prestationEco str = (prestationEco)o; //As you are using Default String Adapter
Toast.makeText(getBaseContext(),str.getTitle(),Toast.LENGTH_SHORT).show();
}
});
If your Activity extends ListActivity, you can simply override the OnListItemClick() method like so:
/** {#inheritDoc} */
#Override
protected void onListItemClick(ListView l, View v, int pos, long id) {
super.onListItemClick(l, v, pos, id);
// TODO : Logic
}
The prestListView.getItemAtPosition(position); returns the UI widget: Text, Icon, ...
Try this instead:
Object o = prestationAdapterEco.getItemAtPosition(position);
or
Object o = arg0.getItemAtPosition(position);
Get the object from the adapter. Not from the list-view.
2.
Object o is a prestationEco object. Not a String.
list.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
You are doing
Object o = prestListView.getItemAtPosition(position);
String str=(String)o;//As you are using Default String Adapter
The o that you get back is not a String, but a prestationEco so you get a CCE when doing the (String)o
Try this:
list.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});