Reputation: 9364
I'm using a AdapterView
for a ListView
, the activity where we can find the list view and an EditText
.
When we click on one element of the ListView
we go to the second activity, and I want to send the data of the selected item as an extra in a intent
.This data will then be shown in the EditText
I tried this code, but it doesn't seem to work, it shows always the default value of the EditText
.
here is the code of the adapter view :
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);
}
}
here is the xml code of the main activity :
<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" />
With a screenshot of the view :
Upvotes: 0
Views: 570
Reputation: 896
update your code as follows:
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);
}
}
and pass the instance of name while initializing the arrayAdapter
Upvotes: 1
Reputation: 132982
How to pass an Intent extra from the Intent View Android
Because EditText
is inside convertView2 layout but click listener is in convertView
do it as:
q.setText(c.getLabel());
convertView.setOnClickListener(this);
convertView.setTag(convertView2);
Use v
parameter of onClick
method to get clicked EditText
value:
@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);
}
Upvotes: 2