所以基本上我现在需要我的程序做的是,当我单击提交按钮时,我需要循环遍历 ListView 的每个动态行并获取 EditText 视图中的值,然后将其插入到我将之后做进一步的计算。这是我现在的代码。
package com.hello_world;
import java.util.ArrayList;
import com.hello_world.ByteInputActivity.MyAdapter.ViewHolder;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class ByteInputActivity extends Activity {
private ListView myList;
private MyAdapter myAdapter;
private Integer resQuestions;
private Integer indexVal = 0;
private View caption;
ViewHolder holder;
ArrayList<Integer> intArrayList = new ArrayList<Integer>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fieldlist);
//Gets number of questions from MainActivity
Bundle extras = getIntent().getExtras();
if(extras !=null) {
resQuestions = extras.getInt("index");
}
myList = (ListView) findViewById(R.id.FieldList);
myList.setItemsCanFocus(true);
myAdapter = new MyAdapter();
myList.setAdapter(myAdapter);
Button submit = (Button) findViewById(R.id.btn_New);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < myList.getCount() ; i++) {
View vListSortOrder;
vListSortOrder = myList.getChildAt(i);
String temp = holder.caption.getText().toString();
Log.e("VALUES", "" +temp);
}
}
});
}
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ArrayList myItems = new ArrayList();
public MyAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < resQuestions; i++) {
ListItem listItem = new ListItem();
listItem.caption = "Index " + i;
listItem.indexText = "Index " + i;
myItems.add(listItem);
indexVal += 1;
}
notifyDataSetChanged();
}
public int getCount() {
return myItems.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item, null);
holder.indexText = (TextView) convertView .findViewById(R.id.textView1);
holder.caption = (EditText) convertView .findViewById(R.id.ItemCaption);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//Fill EditText with the value you have in data source
holder.caption.setText("");
holder.caption.setId(position);
holder.indexText.setText("Index " + position);
holder.indexText.setId(position);
//we need to update adapter once we finish with editing
holder.caption.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
final int position = v.getId();
final EditText Caption = (EditText) v;
myItems.set(position, Caption.getText().toString());
}
}
});
return convertView;
}
class ViewHolder {
EditText caption;
TextView indexText;
}
class ListItem {
String caption;
String indexText;
}
}
}