Таким образом, всякий раз, когда я прокручиваю список в представлении списка и помещаю значение в editText, значение editText исчезает при прокрутке вниз.Я использую пользовательский макет для своего списка просмотра.
Я хочу предотвратить исчезновение значения EditText при выходе строки списка за пределы экрана.
add_food_intake.java
public class add_food_intake extends AppCompatActivity {
SQLiteDatabase db, Foods;
SQLiteOpenHelper helper1, helper;
ArrayList<String> listItem;
ArrayAdapter adapter;
ListView foodview;
SwipeRefreshLayout swiper;
EditText editText1;
int id;
String str, str1, age1, foodname, foodtype, grams, kCal, carbohydrates;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_food_intake);
helper = new FoodDB(this);
editText1 = findViewById(R.id.editText1);
db = helper.getReadableDatabase();
helper1 = new FoodDB(this);
Button aButton = (Button) findViewById(R.id.addButton);
foodview = (ListView) findViewById(R.id.fooditems);
listItem = new ArrayList<>();
viewData();
foodview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String text = foodview.getItemAtPosition(i).toString();
Toast.makeText(add_food_intake.this, "" + text, Toast.LENGTH_SHORT).show();
}
});
Button mShowDialog = (Button) findViewById(R.id.customButton);
mShowDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(add_food_intake.this);
final View mView = getLayoutInflater().inflate(R.layout.custom_food_intake_dialog, null);
final EditText FoodName = (EditText) mView.findViewById(R.id.food_name);
final EditText ServingSize = (EditText) mView.findViewById(R.id.serving_size);
final EditText Fats = (EditText) mView.findViewById(R.id.total_fat);
final EditText Calories = (EditText) mView.findViewById(R.id.calories);
final EditText Carbohydrates = (EditText) mView.findViewById(R.id.carbohydrates);
final EditText Protein = (EditText) mView.findViewById(R.id.protein);
final RadioButton go1 = (RadioButton) mView.findViewById(R.id.go);
final RadioButton grow1 = (RadioButton) mView.findViewById(R.id.grow);
final RadioButton glow1 = (RadioButton) mView.findViewById(R.id.glow);
final CheckBox breakfast = (CheckBox) mView.findViewById(R.id.breakfast_box);
final CheckBox lunch = (CheckBox) mView.findViewById(R.id.lunch_box);
final CheckBox dinner = (CheckBox) mView.findViewById(R.id.dinner_box);
final Button button = (Button) mView.findViewById(R.id.addfromcustomfood);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = FoodName.getText().toString();
String size = ServingSize.getText().toString();
String fats = Fats.getText().toString();
String calories = Calories.getText().toString();
String carbs = Carbohydrates.getText().toString();
String protein = Protein.getText().toString();
String type = "";
ArrayList<String> meal = new ArrayList<>();
String add = meal.toString();
if (go1.isChecked()) {
type = go1.getText().toString();
} else if (grow1.isChecked()) {
type = grow1.getText().toString();
} else if (glow1.isChecked()) {
type = glow1.getText().toString();
}
switch (mView.getId()) {
case R.id.breakfast_box:
if (breakfast.isChecked()) {
meal.add("Breakfast");
} else {
meal.remove("Breakfast");
}
case R.id.lunch_box:
if (lunch.isChecked()) {
meal.add("Lunch");
} else {
meal.remove("Lunch");
}
case R.id.dinner_box:
if (dinner.isChecked()) {
meal.add("Dinner");
} else {
meal.remove("Dinner");
}
}
if (!name.isEmpty() && !size.isEmpty() && !fats.isEmpty() && !calories.isEmpty() && !protein.isEmpty() && !carbs.isEmpty()
&& (go1.isChecked() || grow1.isChecked() || glow1.isChecked())) {
insertData(type, name, size, fats, calories, carbs, protein, add);
Toast.makeText(add_food_intake.this, "Food has ben Added!", Toast.LENGTH_SHORT).show();
refreshData();
} else {
Toast.makeText(add_food_intake.this, "Fail", Toast.LENGTH_SHORT).show();
}
}
});
mBuilder.setView(mView);
AlertDialog dialog = mBuilder.create();
dialog.show();
}
});
}
public void insertData(String type, String name, String size, String fats, String calories, String carbs, String protein, String add) {
db = helper1.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("type", type);
contentValues.put("name", name);
contentValues.put("grams", size);
contentValues.put("cal", calories);
contentValues.put("fat", fats);
contentValues.put("carbohydrates", carbs);
contentValues.put("protein", protein);
contentValues.put("meal_type", add);
long id = db.insert("Food_Items", null, contentValues);
}
private void viewData() {
Cursor cursor = db.rawQuery("SELECT * FROM Food_Items", null);
if (cursor.getCount() == 0) {
Toast.makeText(add_food_intake.this, "No data to show", Toast.LENGTH_SHORT).show();
} else {
while (cursor.moveToNext()) {
listItem.add(cursor.getString(cursor.getColumnIndex("name")));
}
adapter = new MySimpleArrayAdapter(add_food_intake.this, listItem);
foodview.setAdapter(adapter);
}
cursor.close();
}
public void refreshData() {
listItem.clear();
viewData();
foodview.setAdapter(adapter);
}
/**
* This adapter will create your list view row by row
*/
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final ArrayList<String> values;
public MySimpleArrayAdapter(Context context, ArrayList<String> values) {
super(context, R.layout.add_food_intake_layout, values);
this.context = context;
this.values = values;
}
/**
* Here we go and get our rowlayout.xml file and set the textview text.
* This happens for every row in your listview.
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.add_food_intake_layout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.textView1);
EditText editText = (EditText) rowView.findViewById(R.id.editText1);
// Setting the text to display
textView.setText(values.get(position));
return rowView;
}
}
}
add_food_intake_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="20dp"
android:text="Name"
android:textColor="@color/Black"
android:textSize="16dp" />
<RelativeLayout
android:id="@+id/layout2"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView1"
android:orientation="vertical">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="Value"
android:textColor="@color/MainGreen"
android:textSize="18dp" />
</RelativeLayout>
<RelativeLayout
android:background="@drawable/edittext_box"
android:id="@+id/layout3"
android:layout_width="100dp"
android:layout_height="59dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="20dp"
android:orientation="vertical">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:inputType="number"
android:singleLine="true"
android:textAlignment="center"
android:textColor="@color/Black"
android:textSize="18dp" />
</RelativeLayout>
</RelativeLayout>