Item from listview saved state of previously item - android

I am a new android developer and I need your help.
I created a simple listview. I add items in this listview through the keyboard.
Moreover I add action on item. When user click "OK" the app will be strike out text and set background green.
But when I add the next item,I see that it applies that strike out and background option from previously item.
Can you advise me what I need to do in this situation? How to improve it?
My Code:
package com.example.boytsov.foodbasketapp;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
/**
* Created by Boytsov on 23.07.2015.
*/
public class ProductList extends Activity implements View.OnClickListener,AdapterView.OnItemClickListener,AdapterView.OnItemLongClickListener {
EditText myText;
ListView lvMain;
ArrayList<String> catnames;
ArrayAdapter<String> adapter;
Button button;
final String LOG_TAG = "myLogs";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.productlist);
lvMain = (ListView) findViewById(R.id.lvMain);
myText = (EditText) findViewById(R.id.editText);
button=(Button)findViewById(R.id.button);
catnames= new ArrayList<String>();
// создаем адаптер
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, catnames);
// присваиваем адаптер списку
lvMain.setAdapter(adapter);
lvMain.setOnItemClickListener(this);
lvMain.setOnItemLongClickListener(this);
// Прослушиваем нажатия клавиш
button.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button :
catnames.add(0, myText.getText().toString());
adapter.notifyDataSetChanged();
myText.setText("");
myText.setBackgroundColor(Color.TRANSPARENT);
myText.setPaintFlags(0);
break;
}
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.d(LOG_TAG, "itemClick: position = " + i + ", id = "
+ l);
TextView textview= (TextView) view;
if (textview.getPaintFlags() != 16){
textview.setPaintFlags(16);
textview.setBackgroundColor(Color.parseColor("#77dd77"));
Toast.makeText(this, "Куплено", Toast.LENGTH_SHORT).show();
} else {
textview.setPaintFlags(0);
textview.setBackgroundColor(Color.TRANSPARENT);
}
}
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
catnames.remove(position);
adapter.notifyDataSetChanged();
Toast.makeText(this, "Удалено", Toast.LENGTH_SHORT).show();
Log.d(LOG_TAG, "onItemClick: position = " + position + ", id = "
+ id);
return true;
}
}

every listview item has its own R.id.editText so having one global myText = (EditText) findViewById(R.id.editText); does not work.
Instead you have to find the edit in the onClick handler
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button : {
// EditText myText = (EditText) view.findViewById(R.id.editText);
TextView myText = (TextView) view;
catnames.add(0, myText.getText().toString());
adapter.notifyDataSetChanged();
myText.setText("");
myText.setBackgroundColor(Color.TRANSPARENT);
myText.setPaintFlags(0);
}
break;
}
}
[Update 28.7.2015]
Your adapter is using android.R.layout.simple_list_item_1 :
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, catnames);
wich does not have an EditText with id R.id.editText. therefore you get an exception.
simple_list_item_1 is a textview. using
TextView myText = (TextView) view;
might work

Related

OnItemClickListener in Listview with image is not working

I'm beginner in android. I created my OnItemListener in ListView with Image, but when I click the list item, my app crashes.
Here my MainActivity.java
package com.listview.test.arrayadapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
ListView l1;
// RelativeLayout r1;
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l1= (ListView) findViewById(R.id.listview);
// r1= (RelativeLayout) findViewById(R.id.activity_main);
String[] list = {"item1","item2","item3","item4","item5","item6","item7","item8","item9","item10"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.single_view , R.id.textview, list);
l1.setAdapter(adapter);
l1.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView temp = (TextView) view;
Toast.makeText(this, temp.getText() + "" + position, Toast.LENGTH_LONG).show();
}
}
I think you are getting an exception because your R.layout.single_view is not a TextView and you are tring to cast it into that. Do something like this instead:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView temp = (TextView) view.findViewById(R.id.textview);
Toast.makeText(this, temp.getText() + "" + position, Toast.LENGTH_LONG).show();
}

Saved item's state in simple list item

