Reading value from String Array populated using ListView - java

I've got a ListView which populates a list of quotes from a String Array, however onClick I require the selected <item> to be read to a String. This is the code I use:
ListView sample = (ListView)findViewById(R.id.Samplelist);
String[] backup = getResources().getStringArray(R.array.months);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, backup);
sample.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
view.animate().setDuration(2000).alpha(0)
.withEndAction(new Runnable() {
#Override
public final void run()
{
String readItem;
finish();
}
});
}
sample.setAdapter(adapter);
}
And here sample.setAdapter(adapter) seems to be an error and is underlined with red.
Is there a way to get this done correct? Any help will be appreciated

If you look at layout of simple_list_item_1 then you can find id of textview is text1.
You can define OnItemClickListener to your listview like this:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = ((TextView) view.findViewById(R.id.text1)).getText().toString();
}
});
EDIT:
You can also try
String selectedItem = (String)parent.getItemAtPosition(position);
instead of
String selectedItem = ((TextView) view.findViewById(R.id.text1)).getText().toString();
Hope it helps!

You can try this approach:
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
String data=(String)parent.getItemAtPosition(position);
}
});

You may need to parse the text from View in OnItemClickListener
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItemText = ((TextView)view).getText().toString();
}
});

Related

How to debug the error "String resource ID #0x0"

I want to get the value of the selected item when I select the item in the list view.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String data = getText(position).toString();
Toast.makeText(Main2Activity.this,data,Toast.LENGTH_SHORT).show();
}
});
In this code, String data = getText(position).toString(), I get this error:
a String resource ID #0x0
This code is add listview
void DBadd() {
String name = addTxt1.getText().toString();
String info = addTxt2.getText().toString();
if (name.equals("") || info.equals("")) {
Toast.makeText(getApplicationContext(), "정보를 입력해 주세요", Toast.LENGTH_SHORT).show();
return;
} else {
db.execSQL("INSERT INTO tableName VALUES (null, '" + name + "', '" + info + "');");
Toast.makeText(getApplicationContext(), "추가 성공", Toast.LENGTH_SHORT).show();
addTxt1.setText(""); //입력시 EditText에 입력된값 지움
addTxt2.setText("");
cursor = db.rawQuery("SELECT * FROM tableName", null);
startManagingCursor(cursor); //엑티비티의 생명주기와 커서의 생명주기를 같게 한다.
adapter = new ArrayAdapter<String>(Main2Activity.this, android.R.layout.simple_list_item_1);
adapter2 = new ArrayAdapter<String>(Main2Activity.this, android.R.layout.simple_list_item_1);
while (cursor.moveToNext()) {
adapter.add(cursor.getString(1));
adapter2.add(cursor.getString(2));
}
listView.setAdapter(adapter);
listView2.setAdapter(adapter2);
}
}
How do I get the value of a selected item?
Try this, it works for me:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String data = listView.getItemAtPosition(position).toString();
Toast.makeText(Main2Activity.this,data,Toast.LENGTH_SHORT).show();
}
});
Or, just fetch the string value directly from adapter using .get(postion) like that :
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String data = adapter.get(position);
Toast.makeText(Main2Activity.this,data,Toast.LENGTH_SHORT).show();
}
});
Good Luck
I'm not sure did i understood your question correctly, but let me guess the desired behavior:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textViewOfTheClickedRaw = view.findViewById(android.R.id.text1);
String data = textViewOfTheClickedRaw.getText().toString();
Toast.makeText(Main2Activity.this, data, Toast.LENGTH_SHORT).show();
}
});
This is how you can get the value of TextView from android.R.layout.simple_list_item_1
But I really doesn't think this is how things should work in your app

autocompletetextview Set Selection is not working

