I was having some problem when I try to get the selected row from list view to do something upon the button onClickListener.
Basically I have a button for each listview row. When the button is selected, it will navigate to QR code scan intent. When the result returned at the onActivityResult(), I wanted to do something with that row. This is how I set up the button listener:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.attendee_listview_row,
null);
viewHolder = new ViewHolder();
viewHolder.txt_dName = (TextView) convertView
.findViewById(R.id.txtDisplayName);
viewHolder.btn_scan = (ImageView) convertView.findViewById(R.id.btnScan);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txt_dName.setText(attendeeList.get(position)
.getAccountName().trim());
viewHolder.btn_scan.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
});
And here is the onActivityResult():
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == Activity.RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
if(attendeeName.equals("Andrew") && contents.equals("QRCode1")){
}else{
Toast.makeText(context, "Event QR code does not match.",
Toast.LENGTH_LONG).show();
}
} else if (resultCode == Activity.RESULT_CANCELED) {
}
}
}
I already made an if else statement inside the onActivityResult but I not sure how to get the selected row previously.
Any guides?
Thanks in advance.
UPDATES
Here is the llist view row item xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
android:weightSum="1" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:orientation="vertical" >
<ImageView
android:id="@+id/btnScan"
android:layout_width="40dp"
android:layout_height="40dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:src="@drawable/qr_icon" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:orientation="vertical" >
<TextView
android:id="@+id/txtDisplayName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/lightred"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:orientation="vertical" >
<ImageView
android:id="@+id/ivRegisteredTag"
android:layout_width="80dp"
android:layout_height="30dp"
android:paddingTop="15dp"
anddroid:visibility="gone"
android:src="@drawable/registered" />
</LinearLayout>
</LinearLayout>
So basically I have a registered image for each row. Then inside the if statement, let's say the conditons are matched, how do I should the registered image for that row?