I am a new android developer and I need your help. I created a simple listview. User can add some item in listview (type in EditText and click on Button "OK"). When user made onItemClick , the app will strike out text and set background green.
But then , when I add one more item,I see that it applies that strike out and background option from previously item.
Can you advise me what I need to do in this situation? How to improve it?
package com.example.boytsov.foodbasketapp;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Boytsov on 23.07.2015.
*/
public class ProductList extends Activity implements View.OnClickListener,AdapterView.OnItemClickListener,AdapterView.OnItemLongClickListener,TextView.OnEditorActionListener {
EditText myText;
ListView lvMain;
ArrayList<String> catnames;
ArrayAdapter<String> adapter;
Button button;
DataBase db;
final String LOG_TAG = "myLogs";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.productlist);
db = new DataBase(this);
myText = (EditText)findViewById(R.id.editText);
lvMain = (ListView) findViewById(R.id.lvMain);
button=(Button)findViewById(R.id.button);
catnames= new ArrayList<String>();
// создаем адаптер
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, catnames);
// присваиваем адаптер списку
lvMain.setAdapter(adapter);
lvMain.setOnItemClickListener(this);
lvMain.setOnItemLongClickListener(this);
// Прослушиваем нажатия клавиш
button.setOnClickListener(this);
//слушаем edittext
myText.setOnEditorActionListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button : {
//TextView myText = (TextView) view;
catnames.add(0, myText.getText().toString());
adapter.notifyDataSetChanged();
//myText.setBackgroundColor(Color.TRANSPARENT);
//myText.setPaintFlags(0);
Log.d("Insert: ", "Inserting ..");
db.addProduct(new Product(myText.getText().toString()));
myText.setText("");
Log.d("Reading: ", "Reading all contacts..");
List<Product> products = db.getAllProducts();
for (Product cn : products) {
String log = "Id: "+cn.getID_product()+" ,Name: " + cn.getName_product();
// Writing Contacts to log
Log.d("Name: ", log);
}
}
break;
}
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView textview= (TextView) view;
if (textview.getPaintFlags() != 16){
textview.setPaintFlags(16);
textview.setBackgroundColor(Color.parseColor("#77dd77"));
Toast.makeText(this, "Куплено", Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
Log.d(LOG_TAG, "itemClick: position = " + i + ", id = "
+ l);
} else {
textview.setPaintFlags(0);
textview.setBackgroundColor(Color.TRANSPARENT);
Toast.makeText(this, "Не куплено", Toast.LENGTH_SHORT).show();
}
}
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
catnames.remove(position);
adapter.notifyDataSetChanged();
Toast.makeText(this, "Удалено", Toast.LENGTH_SHORT).show();
Log.d(LOG_TAG, "onItemClick: position = " + position + ", id = "
+ id);
return true;
}
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
Log.d(LOG_TAG, "onItemClick: position = " + actionId + ", id = "
+ event);
handled = true;
}
return handled;
}
}
You apply your changes on the view at position 0.
When you add your item you also add it to position 0, hence the entire list is pushed down by 1 and the new item get the already changed view.
Edited Answer
Sorry, I was short on time, but now I can address it more thoroughly.
One important thing you must understand is that the view which shows your data in the list view DOES NOT NECESSARILY correspond with your data.
If you click on an item in your list and change it's views attributes, it doesn't change the state for the item or object which represents the data, but the view itself.
For example if you click on item at position 0 it will change the view's background at position 0. Then, in your example, you add an item at the top of the list, which puts the newly created object at position 0 (with the already modified view) and pushes THE REST OF THE ALREADY CREATED DATA by one, And you end up with an already changed view at position 0 with new data at position 0.
What you should do is as follows:
1)Make sure your object has a boolean member which states if item is "strike out" or not. like for example mIsStrikeOut.
2)create a custom adapter from any android adapter, like from ArrayAdapter.
3)Override it's getView method, for example:
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
{
view = (LinearLayout) inflater.inflate(R.layout. simple_list_item_1, parent, false);
}
TextView textview = (TextView) view.findViewById(R.id.text1);
if (catnames.get(position).isStrikeOut())
{
textview.setPaintFlags(16);
textview.setBackgroundColor(Color.parseColor("#77dd77"));
Toast.makeText(this, "Куплено", Toast.LENGTH_SHORT).show();
Log.d(LOG_TAG, "itemClick: position = " + i + ", id = "
+ l);
}
else
{
textview.setPaintFlags(0);
textview.setBackgroundColor(Color.TRANSPARENT);
Toast.makeText(this, "Не куплено", Toast.LENGTH_SHORT).show();
}
return view;
}
Side note:
When you query your data from your DB make sure the order is correct, I would suggest ORDER BY id dec or something like that.
I do not really know if this is the right answer but it is a suggestion :
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button : {
//TextView myText = (TextView) view;
catnames.add(0, myText.getText().toString());
View view = listView.getChildAt(0);
TextView textview= (TextView) view;
textview.setBackgroundColor(Color.TRANSPARENT);
adapter.notifyDataSetChanged();
//myText.setBackgroundColor(Color.TRANSPARENT);
//myText.setPaintFlags(0);
Log.d("Insert: ", "Inserting ..");
db.addProduct(new Product(myText.getText().toString()));
myText.setText("");
Log.d("Reading: ", "Reading all contacts..");
List<Product> products = db.getAllProducts();
for (Product cn : products) {
String log = "Id: "+cn.getID_product()+" ,Name: " + cn.getName_product();
// Writing Contacts to log
Log.d("Name: ", log);
}
}
break;
}
}

