获取列表视图行的旧值。如何从 android 中的列表视图获取更新值
getting old values of listview row. how to get the updated value from listview in android
那里有 textView 子并添加。单击添加值将被添加,如果 sub 则值将被削减。
适配器 class.
public class ListAdapter extends ArrayAdapter {
ArrayList<RowModel> model;
Context mContext;
ArrayList<Integer> selections;
static class ViewHolder
{
TextView text,edittext;
ImageView indicator;
ImageView image;
TextView sub,add;
}
public ListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
mContext = context;
model = new ArrayList<RowModel>();
selections = new ArrayList<Integer>();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row == null)
{
LayoutInflater inflater =(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_row, null);
final ViewHolder holder = new ViewHolder();
holder.text = (TextView) row.findViewById(R.id.row_txt);
holder.image = (ImageView)row.findViewById(R.id.row_img);
holder.edittext = (TextView) row.findViewById(R.id.edittext);
holder.sub = (TextView) row.findViewById(R.id.listsub);
holder.add = (TextView) row.findViewById(R.id.listadd);
//holder.edittext.setText("0");
holder.indicator = (ImageView)row.findViewById(R.id.row_indicator);
ColorDrawable cd = new ColorDrawable(Color.parseColor("#FF007d7b"));
holder.indicator.setImageDrawable(cd);
row.setTag(holder);
}
final ViewHolder vh = (ViewHolder)row.getTag();
vh.image.setImageResource(model.get(position).resID);
vh.text.setText(model.get(position).country);
vh.edittext.setText(model.get(position).editText1);
vh.sub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//notifyDataSetChanged();
// setNotifyOnChange(true);
int i = Integer.parseInt(vh.edittext.getText().toString());
i--;
vh.edittext.setText(""+i);
notifyDataSetChanged();
}
});
vh.add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//notifyDataSetChanged();
int i = Integer.parseInt(vh.edittext.getText().toString());
i++;
vh.edittext.setText(""+i);
}
});
return row;
}
public String getvalue(int i)
{
String ii = null;
if(!this.model.get(i).editText1.equals("0"))
{
ii = this.model.get(i).country + " "+this.model.get(i).editText1;
}
return ii;
}
}
现在为按钮编写代码,需要从主菜单单击按钮时的所有数量值 activity
public class MainActivity extends Activity {
private ListView list;
private ListAdapter listAdapter;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list =(ListView) this.findViewById(R.id.list);
button1 = (Button)findViewById(R.id.button1);
listAdapter = new ListAdapter(this, 0);
this.initAdapter();
list.setAdapter(listAdapter);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
for(int i = 0; i <listAdapter.getCount();i++)
{
//listAdapter.updateSelection(i);
Toast.makeText(MainActivity.this, listAdapter.getvalue(i), 1).show();
//listAdapter.notifyDataSetChanged();
((ListAdapter)listAdapter).notifyDataSetChanged();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private void initAdapter()
{
RowModel row = new RowModel(R.drawable.china, "China","0");
listAdapter.addRow(row);
RowModel row2 = new RowModel(R.drawable.japan, "Japan","1");
listAdapter.addRow(row2);
RowModel row3 = new RowModel(R.drawable.korea, "Korea","2");
listAdapter.addRow(row3);
RowModel row4 = new RowModel(R.drawable.america, "America","3");
listAdapter.addRow(row4);
RowModel row5 = new RowModel(R.drawable.canada, "Canada","4");
listAdapter.addRow(row5);
RowModel row6 = new RowModel(R.drawable.england, "England","5");
listAdapter.addRow(row6);
RowModel row7 = new RowModel(R.drawable.france, "France","06");
listAdapter.addRow(row7);
}
}
我的get/setclass
public class RowModel {
int resID;
String country;
String editText1;
public RowModel(int rid, String country,String editText1)
{
this.resID = rid;
this.country = country;
this.editText1 = editText1;
}
public String getEditText1() {
return editText1;
}
public void setEditText1(String editText1) {
this.editText1 = editText1;
}
}
你正在尝试做某事并恢复 back.Let 我在 onclick 侦听器中详细解释你正在获取编辑文本并向其添加值不是正确的方法,而是你可以访问对象并更改edittext1 的值,然后调用 notifydatasetchanged 来完成您的工作。
进行以下更改:--
> public class ListAdapter extends ArrayAdapter<RowModel>{
在您的 getView 方法中获取基础模型的行:--
> final RowModel row = getItem(position);
现在您需要使用行模型,而不是布局文本字段。
这是您的子点击:--
vh.sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int i = Integer.parseInt(row.getEditText1());
i--;
row.setEditText1(Integer.toString(i));
// vh.edittext.setText("" + i); --this is not at all required
notifyDataSetChanged();
}
});
类似的是其他添加点击
notifyDataSetChanged()
将在适配器上工作,您根本不会在代码中更新适配器。
我建议你看看this post。它很好地解释了适配器的工作原理。这应该有效
那里有 textView 子并添加。单击添加值将被添加,如果 sub 则值将被削减。 适配器 class.
public class ListAdapter extends ArrayAdapter {
ArrayList<RowModel> model;
Context mContext;
ArrayList<Integer> selections;
static class ViewHolder
{
TextView text,edittext;
ImageView indicator;
ImageView image;
TextView sub,add;
}
public ListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
mContext = context;
model = new ArrayList<RowModel>();
selections = new ArrayList<Integer>();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row == null)
{
LayoutInflater inflater =(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_row, null);
final ViewHolder holder = new ViewHolder();
holder.text = (TextView) row.findViewById(R.id.row_txt);
holder.image = (ImageView)row.findViewById(R.id.row_img);
holder.edittext = (TextView) row.findViewById(R.id.edittext);
holder.sub = (TextView) row.findViewById(R.id.listsub);
holder.add = (TextView) row.findViewById(R.id.listadd);
//holder.edittext.setText("0");
holder.indicator = (ImageView)row.findViewById(R.id.row_indicator);
ColorDrawable cd = new ColorDrawable(Color.parseColor("#FF007d7b"));
holder.indicator.setImageDrawable(cd);
row.setTag(holder);
}
final ViewHolder vh = (ViewHolder)row.getTag();
vh.image.setImageResource(model.get(position).resID);
vh.text.setText(model.get(position).country);
vh.edittext.setText(model.get(position).editText1);
vh.sub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//notifyDataSetChanged();
// setNotifyOnChange(true);
int i = Integer.parseInt(vh.edittext.getText().toString());
i--;
vh.edittext.setText(""+i);
notifyDataSetChanged();
}
});
vh.add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//notifyDataSetChanged();
int i = Integer.parseInt(vh.edittext.getText().toString());
i++;
vh.edittext.setText(""+i);
}
});
return row;
}
public String getvalue(int i)
{
String ii = null;
if(!this.model.get(i).editText1.equals("0"))
{
ii = this.model.get(i).country + " "+this.model.get(i).editText1;
}
return ii;
}
}
现在为按钮编写代码,需要从主菜单单击按钮时的所有数量值 activity
public class MainActivity extends Activity {
private ListView list;
private ListAdapter listAdapter;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list =(ListView) this.findViewById(R.id.list);
button1 = (Button)findViewById(R.id.button1);
listAdapter = new ListAdapter(this, 0);
this.initAdapter();
list.setAdapter(listAdapter);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
for(int i = 0; i <listAdapter.getCount();i++)
{
//listAdapter.updateSelection(i);
Toast.makeText(MainActivity.this, listAdapter.getvalue(i), 1).show();
//listAdapter.notifyDataSetChanged();
((ListAdapter)listAdapter).notifyDataSetChanged();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private void initAdapter()
{
RowModel row = new RowModel(R.drawable.china, "China","0");
listAdapter.addRow(row);
RowModel row2 = new RowModel(R.drawable.japan, "Japan","1");
listAdapter.addRow(row2);
RowModel row3 = new RowModel(R.drawable.korea, "Korea","2");
listAdapter.addRow(row3);
RowModel row4 = new RowModel(R.drawable.america, "America","3");
listAdapter.addRow(row4);
RowModel row5 = new RowModel(R.drawable.canada, "Canada","4");
listAdapter.addRow(row5);
RowModel row6 = new RowModel(R.drawable.england, "England","5");
listAdapter.addRow(row6);
RowModel row7 = new RowModel(R.drawable.france, "France","06");
listAdapter.addRow(row7);
}
}
我的get/setclass
public class RowModel {
int resID;
String country;
String editText1;
public RowModel(int rid, String country,String editText1)
{
this.resID = rid;
this.country = country;
this.editText1 = editText1;
}
public String getEditText1() {
return editText1;
}
public void setEditText1(String editText1) {
this.editText1 = editText1;
}
}
你正在尝试做某事并恢复 back.Let 我在 onclick 侦听器中详细解释你正在获取编辑文本并向其添加值不是正确的方法,而是你可以访问对象并更改edittext1 的值,然后调用 notifydatasetchanged 来完成您的工作。
进行以下更改:--
> public class ListAdapter extends ArrayAdapter<RowModel>{
在您的 getView 方法中获取基础模型的行:--
> final RowModel row = getItem(position);
现在您需要使用行模型,而不是布局文本字段。 这是您的子点击:--
vh.sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int i = Integer.parseInt(row.getEditText1());
i--;
row.setEditText1(Integer.toString(i));
// vh.edittext.setText("" + i); --this is not at all required
notifyDataSetChanged();
}
});
类似的是其他添加点击
notifyDataSetChanged()
将在适配器上工作,您根本不会在代码中更新适配器。
我建议你看看this post。它很好地解释了适配器的工作原理。这应该有效