我有一个自定义listView,其中包含textView,editText和一行按钮。 listView是使用适配器创建的
我想在按下按钮时用editText中输入的值更新textView。
我想这样做而不刷新整个清单。
任何想法如何只更新列表中的一行?
谢谢!
为了更清楚,我将提供代码:
私有静态类EfficientAdapter扩展BaseAdapter { private LayoutInflater mInflater; 上下文myContext;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
myContext = context;
}
public int getCount() {
return nameList.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.travelpassengers_item,
null);
holder = new ViewHolder();
holder.ratingTv = (TextView) convertView
.findViewById(R.id.tvTPRating);
holder.rateBtn = (Button) convertView
.findViewById(R.id.btnTPRate);
holder.plusBtn = (Button) convertView
.findViewById(R.id.btnPlus);
holder.minusBtn = (Button) convertView
.findViewById(R.id.btnMinus);
holder.rateEt = (EditText) convertView
.findViewById(R.id.etTPRating);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ratingTv.setText(rating[position] + "/10");
holder.callBtn.setTag(phoneList[position]);
holder.ratingBtn.setTag(ids[position]);
holder.ratingDisableBtn.setTag(numberRates[position]);
holder.ratingTv.setTag(rating[position]);
holder.rateEt.setTag(position);
holder.rateBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String id = holder.ratingBtn.getTag().toString();
int position = Integer.parseInt(holder.rateEt
.getTag().toString());
double oldRate = Double
.parseDouble(rating[position]);
int number = numberRates[position];
double newRate = Double.parseDouble(holder.rateEt
.getText().toString());
DecimalFormat oneDigit = new DecimalFormat(
"#,##0.0");
number++;
double avg = (oldRate + newRate) / number;
newRate = Double.valueOf(oneDigit.format(avg));
// I want to put the value from newRate in the holder.ratingTv on right position without updating entire listView.
}
}
});
holder.plusBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int rate = Integer.parseInt(holder.rateEt.getText()
.toString());
if (rate < 10) {
rate++;
holder.rateEt.setText(Integer.toString(rate));
}
}
});
holder.minusBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int rate = Integer.parseInt(holder.rateEt.getText()
.toString());
if (rate > 0) {
rate--;
holder.rateEt.setText(Integer.toString(rate));
}
}
});
return convertView;
}
static class ViewHolder {
public TextView ratingTv;
public Button minusBtn, plusBtn;
public EditText rateEt;
public Button rateBtn;
}
}
答案 0 :(得分:1)
TextView tv = (TextView) getViewById(R.id.myTextView);
EditText et = (EditText) getViewById(R.id.myEditText);
Button b = (Button) getViewById(R.id.myButton);
b.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
tv.setText(et.getText());
}
});
这可能需要一些调整,因为我还没有测试过(例如从CharSequence转换为String)。