how to update the display of my CheckBox items in my ListView marked as check?

When I first run my app, the CheckBox items in my ListView are unchecked. In my layout.xml I have a single CheckBox above my ListView items. When that single CheckBox above my ListView is checked it should mark all the CheckBox items in my ListView as checked. My code below does not update the display of my CheckBox items in my ListView as checked but when I tried to get the boolean of each CheckBox item in my ListView by calling checkItem.isChecked() and let it display in my logcat, it returns true. Did I miss something?
package com.usjr.sss.fragment;
import java.util.ArrayList;
import com.usjr.sss.R;
import com.usjr.sss.activity.CourseFragmentActivity;
import com.usjr.sss.adapter.InfoTechAdapter;
import com.usjr.sss.adapter.SubjectDbAdapter;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Toast;
public class InfoTechFirstYearFragment extends Fragment {
private CheckBox checkFirstYearFirstSem;
private ListView listItFirstYearFirstSem;
private ListView listItFirstYearSecondSem;
private InfoTechAdapter infoTechAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_it_first_year, container,
false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i("InfoTechFirstYearFragment", "onActivityCreated");
listItFirstYearFirstSem = (ListView) getActivity().findViewById(
R.id.listItFirstYearFirstSem);
listItFirstYearSecondSem = (ListView) getActivity().findViewById(
R.id.listItFirstYearSecondSem);
}
#Override
public void onStart() {
super.onStart();
Log.i("InfoTechFirstYearFragment", "onStart");
SubjectDbAdapter subjectDbAdapter = new SubjectDbAdapter(getActivity());
subjectDbAdapter.open();
Cursor cursor = subjectDbAdapter.fetchAllSubjects();
ArrayList<String> arrayListSubject = new ArrayList<String>();
ArrayList<String> arrayListFirstSem = new ArrayList<String>();
ArrayList<String> arrayListSecondSem = new ArrayList<String>();
while (cursor.moveToNext()) {
String subject = cursor.getString(cursor
.getColumnIndexOrThrow(SubjectDbAdapter.SUBJECT_ID));
arrayListSubject.add(subject);
}// end while
subjectDbAdapter.close();
int index;
/**
* 1st yr 1st sem
*/
for (index = 0; index < 9; index++) {
arrayListFirstSem.add(arrayListSubject.get(index));
}
infoTechAdapter = new InfoTechAdapter(
(CourseFragmentActivity) getActivity(), arrayListFirstSem);
listItFirstYearFirstSem.setAdapter(infoTechAdapter);
/**
* 1st yr 2nd sem
*/
for (index = 9; index < 18; index++) {
arrayListSecondSem.add(arrayListSubject.get(index));
}
infoTechAdapter = new InfoTechAdapter(
(CourseFragmentActivity) getActivity(), arrayListSecondSem);
listItFirstYearSecondSem.setAdapter(infoTechAdapter);
/**
* MARK ALL THE CHECKBOX AS CHECKED
*/
checkFirstYearFirstSem = (CheckBox) getActivity().findViewById(
R.id.checkFirstYearFirstSem);
checkFirstYearFirstSem.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (checkFirstYearFirstSem.isChecked()) {
Toast.makeText(getActivity(), "ckecked", Toast.LENGTH_SHORT)
.show();
int count = listItFirstYearFirstSem.getCount();
for (int index = 0; index < count; index++) {
// View viewMy =
// listItFirstYearFirstSem.getChildAt(index);
// Object id = v.getTag();
View viewItem = (View) listItFirstYearFirstSem
.getAdapter().getView(index, getView(),
listItFirstYearFirstSem);
CheckBox checkItem = (CheckBox) viewItem
.findViewById(R.id.subject);
checkItem.setChecked(true);
listItFirstYearFirstSem.setItemChecked(index, true);
infoTechAdapter.notifyDataSetChanged();
Log.i("checkItem",
String.valueOf(checkItem.getText().toString()));
Log.i("checkItem",
String.valueOf(checkItem.isChecked()));
}
} else {
Toast.makeText(getActivity(), "unckecked",
Toast.LENGTH_SHORT).show();
}// end if-else(checkFirstYearFirstSem.isChecked())
}// end onClick
});// end OnClickListener
}// end onStart()
}
I solved it myself yey!
Instead of using
View viewItem = (View) listItFirstYearFirstSem.getAdapter().getView(index, getView(), listItFirstYearFirstSem);
I used
View viewItem = listItFirstYearFirstSem.getChildAt(index);
Then
CheckBox checkItem = (CheckBox) viewMy.findViewById(R.id.subject);
checkItem.setChecked(true);

