如何通过界面使ListView中的项目可编辑

时间:2022-09-12 08:51:56

I have a ListView (shown below) that has a list of ingredients and how much of each ingredient each item has. How can I make it so there is some way to edit the number value when clicking on each ingredient?

我有一个ListView(如下所示),它有一个成分列表以及每个项目有多少成分。如何制作,以便在点击每种成分时有一些编辑数值的方法?

如何通过界面使ListView中的项目可编辑

Context menu and alert dialog so far, I also considered buttons to match the items but discovered it didn't work with the infinite listview. Thank you to anyone who helps me with this!

到目前为止,上下文菜单和警告对话框,我还考虑了按钮来匹配项目,但发现它不适用于无限列表视图。感谢任何帮助我的人!

1 个解决方案

#1


0  

If you want to save the these values then you can create a child class and create list of this of child class. you can use the onItemClickListner to update the value in the object and then do notifydatasetchange on the adapter this will do the trick.

如果要保存这些值,则可以创建子类并创建子类的列表。你可以使用onItemClickListner来更新对象中的值,然后在适配器上进行notifydatasetchange,这样就可以了。

or

Alternatively you can pass the two list in the adapter and update the list you want to update and do notifydatasetchange.

或者,您可以传递适配器中的两个列表并更新要更新的列表,并执行notifydatasetchange。

for e.g.:-

//MainActivity
public class MainActivity extends AppCompatActivity {

ListView recView;
ArrayList<Childclass> chobj=new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recView= (ListView) findViewById(R.id.recView);

    chobj.add(new Childclass("Tomato sauce","0.0"));
    chobj.add(new Childclass("Chicken","0.0"));
    chobj.add(new Childclass("Olives","0.0"));

    ListAdapter lstadptr=new ListAdapter(MainActivity.this,chobj);
    recView.setAdapter(lstadptr);

    recView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
            LayoutInflater inflater = MainActivity.this.getLayoutInflater();
            final View dialogView = inflater.inflate(R.layout.custom_dialog, null);
            dialogBuilder.setView(dialogView);

            final EditText edt = (EditText) dialogView.findViewById(R.id.edit1);

            dialogBuilder.setTitle("Custom dialog");
            dialogBuilder.setMessage("Enter text below");
            dialogBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    //do something with edt.getText().toString();
                    chobj.get(position).setQuantity(edt.getText().toString());
                    ((ListAdapter) recView.getAdapter()).notifyDataSetChanged();
                }
            });
            dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    //pass
                }
            });
            AlertDialog b = dialogBuilder.create();
            b.show();

        }
    });

}}

Child Class

public class Childclass {

String name;
String quantity;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getQuantity() {
    return quantity;
}

public void setQuantity(String quantity) {
    this.quantity = quantity;
}

public Childclass(String name, String quantity) {

    this.name = name;
    this.quantity = quantity;
}}

ListAdapter

public class ListAdapter extends ArrayAdapter {

ArrayList<Childclass> chobj;
Context context;

public ListAdapter(@NonNull Context context,ArrayList<Childclass> chobj) {
    super(context, R.layout.recyclerow, chobj);
    this.context = context;
    this.chobj = chobj;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    View v;
    LayoutInflater inflater = LayoutInflater.from(getContext());
    v = inflater.inflate(R.layout.recyclerow, parent, false);
    TextView tv= (TextView) v.findViewById(R.id.txtrow);
    TextView tv1= (TextView) v.findViewById(R.id.txtrow1);

    tv.setText(chobj.get(position).getName());
    tv1.setText(chobj.get(position).getQuantity());

    return v;
}}

activity main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.abhishek.kotlinprojecttest.MainActivity">


<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f2f2f2"
    android:paddingTop="10dp"
    android:id="@+id/recView">

</ListView>

custom alert

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical">

<EditText
    android:id="@+id/edit1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text" />

