Gaurav Arora
Gaurav Arora

Reputation: 8362

Text View On Click Listener in Base Adapter

I have a list view with List View row as:

<LinearLayout
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/personName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="4dip"
        android:text="TEST Arora"
        android:textColor="@color/BLACK"
        android:textSize="@dimen/activity_big_bold_text" />

    <TextView
        android:id="@+id/distance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="4dip"
        android:text="200m away"
        android:textColor="@color/GREY"
        android:textSize="14dp" />
</LinearLayout>

<LinearLayout
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:gravity="center" >

    <TextView
        android:id="@+id/tv_confirmHelp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Confirm Help"
        android:textColor="@color/PURPLE"
        android:textSize="@dimen/activity_solo_text" />

    <ImageButton
        android:id="@+id/report_user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:src="@drawable/report_icon"
        android:layout_marginLeft="4dip"
        android:visibility="visible" />
</LinearLayout>

<ImageButton
    android:id="@+id/check_location"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@android:color/transparent"
    android:src="@drawable/notification_location" />

And My GET VIEW IS:

@SuppressWarnings("null")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    if(convertView==null){
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.notification_rowlayout, parent,false);
        holder.tv_name = (TextView)convertView.findViewById(R.id.personName);
        holder.btn_checkLocation = (ImageButton)convertView.findViewById(R.id.check_location);
        holder.tv_ConfirmHelp = (TextView)convertView.findViewById(R.id.tv_confirmHelp);



        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

holder.btn_checkLocation.setTag(position);
            holder.tv_ConfirmHelp.setTag(position);


    holder.tv_ConfirmHelp.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            int pos = (Integer) holder.tv_ConfirmHelp.getTag();
            System.out.println("Position Selectd"+pos );

            if(holder.tv_ConfirmHelp.getText().toString().equalsIgnoreCase("confirm help")){
                //TakeCareUtilities.startProgressBar(context, "Confirming. Please wait..", true, null);
                JsonProcessor jsonProcessor = JsonProcessor.getInstance();
                APIVariables apiVariables = new APIVariables();
                jsonProcessor.makeRequest(apiVariables.confirmNotification(context, "",dataList.get(pos).getPhonenum() ), context, callback,null,"GET_NOTIFICATIONS");
                Toast.makeText(context, "Alert Confirmed", 0).show();
                holder.tv_ConfirmHelp.setText("Report User");
            }else if(holder.tv_ConfirmHelp.getText().toString().equalsIgnoreCase("Report User")){
                //Toast.makeText(context, "Already Reported", 0).show();
                JsonProcessor jsonProcessor = JsonProcessor.getInstance();
                APIVariables apiVariables = new APIVariables();
                //jsonProcessor.makeRequest(apiVariables.confirmNotification(context, "",dataList.get(position).getPhonenum() ), context, callback,null,"GET_NOTIFICATIONS");
                jsonProcessor.makeRequest(apiVariables.reportaUser(context,dataList.get(pos).getPhonenum() ), context, callback,null,"GET_NOTIFICATIONS");

                holder.tv_ConfirmHelp.setText("Reported");
            }else{
                Toast.makeText(context, "Already Reported", 0).show();
                /*JsonProcessor jsonProcessor = JsonProcessor.getInstance();
                APIVariables apiVariables = new APIVariables();
                //jsonProcessor.makeRequest(apiVariables.confirmNotification(context, "",dataList.get(position).getPhonenum() ), context, callback,null,"GET_NOTIFICATIONS");
                jsonProcessor.makeRequest(apiVariables.reportaUser(context,dataList.get(position).getPhonenum() ), context, callback,null,"GET_NOTIFICATIONS");
                 */
                holder.tv_ConfirmHelp.setText("Reported");
            }
        }
    });

    holder.tv_name.setText(dataList.get(position).getName());

    /*if(dataList.get(position).getStatus().equalsIgnoreCase("confirmed")||dataList.get(position).getStatus().equalsIgnoreCase("")){
        holder.tv_ConfirmHelp.setText("Confirmed");
    }else{
        holder.tv_ConfirmHelp.setText("Confirm Help");
    }*/


    holder.btn_checkLocation.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context,TrackUserLocation.class);
            intent.putExtra("LATITUDE", dataList.get(position).getLatitude());
            intent.putExtra("LONGITUDE", dataList.get(position).getLongitude());
            intent.putExtra("PERSON_NAME", dataList.get(position).getName());
            context.startActivity(intent);
        }
    });

Here I am getting one problem, when I click on ROW 1 text view, it is not updated rather last text view on the screen is updated.

I am fed up in finding my mistake to get why I am getting wrong position. Can anyone please help

Thanks

Upvotes: 0

Views: 750

Answers (2)

Ircover
Ircover

Reputation: 2446

In the onClick instead of

int pos = (Integer) holder.tv_ConfirmHelp.getTag();

use

ViewHolder currentHolder = (ViewHolder) v.getTag();
int pos = (Integer) currentHolder.tv_ConfirmHelp.getTag();

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

Use v.getTag() to get right position :

@Override
public void onClick(View v) {
   TextView txtConfirmHelp=((TextView)v);
   int pos = (Integer) txtConfirmHelp.getTag();
   System.out.println("Position Selectd"+pos );
  //...now use `txtConfirmHelp` for setting TextView text
  ...
}

Now use txtConfirmHelp instead of holder.tv_ConfirmHelp inside onClick method to change and get text from tv_confirmHelp TextView

Upvotes: 1

Related Questions