Click on item of customlist-view is not working

This is my code for click on a customlistview. When I click on the header, it works but after header its not working. CustomAdapter is another class in my App where I have defined header and all variables of listview. Please help me to resolve this problem.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class ProbabilityConditional extends Activity {
String htmlcodefor_root = "&#x221A", htmlcodefor_multiply = "&#xD7",
htmlcodefor_divide = "&#xF7", htmlcodefor_underroot = "&#00B3";
ListView listView1;
String htmlcodefor_space = "&#8194", htmlcodefor_pi = "&#928",
htmlcodefor_largespace = "&#8195";
String htmlcodefor_implies = "&#x21D2";
String htmlcodefor_i = "&#7522";
String htmlcodefor_angle = "&#952";
String htmlcodefor_overline = "&#x203E", htmlcodefor_plusminus = "&#177";
// TextView txtv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// txtv = (TextView)findViewById(R.id.txtTitle);
// txtv.setText(Html.fromHtml("1.(constant)<sup><small>0</></> = 1"));
listView1 = (ListView) findViewById(R.id.listView1);
CustomAdapter.formula_one_custom_adapter_class_var = Html.fromHtml("1 ");
CustomAdapter.formula_two_custom_adapter_class_var = Html.fromHtml("2 ");
CustomItemCall formula_data[] = new CustomItemCall[] {
new CustomItemCall(CustomAdapter.formula_one_custom_adapter_class_var),
new CustomItemCall(CustomAdapter.formula_two_custom_adapter_class_var),
};
CustomAdapter adapter = new CustomAdapter(this,R.layout.listview_item_row, formula_data);
View header = (View) getLayoutInflater().inflate(R.layout.listview_header_row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
System.out.println(position);
Toast.makeText(ProbabilityConditional.this,position + " " , Toast.LENGTH_LONG).show();
// When clicked, show a toast with the TextView text
if (position == 1) {
startActivity(new Intent(ProbabilityConditional.this,ProbabilityConditionalDiscrete.class));
} if (position == 2) {
startActivity(new Intent(ProbabilityConditional.this,ProbabilityConditionalContinuous.class));
}
}
});
}
}
make your listview focusable true add these line in your xml android:focusable="true"
and make other item of list make false android:focusable="false "
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
System.out.println(position);
Toast.makeText(ProbabilityConditional.this,position + " " , Toast.LENGTH_LONG).show();
// When clicked, show a toast with the TextView text
if (position == 1) {
startActivity(new Intent(ProbabilityConditional.this,ProbabilityConditionalDiscrete.class));
} if (position == 2) {
startActivity(new Intent(ProbabilityConditional.this,ProbabilityConditionalContinuous.class));
}
}
});
write code like this, simple way:-
ListView list;
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View view,
int position, long id)
{
//if in future you need to start a new activity
//then add below line also in your activity
Intent in = new Intent(MainActivity.this, SecondActivity.class);
startActivity(in);
}
});

Cant get EditText value from custom listView

