1
我在列表視圖項目中有一些TextViews和edittexts。編輯文字在列表視圖中失去焦點
我想要的是顯示一些項目,數量,速度和總價...
每當量或速率發生變化,總價格也應該改變......作爲選擇時的EditText 列表項應該看到是集中的。
public class MainActivity extends Activity {
private MySimpleArrayAdapterNew sampleListAdapter;
private ArrayList<HashMap<String, String>> sampleArrayList;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.listView1);
sampleArrayList = new ArrayList<HashMap<String, String>>();
for(int i=0;i<100;i++)
{
HashMap<String, String> sampleObjectMap = new HashMap<String, String>();
sampleObjectMap.put("value1", "aaa"+i);
sampleObjectMap.put("value2", "666");
sampleObjectMap.put("value3", "1");
sampleObjectMap.put("value4", "777");
sampleObjectMap.put("value5", "444"+i);
sampleObjectMap.put("value6", "444"+i);
sampleArrayList.add(sampleObjectMap);
}
sampleListAdapter = new MySimpleArrayAdapterNew(MainActivity.this,
R.layout.cartitem, sampleArrayList);
listView.setAdapter(sampleListAdapter);
}
public class MySimpleArrayAdapterNew extends
ArrayAdapter<HashMap<String, String>> {
private Context context;
private int layoutResourceId;
private ArrayList<HashMap<String, String>> sampleArrayLists;
public MySimpleArrayAdapterNew(Context context, int layoutResourceId,
ArrayList<HashMap<String, String>> sampleArrayList) {
super(context, R.layout.cartitem, sampleArrayList);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.sampleArrayLists = sampleArrayList;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View row = convertView;
WeatherHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context)
.getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new WeatherHolder();
holder.slno = (TextView) row.findViewById(R.id.slno);
holder.itemname = (TextView) row.findViewById(R.id.itemname);
holder.qty = (EditText) row.findViewById(R.id.qty);
holder.rate = (EditText) row.findViewById(R.id.rate);
holder.seatno = (EditText) row.findViewById(R.id.seat);
holder.value = (TextView) row.findViewById(R.id.value);
row.setTag(holder);
} else {
holder = (WeatherHolder) row.getTag();
}
final HashMap<String, String> sampleObjectMap = sampleArrayLists
.get(position);
holder.itemname.setText(sampleObjectMap.get("value1"));
holder.slno.setText(sampleObjectMap.get("value2"));
holder.qty.setText(sampleObjectMap.get("value3"));
holder.rate.setText(sampleObjectMap.get("value4"));
holder.seatno.setText(sampleObjectMap.get("value5"));
holder.value.setText(sampleObjectMap.get("value6"));
holder.rate.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
final EditText Caption = (EditText) v;
String currentrate = sampleObjectMap.get("value4");
try {
if (!((sampleObjectMap.get("value4")) == null)) {
sampleObjectMap.put("value4", Caption.getText()
.toString());
int currentquantity = Integer
.parseInt(sampleObjectMap.get("value3"));
sampleObjectMap.put(
"value6",
(Double.parseDouble(sampleObjectMap
.get("value4")) * (currentquantity))
+ "");
sampleArrayList.set(position, sampleObjectMap);
}
} catch (NumberFormatException ne) {
ne.printStackTrace();
sampleObjectMap.put("value4", currentrate);
sampleArrayList.add(sampleObjectMap);
}
if (!hasFocus) {
notifyDataSetChanged();
} else if (hasFocus) {
}
}
});
holder.qty.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
final EditText Caption = (EditText) v;
try {
if (!((sampleObjectMap.get("value3")) == null)) {
sampleObjectMap.put("value3", Caption.getText()
.toString());
int currentquantity = Integer
.parseInt(sampleObjectMap.get("value3"));
sampleObjectMap.put(
"value6",
(Double.parseDouble(sampleObjectMap
.get("value4")) * (currentquantity))
+ "");
sampleArrayList.set(position, sampleObjectMap);
}
} catch (NumberFormatException ne) {
ne.printStackTrace();
sampleObjectMap.put("value3", "1");
sampleArrayList.add(sampleObjectMap);
}
if (!hasFocus) {
notifyDataSetChanged();
} else if (hasFocus) {
}
}
});
holder.seatno.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
final EditText Caption = (EditText) v;
sampleObjectMap.put("value5", Caption.getText().toString());
sampleArrayList.set(position, sampleObjectMap);
if (!hasFocus) {
notifyDataSetChanged();
} else if (hasFocus) {
}
}
});
row.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listView.setItemChecked(position, true);
}
});
return row;
}
public class WeatherHolder {
TextView slno;
TextView itemname;
EditText qty;
EditText rate;
EditText seatno;
TextView value;
}
}
}
顯示你的代碼。 – Aniruddha
我知道這段代碼亂七八糟。請嘗試通過它。 –
讓列表單元格的焦點工作。有用的鏈接 http://stackoverflow.com/a/13711066/1352919 – Faakhir