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

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

Related

How to get selected item id from JSON in Spinner?

How to get id From JSON in spinner ?
i want get city id, if i choose item in spinner
example : https://imgur.com/shvfvnO,
if i choose "ACEH BARAT", i'll get id "512"
public void displayCities(List<CityResponse.City> cities) {
for (CityResponse.City city : cities) {
Log.d(TAG, city.getNama());
spinnerItem.add(city.getNama());
adapter.notifyDataSetChanged();
}
}
private void setSpinner(Context context){
adapter = new ArrayAdapter<>(context, R.layout.spinner_item, spinnerItem);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mainBinding.spinnerCity.setAdapter(adapter);
mainBinding.spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String citySelected = parent.getItemAtPosition(position).toString();
Toast.makeText(context, "City : " + citySelected, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(context, "Not Selected", Toast.LENGTH_LONG).show();
}
});
}
You need to create another arraylist to store id
locationId.add(i, response.getData());
now you need to get Id in spinner
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
try {
selectedId = (String) locationId.get(position);
} catch (Exception e) {
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
You need to update mainBinding.spinnerCity.setOnItemSelectedListener() inside setSpinnner() method as I have added below:
private void setSpinner(Context context){
adapter = new ArrayAdapter<>(context, R.layout.spinner_item, spinnerItem);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mainBinding.spinnerCity.setAdapter(adapter);
mainBinding.spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Comment by Hari: You have already position available here so why you are doing this..
//String citySelected = parent.getItemAtPosition(position).toString();
/*Comment by Hari: If CityModel is the model of your cities ArrayList then it can be received inside spinner from the same index position as of the item as you are creating spinnerItemList in loop and both the list item position will be synced. */
CityModel cityModel = cities.get(position);
String citySelected = cityModel.getCity(); //Where cities is the main array in your activity class.
String cityId = cityModel.getCityId();
Toast.makeText(context, "City Name: " + citySelected + " // City Id: cityId" + , Toast.LENGTH_LONG).show();
//Do what you want to do next with the cityId here....
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(context, "Not Selected", Toast.LENGTH_LONG).show();
}
});
}
Change your onItemSelected like this
mainBinding.spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String citySelected = parent.getItemAtPosition(position).toString();
for (CityResponse.City city : cities) {
if(city.getNama().equalsIgnoreCase(citySelected){
String city_id = city.getCityId(); //THIS IS YOUR CITY ID
}
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(context, "Not Selected", Toast.LENGTH_LONG).show();
}
});

How to get customer id of selected value from spinner

I want to get the id of the value selected in the spinner. For example when i add new customer, the customer name is "jack" and id is "4". Then i can select customer name - "jack in spinner", how to get id "4".
The Database:
public List<String> getAllUsers(String userID) {
List<String> userlist = new ArrayList<>();
String status= "Active";
SQLiteDatabase db = this.getReadableDatabase();
String query = "select " + COLUMN_Customer_NAME + " from customer where " + COLUMN_Customer_USERID + "='" + userID + "'"+" AND " + COLUMN_Customer_STATUS + "='" + status + "'";
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
userlist.add(cursor.getString(0));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return userlist;
}
The implementation code:
prepareData();
allusers.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// clicked item will be shown as spinner
value = parent.getItemAtPosition(position).toString();
custid = position;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
public void prepareData()
{
users=databaseHelper.getAllUsers(user_id2);
//adapter for spinner
adapter=new ArrayAdapter<String>(AddMobRepairActivity.this,android.R.layout.simple_dropdown_item_1line,android.R.id.text1,users);
//attach adapter to spinner
allusers.setAdapter(adapter);
}
prepareData();
allusers.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// clicked item will be shown as spinner
value = parent.getItemAtPosition(position).toString();
custid = position;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
public void prepareData()
{
users=databaseHelper.getAllUsers(user_id2);
//adapter for spinner
adapter=new ArrayAdapter<String>(AddMobRepairActivity.this,android.R.layout.simple_dropdown_item_1line,android.R.id.text1,users);
//attach adapter to spinner
allusers.setAdapter();
}

How to delete the selected items from the list in android