Excuse me, but i somehow cant post comments for the answers, so i have to write my own here
I need to get String data from custom ListView (consist of EditText and ImageView).
Here is my code:
First, i declare my own copy of ArrayList
private ArrayList<EditText> m_edit = null;
Second, i initialize it inside onCreate method of my Activity
public void onCreate(Bundle savedInstanceState) {
...
m_edit = new ArrayList<EditText>();
...
Third, i fill my ArrayList with EditText copies and then add them to ListView using ArrayAdapter
fieldAddButton.setOnClickListener(new OnClickListener()
{
public void onClick(View view){
EditText et = new EditText(AddCategory.this);
m_edit.add(et);
m_adapter.notifyDataSetChanged();
}
});
Fourth, i want to get EditText value for any item in my custom ListView. So i do something like this:
for (int i = 0;i<m_edit.size();i++){
_string = m_edit.get(i).getText().toString();
}
And there is problem: result is always ""
What am i doing wrong?
My class source code
import android.app.Activity;
import android.widget.Toast;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import android.widget.ArrayAdapter;
import android.view.View.OnClickListener;
import android.view.*;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class AddCategory extends Activity {
private ArrayList<EditText> m_edit = null;
private EditAdapter m_adapter;
private EditText categoryName;
private MyDbAdapter dbAdapter;
private Context context;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addcategory);
final Button categoryAddButton = (Button)findViewById(R.id.CategoryAddButton);
final Button fieldAddButton = (Button)findViewById(R.id.FieldAddButton);
final ListView fieldList = (ListView)findViewById(R.id.CategoryFieldList);
categoryName = (EditText)findViewById(R.id.CategoryNameEdit);
m_edit = new ArrayList<EditText>();
this.m_adapter = new EditAdapter (this, R.layout.categoryfieldlayout, m_edit);
fieldList.setAdapter(m_adapter);
dbAdapter = new MyDbAdapter(this);
context = getApplicationContext();
dbAdapter.open();
fieldAddButton.setOnClickListener(new OnClickListener()
{
public void onClick(View view){
EditText et = new EditText(AddCategory.this);
m_edit.add(et);
m_adapter.notifyDataSetChanged();
}
});
categoryAddButton.setOnClickListener(new OnClickListener()
{
public void onClick(View view){
int duration;
String error;
long categoryID;
;
int errCount = 0;
if (!categoryName.getText().toString().equals("")){
for (int i = 0;i<m_edit.size();i++){
if (m_edit.get(i).getText().toString().equals("")){
errCount++;
}
}
if (errCount != 0){
GroupItem group = new GroupItem(categoryName.getText().toString());
categoryID = dbAdapter.insertGroup(group);
for (int i = 0;i<m_edit.size();i++){
FieldItem field = new FieldItem(m_edit.get(i).getText().toString());
duration = Toast.LENGTH_SHORT;
error = m_edit.get(i).getText().toString();
Toast.makeText(context, error, duration).show();
}
}
else{
duration = Toast.LENGTH_SHORT;
error = "Field cannot be empty";
Toast.makeText(context, error, duration).show();
}
}
else{
duration = Toast.LENGTH_SHORT;
error = "Name cannot be empty";
Toast.makeText(context, error, duration).show();
}
}
});
}
private class EditAdapter extends ArrayAdapter<EditText>{
private ArrayList <EditText> items;
public EditAdapter(Context context, int textViewResourceId, ArrayList<EditText> items) {
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.categoryfieldlayout, null);
}
EditText et = (EditText)v.findViewById(R.id.fieldEdit);
return v;
}
}
}
When i update (write some text) EditText field of my custom listView item, i think, m_edit dont update its content automaticaly. So the problem is, how to force that update
ps. sorry for my poor english
From the code below, I guess you are going wrong by getting text from an ArrayList and not your EditText field.
m_edit = new ArrayList<EditText>();
for (int i = 0;i<m_edit.size();i++) {
_string = m_edit.get(i).getText().toString();
}
Simply set a ListView onClickListener and you will get the Edittext value for each listview item you click. In your case...
final ListView fieldList = (ListView)findViewById(R.id.CategoryFieldList);
fieldList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
EditText et = (EditText)v.findViewById(R.id.fieldEdit);
String myText = et.getText().toString();
Toast.makeText(getApplicationContext(), myText , Toast.LENGTH_SHORT).show();
}
});
can you provide you code snippets so get more idea what you are doing with edittext Arraylist.
From this i can see that you are adding edittext in arraylist and refreshing your listview. but did your listview refreshed with added edittext or not? Then that is the main problem with your code.
thanks for your source,
I think you are using listview and on button click you are trying to get edittext value. but how can you know that which position view is got selected from the listview.
I think there is some problem in what you are asking and what you doing. i think what you are doing is on single button click you try to get all edittext value.
You are getting blank value because in your listview you have not edited it..you can do it something like this.
Inside your adapter getview method put this code
public View getView(final int position, View convertView, ViewGroup parent) {
ed.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
list.get(position).edittext.setText(ed.getText().toString())
}
});
}
Hope this may help you.

Categories

Resources