Reputation: 37
I am new in android development and I have a simple question,
I made a ListView
with ArrayAdapter
and an array of some strings.
I want the text of that item (or any other property such text color) will be changed when an item clicked!!
I do that by :
private AdapterView.OnItemClickListener MshowforItem = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
((TextView)view).setText("Hello");
}
};
So when I click on any item the text of it will change but when I scroll down/up and then went to that position, the text of that item changed to the default!! what should I do?????
Upvotes: 2
Views: 17734
Reputation: 6857
Listview populates from dataset [ String Array ] we provide while creating ArrayAdapter. we must update data of that String Array to persist the change of text.
Code:
public class listviewActivity extends AppCompatActivity {
ListView listView;
String dataset[] = {"data", "data", "data", "data", "data", "data", "data", "data", "data", "data", "data", "data", "data", "data", "data", "data", "data", "data", "data", "data"};
ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
listView = (ListView) findViewById(R.id.listview);
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, dataset);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(MshowforItem);
}
private AdapterView.OnItemClickListener MshowforItem = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tvTemp = ((TextView) view);
dataset[position] = "Hello";
tvTemp.setText(dataset[position]);
arrayAdapter.notifyDataSetChanged();
}
};
}
Upvotes: 1
Reputation:
One line you need to write yourArrayListData.set(position, your_model_new_data ));
When you scroll what happen inside ListView
that is very impotent.
Another image representation...
So your problem is Recycling. When you scroll up upper most view get invisible. When you scroll down top view visible again. And its data get form model that you initialize first time.
So you don't get back edited text in your EditText
I am not describing the more but your solution...
There is many way...but one thins is best I think you can do...
Update your Data Source when you setting value in item click in one line
private AdapterView.OnItemClickListener MshowforItem = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
((TextView)view).setText("Hello");
yourArrayListData.set(position, your_model_new_data ));
}
};
Sample code for your..
//Suppose your model like this
public class Greetings {
public String message;
public Greetings(String message) {
this.message = message;
}
}
//So your ArrayList like
ArrayList<Greetings> greetings = new ArrayList<Greetings>();
//You need to write code in one line like
greetings.set(position, new Greetings("hello") ));
That's all .. best of luck..
Upvotes: 4