list row

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="3dp"
    android:layout_marginTop="3dp"
    android:id="@+id/rowcd">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="listTExt"
        android:id="@+id/txtrow"
        android:textColor="#000000"
        android:textSize="16sp"
        android:gravity="center"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="listTExt"
        android:id="@+id/txtrow1"
        android:textColor="#000000"
        android:textSize="16sp"
        android:gravity="center"/>

</android.support.v7.widget.CardView>

Hope this will help you

希望对你有帮助

#1


0  

If you want to save the these values then you can create a child class and create list of this of child class. you can use the onItemClickListner to update the value in the object and then do notifydatasetchange on the adapter this will do the trick.

如果要保存这些值,则可以创建子类并创建子类的列表。你可以使用onItemClickListner来更新对象中的值,然后在适配器上进行notifydatasetchange,这样就可以了。

or

Alternatively you can pass the two list in the adapter and update the list you want to update and do notifydatasetchange.

或者,您可以传递适配器中的两个列表并更新要更新的列表,并执行notifydatasetchange。

for e.g.:-

//MainActivity
public class MainActivity extends AppCompatActivity {

ListView recView;
ArrayList<Childclass> chobj=new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recView= (ListView) findViewById(R.id.recView);

    chobj.add(new Childclass("Tomato sauce","0.0"));
    chobj.add(new Childclass("Chicken","0.0"));
    chobj.add(new Childclass("Olives","0.0"));

    ListAdapter lstadptr=new ListAdapter(MainActivity.this,chobj);
    recView.setAdapter(lstadptr);

    recView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
            LayoutInflater inflater = MainActivity.this.getLayoutInflater();
            final View dialogView = inflater.inflate(R.layout.custom_dialog, null);
            dialogBuilder.setView(dialogView);

            final EditText edt = (EditText) dialogView.findViewById(R.id.edit1);

            dialogBuilder.setTitle("Custom dialog");
            dialogBuilder.setMessage("Enter text below");
            dialogBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    //do something with edt.getText().toString();
                    chobj.get(position).setQuantity(edt.getText().toString());
                    ((ListAdapter) recView.getAdapter()).notifyDataSetChanged();
                }
            });
            dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    //pass
                }
            });
            AlertDialog b = dialogBuilder.create();
            b.show();

        }
    });

}}

Child Class

public class Childclass {

String name;
String quantity;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getQuantity() {
    return quantity;
}

public void setQuantity(String quantity) {
    this.quantity = quantity;
}

public Childclass(String name, String quantity) {

    this.name = name;
    this.quantity = quantity;
}}

ListAdapter

public class ListAdapter extends ArrayAdapter {

ArrayList<Childclass> chobj;
Context context;

public ListAdapter(@NonNull Context context,ArrayList<Childclass> chobj) {
    super(context, R.layout.recyclerow, chobj);
    this.context = context;
    this.chobj = chobj;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    View v;
    LayoutInflater inflater = LayoutInflater.from(getContext());
    v = inflater.inflate(R.layout.recyclerow, parent, false);
    TextView tv= (TextView) v.findViewById(R.id.txtrow);
    TextView tv1= (TextView) v.findViewById(R.id.txtrow1);

    tv.setText(chobj.get(position).getName());
    tv1.setText(chobj.get(position).getQuantity());

    return v;
}}

activity main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.abhishek.kotlinprojecttest.MainActivity">


<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f2f2f2"
    android:paddingTop="10dp"
    android:id="@+id/recView">

</ListView>

custom alert

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical">

<EditText
    android:id="@+id/edit1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text" />

list row

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="3dp"
    android:layout_marginTop="3dp"
    android:id="@+id/rowcd">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="listTExt"
        android:id="@+id/txtrow"
        android:textColor="#000000"
        android:textSize="16sp"
        android:gravity="center"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="listTExt"
        android:id="@+id/txtrow1"
        android:textColor="#000000"
        android:textSize="16sp"
        android:gravity="center"/>

</android.support.v7.widget.CardView>

Hope this will help you

希望对你有帮助