我有一个ListView(如下所示),它有一个成分列表以及每个项目有多少成分。如何制作,以便在点击每种成分时有一些编辑数值的方法?
到目前为止,上下文菜单和警告对话框,我还考虑了与项目匹配的按钮,但发现它没有使用无限列表视图。感谢任何帮助我的人!
答案 0 :(得分:0)
如果要保存这些值,则可以创建子类并创建子类的列表。 你可以使用onItemClickListner来更新对象中的值,然后在适配器上进行notifydatasetchange,这样做就可以了。
或
或者,您可以传递适配器中的两个列表并更新要更新的列表,并且不要进行更改。
例如: -
//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();
}
});
}}
儿童班
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;
}}
活动主要
<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>
自定义提醒
<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" />
列表行
<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>
希望这会对你有所帮助