I have two autocompletetextview
the goal for example if any one select the autocompletetextview "HUB ID " position#3 then the another autocompletetextview " HUBName" must be forced to be on position 3
i.e want the both of them at the same position
// HUB Name listener
HubName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {
HubName.setSelection(pos);
Log.d("ID Array follow pos","HUB ID in selected");
HubID.setSelection(pos);
HubName.setSelection(pos);
ShltrArray.clear();
ShltrArray.add(Shltr1Array.get(pos).toString());
if (Shltr2Array.get(pos).toString() != "Not")
{
ShltrArray.add(Shltr2Array.get(pos).toString());
}
ShltrAdapt.notifyDataSetChanged();
}
});
HubName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
HubName.setSelection(position);
HubID.setSelection(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
HubName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
HubName.showDropDown();
Log.d("ID Array follow ID ", Integer.toString(HubID.getListSelection()));
Log.d("ID Array follow Name ", Integer.toString(HubName.getListSelection()));
}
});
the solution to index the array list to the same position of the first array then inside autocompleteTextView listener then set text to this position as follow
HubName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {
HubID.setText(IDArray.get(pos).toString());}

How to get the path of the clicked item in a ListView

I am displaying a list of files names in a ListView so i want to get the path of the clicked item
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//...
}
any suggestion?
Just get value by position:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String value = (String) lv.getItemAtPosition(position);
//cast it to something else if your object is not String
//for example
File file = (File) lv.getItemAtPosition(position);
String path = file.getAbsolutePath();
}
You can try this in your code .
private List<File> files = new ArrayList<>();
private ListView lv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
lv = (ListView) findViewById(R.id.lv);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
File file = files.get(position);
String fileName = file.getName();
}
});
}

Retrieve Object From Spinner Array And Set It

I'm trying retrieve an object from my spinner array and set it to a certain constant, in this case "EFFECT_AQUA"
My array
String[] spinnerValues = {"Aqua", "Mono", "Blackbird", "Negative"};
when the user clicks on the "Aqua" in the spinner I want the screen to change to Aqua.
My spinner is set and called
Spinner mySpinner = (Spinner) findViewById(R.id.spinner_show);
mySpinner.setAdapter(new MyAdapter(this,R.layout.custom_spinner,spinnerValues));
But not sure how should I approach. Seen many different answers but haven't found anything working.
I know my switch will come in this part
class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
}
Any help is appricated!!
If you want to retrieve an object from your spinner array on item click, you can easily do it with the position parameter in the method onItemSelected.
For example :-
class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String itemSelected = spinnerValues[position];
//Set this to a constant
}
}
Now if you select Aqua, then code will set the variable itemSelected to Aqua.
I hope this is what you need
final Spinner cardStatusSpinner1 = (Spinner)findViewById(R.id.text_interested);
String cardStatusString;
cardStatusSpinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
cardStatusString = parent.getItemAtPosition(pos).toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
put the code below to get the item first then you can manipulate by taking the values like aqua or any color.
spinner = (Spinner)findViewById(R.id.spinner1);
spinner.setAdapter(new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_expandable_list_item_1,spinnerValues));
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
TextView tv = (TextView)view;
Toast.makeText(getApplicationContext(), tv.getText().toString(), 5000).show();
switch(tv.getText().toString()){
case "Aqua":{
//change the color to aqua
break;
}
case " ...":{
//
break;
}
//.... for all the option
}
}

How to use autocomplete textview

When I fill name and select in auto-complete text-view. It's not working same spinner. function in if-else not working or condition incorrect(I don't know).Please suggest me how to use onitemselectlistener on auto-complete text-view to use function in each condition.
AutoCompleteTextView auto1 = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
String[] word;
word = getResources().getStringArray(R.array.word_name);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, word);
auto1.setAdapter(adapter);
auto1.setOnItemSelectedListener(new OnItemSelectedListener() {
TextView txt1 = (TextView)findViewById(R.id.textView1);
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if(arg2==0) // when I fill AED(array 0 in string.xml It's not work)
{
getmoney();
}
else if(arg2==1)
{
getmoney1();
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
String.xml
<string-array name="word_name">
<item>AED United Arab Emirates Dirham</item>
<item>AFN Afghan Afghani</item>
<item>ALL Albanian Lek</item>
<item>AMD Armenian Dram</item>
</string-array>
Instead of OnItemSelectedListener, OnItemClickListener works for AutocompleteTextView.
For example:
auto1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this,
adapter.getItem(position).toString(),
Toast.LENGTH_SHORT).show();
}
});

Categories