I need to replace "gold" in Listview
AndroidListView1= "gold" > "goldennnnnnn" AndroidListView2= "gold" > "golderrrrrrrrr"
when i set it in edittext then its work!! but when i set it in List view then print only 1 item("goldennnnnnn")##
this.et3.setText(AndroidListView); (edittext work) but this.Listview.setText(AndroidListView1);not work
this is my code thanks in advance
ListView listView ;
EditText myTextBox,myOuttext,et3;
Button btn;
ClipboardManager myClipboard;
private Object adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTextBox = (EditText) findViewById(R.id.editText1);
myOuttext = (EditText) findViewById(R.id.editText2);
et3=(EditText)findViewById(R.id.editText3);
btn=(Button)findViewById(R.id.button1);
myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
// Get ListView object from xml
listView = (ListView) findViewById(R.id.listView1);
// Defined Array values to show in ListView
String[] values = new String[]
{ "AndroidListView1",
"AndroidListView2",
"AndroidListView3",
"AndroidListView4",
"AndroidListView5",
"AndroidListView6",
"AndroidListView7",
"AndroidListView8",
"AndroidListView9",
"AndroidListView10",
"AndroidListView11",
"AndroidListView12",
"AndroidListView13",
"AndroidListView14",
"AndroidListView15",
"AndroidListView16",
"AndroidListView17",
};
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
ArrayList<?> yourlist = new ArrayList<Object>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
// Assign adapter to ListView
listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, values));
// ListView Item Click Listener
listView.setOnItemClickListener(new 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();
}
});
myTextBox.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#SuppressLint("NewApi")
public void onTextChanged(CharSequence s, int start,
int before, int count) {
MainActivity.this.my1(s);
MainActivity.this.AndroidListView(s);
}
});
}
private String my1(CharSequence arg) {
String str = "";
str = arg.toString().replace(" ", " " ).replace("gold", "goldennnnnnn");
this.myOuttext.setText(str);
System.out.println(str);
return str;
}
private String AndroidListView(CharSequence arg) {
String AndroidListView = "";
AndroidListView= arg.toString().replace(" ", "").replace("gold", "golderrrrrrrrr");
this.et3.setText(AndroidListView);
System.out.println(AndroidListView);
return AndroidListView;
}
}
Try this code to change listView item at particular position:
ArrayList<?> yourlist = new ArrayList<Object>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
//listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,
// android.R.layout.simple_list_item_1, values));
values[3] = "myNewItem";
adapter.notifyDataSetChanged();
Response of above code is:
EDIT: To search a particular item and to change that:
for(int i =0; i<values.length; i++){
if(values[i].equals("myNewItem")){
values[i] = "A_Newer_One";
}
}
adapter.notifyDataSetChanged();
Related
I am pulling data from a SQLite database and storing it in an arrayList to display info on the contact through listView. The user can then type in an EditText and the TextWatcher filters the listView. But, when this happens the position of the listview items do not update in my onClickListener.
The ID I send to my CRUD activity is always the position after they have been filtered not their position when originally listed. How can I make my ListView items hold their original position after they have been filtered? Or a better way to give my CRUD activity the correct ID not via position.
public class Search extends AppCompatActivity {
// List view
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// Search EditText
EditText inputSearch;
// ArrayList for Listview
ArrayList<String> arrayList = new ArrayList<>();
DBHelper db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
db = new DBHelper(this);
arrayList = db.getAllContacts();
lv = (ListView) findViewById(R.id.list_view);
inputSearch = (EditText) findViewById(R.id.inputSearch);
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
lv.setAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Search.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
adapter.notifyDataSetChanged();
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
int id_To_Search = pos + 1;
Bundle dataBundle = new Bundle();
dataBundle.putInt(EXTRA_FLAG, id_To_Search);
// The flag value > 0 is used to signal that the upcoming operation
// is not adding, but updating or deleting
Intent intent = new Intent(getApplicationContext(), CRUDActivity.class);
intent.putExtras(dataBundle); //Intent.putExtras(Bundle extras
startActivity(intent);
}
});
}
I ended up just creating a second array and then splitting it, compared it to the item selected, and then parsed the id from a String to an int. After that, I sent to id to my Crud activity so it could populate the contact searched.
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
String split = (String) parent.getItemAtPosition(pos);
// Split this into sections then compare the name with arrayList2, then take the ID from arratList2
// parse it to an int and set it as the ID to search
int id_To_Search = 0;
String array[] = split.split(" ");
for ( int i = 0; i < arrayList2.size(); i++) {
String split2[] = arrayList2.get(i).split(" ");
if (split2[1].equals(array[0])) {
//print item the same way as you did
id_To_Search = Integer.parseInt(split2[0]);
}
}
Bundle dataBundle = new Bundle();
dataBundle.putInt(EXTRA_FLAG, id_To_Search);
// The flag value > 0 is used to signal that the upcoming operation
// is not adding, but updating or deleting
Intent intent = new Intent(getApplicationContext(), CRUDActivity.class);
intent.putExtras(dataBundle); //Intent.putExtras(Bundle extras
startActivity(intent);
}
});
}
I am creating spinners dynamically when i click add button.but i dont know how to get the values from created dynamic spinners.I need to get values how many shirt selected, saree selected and jeans selected when i click a submit button
this is my code
buttonAdd = (Button)findViewById(R.id.add);
container = (LinearLayout)findViewById(R.id.container);
final List<String> dressArray = new ArrayList<String>();
dressArray.add("Select Dress");
dressArray.add("Shirt");
dressArray.add("Saree");
dressArray.add("Jeans");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, dressArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner dress = (Spinner) findViewById(R.id.dress);
dress.setAdapter(adapter);
final List<String> dresscountArray = new ArrayList<String>();
dresscountArray.add("Count");
dresscountArray.add("1");
dresscountArray.add("2");
dresscountArray.add("3");
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, dresscountArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner dresscount = (Spinner) findViewById(R.id.dresscount);
dresscount.setAdapter(adapter1);
buttonAdd.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
Spinner dresscount = (Spinner)addView.findViewById(R.id.dresscount);
Spinner dress = (Spinner)addView.findViewById(R.id.dress);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
PickupOrder.this, android.R.layout.simple_spinner_item, dressArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dress.setAdapter(adapter);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
PickupOrder.this, android.R.layout.simple_spinner_item, dresscountArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dresscount.setAdapter(adapter1);
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}});
container.addView(addView);
}});
Firstly define globally
String value1;
int value_position1;
And then call these methods inside onCreate after setting value in each Spinner
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
value_position1 = position;
value1 = parent.getSelectedItem().toString()
.trim();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
And when you call your submit button, use values of each String.
showallPrompt = "";
int Count = new_container.getChildCount();
try {
for (int cc = 0; cc < Count; cc++) {
View child = new_container.getChildAt(cc);
Spinner value = (Spinner)(child.findViewById(R.id.sp_dynamic));
String spin_value = value.getSelectedItem().toString();
showallPrompt += spin_value;
}
} catch (Exception e) {
}
Toast.makeText(MainActivity.this,
showallPrompt,
Toast.LENGTH_LONG).show();
}
}
Do like this on your button click...
In the registration process, when the user select the title from the spinner then its array index values should be automatically select and send to the asynctask. I want to send the array index value (position) as well in the below shown code.
new RegisterAsyncTask(getApplicationContext()).execute(TitleName,CountryName);
original value of the position is inside the onItemSelected(), I want to know how to pass it to the onclick()
this is my java code
public class RegisterActivity extends Activity {
private String[] states, states1;
private Spinner countrySP, titleSP;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_register);
Spinner spinner = (Spinner) findViewById(R.id.registerTitle);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
states = getResources().getStringArray(R.array.title_array);
states1 = getResources().getStringArray(R.array.country_array);
titleSP = (Spinner) findViewById(R.id.registerTitle);
final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, states);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
titleSP.setAdapter(dataAdapter);
countrySP = (Spinner) findViewById(R.id.registerCountry);
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, states1);
dataAdapter1
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
countrySP.setAdapter(dataAdapter1);
final Button button = (Button) findViewById(R.id.btnRegister);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
String TitleName = titleSP.getSelectedItem().toString();
String CountryName = countrySP.getSelectedItem().toString();
String position = String.valueOf(titleSP.getSelectedItemPosition());
String position = String.valueOf(countrySP.getSelectedItemPosition()); //Duplicate local variable position
new RegisterAsyncTask(
getApplicationContext()).execute(
TitleName,
CountryName, position);
Toast.makeText(getApplicationContext(),
"Asynctask started",
Toast.LENGTH_SHORT).show();
} catch (Exception ex) {
}
}
});
public class MyOnItemSelectedListener implements OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
//check which spinner triggered the listener
switch (parent.getId()) {
//country spinner
case R.id.registerTitle:
//make sure the country was already selected during the onCreate
String[] titles = getResources().getStringArray(R.array.title_array);
String selectedTitle = titles[position];
System.out.println("title = " + position );
case R.id.registerCountry:
//make sure the country was already selected during the onCreate
String[] countries = getResources().getStringArray(R.array.country_array);
String selectedCountry = countries[position];
System.out.println("country = " + position );
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
}
My title.xml inside values folder
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title_prompt">Choose the title</string>
<string-array name="title_array">
<item>Mr</item>
<item>Mrs</item>
<item>Miss</item>
<item>Dr</item>
<item>Rev</item>
<item>Ms</item>
</string-array>
</resources>
Use getSelectedItemPosition to get selected item position from Spinner on Button click as:
public void onClick(View v) {
String TitleName = titleSP.getSelectedItem().toString();
String CountryName = countrySP.getSelectedItem().toString();
String position=String.valueOf(titleSP.getSelectedItemPosition());
// pass position to RegisterAsyncTask
new RegisterAsyncTask(
getApplicationContext()).execute(TitleName,CountryName,position);
}
yes now i get it,
you need to do something like this
public void onClick(View v) {
try {
String TitleName = titleSP.getSelectedItem().toString();
String CountryName = countrySP.getSelectedItem().toString();
String selectedPos=Integer.toString(titleSP.getSelectedItemPosition()); // or countrySP if u need it to
new RegisterAsyncTask(
getApplicationContext()).execute(
TitleName,
CountryName, selectedPos);
:
:
now you have to modify RegisterAsyncTask doInBackground():
... doInBackground(String... params){
//params[0] is TitleName
//params[1] is CountryName
//params[2] is selectedPos
:
:
}
I am having an issue with using a listview that when I click it nothing is happening. I would like for it when I click the listview item to make a toast so I know it is being clicked. I have been trying/researching for a while and nothing. Would anybody mind taking a look to see if I am missing something I just am overlooking? Many thanks in advance!
Here is my class:
public class MyCar extends Activity {
/**
* Called when the activity is first created.
*/
public ListView mylistView;
String carInfo;
private ArrayAdapter<String> mylistAdapter;
ArrayList<String> arrayListCar = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mycar);
mylistView = (ListView) findViewById(R.id.listView);
arrayListCar = new ArrayList<String>();
//Just had to remove setting this adapter 2 times. Took out line below to fix.
mylistView.setAdapter(mylistAdapter);
mylistView.setOnItemClickListener(new 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();
}
});
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
carInfo = trySomethin();
fillList();
}
public void fillList() {
String make = "";
String model = "";
String[] pieces = carInfo.split("\"");
make = pieces[3];
model = pieces[7];
ArrayList<String> carList = new ArrayList<String>();
carList.add(make + " " + model);
// Create ArrayAdapter using the car list.
mylistAdapter = new ArrayAdapter<String>(MyCar.this, android.R.layout.simple_list_item_single_choice, carList);
mylistView.setAdapter(mylistAdapter);
mylistAdapter.notifyDataSetChanged();
}
}
if you have any elements on listview item change this for them
android:focusable="false"
and if you are changing any elements visibility on runtime you have to handle focus programatically each time you change its visibility.
Try this
listView.setOnItemClickListener(new 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();
}
});
I'm supposed to make a task , I have a spinner connected with a String array x , this array contains three values , so I want when clicking any choice of the spinner a specific list view will will give a specific three values and , this is my code :
public class Four extends ActionBarActivity {
String x [] = {"Jordan","Saudi Arabia", "Syria"};
String Jordan[] = {"Amman","Aqaba","Sarqa"};
String Saudi[] = {"Riyadh","Jeddah","Khobar"};
String Syria[] = {"Hems","Halab","Demashk"};
Spinner sp1 ;
ListView lv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.four);
lv1 = (ListView)findViewById(R.id.listView1);
// Jordan List View
ArrayAdapter<String> jor = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Jordan);
lv1.setAdapter(jor);
// Saudi Arabia List View
ArrayAdapter<String> saud = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Saudi);
lv1.setAdapter(saud);
// Syria List View
ArrayAdapter<String> syr = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Syria);
lv1.setAdapter(syr);
sp1 = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> a = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item , x);
sp1.setAdapter(a);
sp1.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
}
});
}
}
The thir parameter of onItemSelected() (int arg2 in your example, but I would suggest you rename them) is the position you selected in the Spinner. So you could implement that method like this:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if (position == 0)
lv1.setAdapter(jor);
else if (position == 1)
lv1.setAdapter(saud);
else if (position == 1);
lv1.setAdapter(syr);
}
Remember that all the variables involved (lv1, jor, saud, syr) must be defined as final to be used inside the anonymous class, e.g.
final ArrayAdapter<String> jor = ...
final ArrayAdapter<String> saud = ...