I have database and I display the contents in a list using the simple_list_item_checked layout. Now I want to delete items that the user chooses from that list. How can I do so?
this is the xml activity:
<ImageButton
android:id="#+id/ib_delete"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/delete"
android:layout_centerHorizontal="true"
android:layout_below="#+id/tv_page_mySpace2"
android:layout_marginTop="20dp"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/theuserideaslist"
android:background="#drawable/brain1"
android:layout_below="#id/ib_delete"
android:layout_marginTop="20dp"/>
and this is the remove method they told me to put it my open helper:
public void removeIedas (long id) {
String string = String.valueOf(id);
database.execSQL("DELETE FROM name WHERE _id = '" + string + "'");
}
and this is my java class for the activity:
final MyOpenHelper myOpenHelper = new MyOpenHelper( getApplicationContext());
final ListView theuserideaslist = (ListView) findViewById(R.id.theuserideaslist);
ArrayList<String> n ;
n = myOpenHelper.ShowTheUserIdeas();
final ArrayAdapter<String> myadapter = new ArrayAdapter<String> (getApplicationContext() , android.R.layout.simple_list_item_checked , n ) ;
theuserideaslist.setAdapter(myadapter);
theuserideaslist.setChoiceMode(2);
final ImageButton ib_delete = (ImageButton) findViewById(R.id.ib_delete);
theuserideaslist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, final long id) {
ib_delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myOpenHelper.removeIedas(id); //create removemethod in database class
}
});
}
});
/* ib_delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
theuserideaslist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// ArrayList database = null;
myOpenHelper.removeIedas(id); //create removemethod in database class
}
});
}
});*/
what i want is: when i click on ib_delete delete the checked items.
And I am sorry, i am still new.
Thank You.
Use This:
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
database.remove(id); //Create a remove method in your database class
}
});
and in remove method:
public void remove(long id){
String string = String.valueOf(id);
database.execSQL("DELETE FROM favorite WHERE _id = '" + string + "'");
}
Collected from
this stackoverflow question
Update:
don't set onclick lister on the button, in fact you probably want to remove an item from list view when it is clicked.
Assuming that your listView is the id for ListView of your Activity,
Here:
myDBHelper is an object of class that extends SQLiteOpenHelper like
public class MyDBHelper extends SQLiteOpenHelper{
//codes..
public void DeleteFromDB(String d) {
SQLiteDatabase db = getWritableDatabase();
String dm = "\"" + d + "\"";
try {
db.execSQL("delete from " + db_table + " where taskName=" + dm);
} catch (SQLException e) {
Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT).show();
}
}
}
Then use this in main activity :
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
myDBHelper.DeleteFromDB(arrayList.get(position));
}
});

Reading value from String Array populated using ListView

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();
}
});

unable to retrieve value from spinner

This is a very basic issue.
It is that, I have a basic Spinner implemented with a listener as below:
Spinner sensModeSpinner = (Spinner) findViewById(R.id.SensorModeSpinner);
ArrayAdapter<CharSequence> sensModeAdapter = ArrayAdapter.createFromResource(this, R.array.sensorMode_array, android.R.layout.simple_spinner_item);
sensModeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sensModeSpinner.setAdapter(sensModeAdapter);
sensModeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
sMode = parent.getItemAtPosition(pos).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getBaseContext(), "You need to select atleast one mode!", Toast.LENGTH_SHORT).show();
}
});
Toast.makeText(getBaseContext(), "sensorMode = " + sMode, Toast.LENGTH_SHORT).show();
The issue is the sMode variable result is NOT appearing in the Toast message(value is NULL) where as it works fine within the OnItemSelected method.
Where am I going wrong?
you are executing the Toast.show(); method before the sMode value could be set, instead place this in onItemSelected() after setting sMode value, change your code to following:
Spinner sensModeSpinner = (Spinner) findViewById(R.id.SensorModeSpinner);
ArrayAdapter<CharSequence> sensModeAdapter = ArrayAdapter.createFromResource(this, R.array.sensorMode_array,
android.R.layout.simple_spinner_item);
sensModeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sensModeSpinner.setAdapter(sensModeAdapter);
sensModeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
sMode = parent.getItemAtPosition(pos).toString();
Toast.makeText(getBaseContext(), "sensorMode = " + sMode, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getBaseContext(),
"You need to select atleast one mode!",
Toast.LENGTH_SHORT).show();
}
});
You can do this
Spinner sensModeSpinner = (Spinner) findViewById(R.id.SensorModeSpinner);
ArrayAdapter<CharSequence> sensModeAdapter = ArrayAdapter.createFromResource(this, R.array.sensorMode_array, android.R.layout.simple_spinner_item);
sensModeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sensModeSpinner.setAdapter(sensModeAdapter);
sensModeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
sMode = parent.getItemAtPosition(pos).toString();
//CALL_YOUR_FUNCTION_HERE
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getBaseContext(), "You need to select atleast one mode!", Toast.LENGTH_SHORT).show();
}
});
some time may be it is silly mistake so try this..
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// sMode = parent.getItemAtPosition(pos).toString();
sMode = parent.getAdapter().getItemAtPosition(pos).toString();
}

Categories