如何从Intent View Android传递Intent extra

时间:2015-03-02 09:41:13

标签: android android-intent

我正在AdapterView使用ListView,我们可以找到列表视图的活动和EditText

当我们点击ListView的一个元素时,我们转到第二个活动,并且我想在intent中将所选项目的数据作为额外项目发送。此数据将是显示在EditText

我尝试了这段代码,但它似乎不起作用,它始终显示EditText的默认值。

这是适配器视图的代码:

public class adapterq extends ArrayAdapter<Questionaire> implements OnClickListener {
    Bitmap image;
      EditText name;

    public adapterq(Context context, ArrayList<Questionaire> questionaires) {
       super(context, 0, questionaires);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       // Get the data item for this position
       final Questionaire c = getItem(position);    
       // Check if an existing view is being reused, otherwise inflate the view
       if (convertView == null) {
          convertView = LayoutInflater.from(getContext()).inflate(R.layout.customquest, parent, false);
       }

     View  convertView2 = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);


       TextView q = (TextView) convertView.findViewById(R.id.textView1);
 name =     (EditText) convertView2.findViewById(R.id.editText1);


       q.setText(c.getLabel()); 

       convertView.setOnClickListener(this);

       return convertView;
   }
    @Override
    public void onClick(View v) {
          Intent intent = new Intent();

            intent.setClass(getContext(), Questions.class);

        intent.putExtra("name", name.getText().toString());
            getContext().startActivity(intent);  

    }


}

这是主要活动的xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context="com.example.orange.v1.MainActivity" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="270dp"
    android:layout_height="250dp"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
     android:isScrollContainer="false"
    android:layout_marginTop="16dp" >

</ListView>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignParentTop="true"
    android:layout_marginTop="18dp"

    android:text="User Identifier :"
    android:textStyle="bold" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="16dp"
    android:text="Yass"
    android:ems="10" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText1"
    android:layout_marginLeft="21dp"
    android:layout_marginTop="36dp"
    android:text="Questionaire Choice"
    android:textStyle="bold" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/listView1"
    android:layout_below="@+id/listView1"
    android:layout_marginTop="26dp"
    android:text="Select a category to start playing" />

使用视图的屏幕截图:

enter image description here

2 个答案:

答案 0 :(得分:2)

  

如何从Intent View Android传递Intent extra

因为EditText位于convertView2布局中,但点击监听器位于convertView中,请执行以下操作:

q.setText(c.getLabel()); 
convertView.setOnClickListener(this);
convertView.setTag(convertView2);

使用v方法的onClick参数获取点击的EditText值:

@Override
    public void onClick(View v) {
          Intent intent = new Intent();
          intent.setClass(getContext(), Questions.class);
          View view=(View)v.getTag();
          EditText edtName=(EditText)view.findViewById(R.id.editText1);
          intent.putExtra("name", edtName.getText().toString());
           getContext().startActivity(intent);  

    }

答案 1 :(得分:1)

按如下方式更新您的代码:

public class adapterq extends ArrayAdapter<Questionaire> implements OnClickListener {
Bitmap image;
EditText name;

public adapterq(Context context, ArrayList<Questionaire> questionaires, EditText name) {
   super(context, 0, questionaires);
   this.name=name;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
   // Get the data item for this position
   final Questionaire c = getItem(position);    
   // Check if an existing view is being reused, otherwise inflate the view
   if (convertView == null) {
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.customquest, parent, false);
   }

 TextView q = (TextView) convertView.findViewById(R.id.textView1);


   q.setText(c.getLabel()); 

   convertView.setOnClickListener(this);

   return convertView;
}

@Override
public void onClick(View v) {
      Intent intent = new Intent();

        intent.setClass(getContext(), Questions.class);

    intent.putExtra("name", name.getText().toString());
        getContext().startActivity(intent);  

}

}

并在初始化arrayAdapter时传递name实例