0

所以基本上我现在需要我的程序做的是,当我单击提交按钮时,我需要循环遍历 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;
}
}
}
4

2 回答 2

1

首先,我不确定这是如何工作的:

//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());                    
            }
        }
    });

如果您尝试创建 ListItem 对象的 ArrayList,那么您应该这样做:

//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;

                ListItem litem = new ListItem();
                litem.caption = holder.caption.getText().toString();
                myItems.set(position, litem);                    
            }
        }
    });

然后从提交按钮的 OnClickListener 中,您可以执行以下操作:

    submit.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {
            ViewHolder holder;

            for(int i = 0; i < myList.getAdapter().getCount(); i++)
            {
                holder = (ViewHolder) myList.getAdapter().getItem(i);

                String temp = holder.caption.getText().toString();

                Log.e("VALUES", "" + temp);
            }
        }
    });
于 2012-09-20T03:55:49.493 回答
1

这就是我要做的:

从改变这个开始:

public ArrayList myItems = new ArrayList();

进入这个:

public HashMap<Integer, String> myItems = new HashMap<Integer, String>();

然后修改getView():

public View getView(final 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(myItems.get(postition));

    holder.indexText.setText("Index " + 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 EditText Caption = (EditText) v;
                myItems.put(position, Caption.getText().toString());                    
            }
        }
    });

    return convertView;
}

并修改 getItem(int position):

public Object getItem(int position) {
    return myItems.get(position);
}

对于测试解析,您可以使用以下内容:

    @Override
    public void onClick(View v) {
        for (int i = 0; i < myList.getCount() ; i++) {
            String item = (Strng) myAdapter.getItem(position); 
                Log.e("VALUES", "" +item);
        }
    }

还没有测试代码,所以让我知道它是如何工作的,所以如果需要我可以更正它。

于 2012-09-20T03:35:40.273 回答