如何在活动中传递listview中的所有数据

时间:2015-06-01 02:22:34

标签: android listview android-listview

我有2项活动。第一个包括文本视图和编辑文本以及一个按钮。那些编辑文本视图和编辑文本位于列表视图中。用于从服务器填充标题的文本视图(我还没有从服务器填充数据,现在我只创建虚拟数据),编辑文本是从用户获取数据用户。用户填写完所有编辑文本并按下按钮后,此活动中的所有数据都将传递给另一个活动。怎么做?现在,我只知道列表视图中的项目点击监听器,但那不是我想要的。如何从listview获取数据?

1.主要活动

public class MainActivity extends Activity {
ListView listview;
ListAdapter adapter;
ViewGroup group1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listview = (ListView) findViewById(R.id.listView1);

    String[] list = new String[]{
            "TItle 1",
            "TItle 2",
            "TItle 3",
            "TItle 4",
            "TItle 5"
    };

    adapter = new ListAdapter(this,list);
    listview.setAdapter(adapter);

    group1 = (ViewGroup) findViewById(R.id.mainLayout);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

static class ListAdapter extends BaseAdapter{

    private Context context;
    private String[] textString;
    private LayoutInflater vi;

    public ListAdapter(Context context, String[] textString){
        this.context = context;
        this.textString = textString;
        vi = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return textString.length;
    }

    @Override
    public Object getItem(int position) {
        return textString[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ViewHolder VH = new ViewHolder();
        v = vi.inflate(R.layout.item_list_view, null);

        EditText et1 = (EditText) v.findViewById(R.id.editText);
        et1.setTag("et1" + position);


        VH.tv = (TextView) v.findViewById(R.id.txtTitle);
        VH.tv.setText(textString[position]);

        return v;
    }

    class ViewHolder{
        TextView tv;
    }
}
  1. activity_main.xml中

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/btn1">
    
    </ListView>
    
    
    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>
    
  2. activity_retrieve.xml

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"/>
    

  3. 活动检索

    public class RetrieveActivity extends Activity {
    
    TextView txtTemp;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrieve);
        txtTemp = (TextView) findViewById(R.id.text1);
    }
    
    
    }
    

2 个答案:

答案 0 :(得分:2)

在ListView适配器中,为将用于启动另一个活动的按钮添加onClickListener()。

首先创建一个ArrayList,保存数组数据,

ArrayList<String> allArray = new ArrayList<String>();

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ViewHolder VH = new ViewHolder();
        v = vi.inflate(R.layout.item_list_view, null);

        EditText et1 = (EditText) v.findViewById(R.id.editText);
        et1.setTag("et1" + position);


        VH.tv = (TextView) v.findViewById(R.id.txtTitle);
        VH.tv.setText(textString[position]);

        Button b = (Button) v.findViewById(R.id.btnSubmit);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            // user has click on the button now get the data from the EditText
            // and pass it to the new Activity

            allArray.add(et1.get(position).toString());
            Intent i = new Intent (getApplicationContext, RetrieveActivity.class);
            i.putStringArrayListExtra("edit_key_name", value);
            StartActivity(i);

            }
        });


        return v;
    }

现在,从RetrieveActivity类中获取这样的Intent数据,

 Intent i = getIntent();
 ArrayList<String> passedText = i.getStringArrayListExtra("edit_key_name");

同样,您可以根据需要传递数据。

答案 1 :(得分:0)

在您的案例中,在列表视图中编辑文本内容的方法:

for(int i = 0; i < list.size(); i ++){
    EditText editText = (EditText) adapter.getView(i, null, null).findViewById(R.id.editText);
    //save current text to list by editText.getText()
}

存储到列表/数组后,使用putExtra发送到新活动