Search code examples
androidquickblox

How to get value from the selected row (android studio)


I am new to android studio.

I have a list of rows, each of which has a button. Once the button is clicked, it will be able to update the data in the database(quickblox). However, the update code for quickblox does not work if I don't give it the id selected. How can I get the id?

Here is my code:

public class ListAdapter1 extends BaseAdapter  {

public Context ctx;
private LayoutInflater layoutInflater;

public ListAdapter1(Context context) {
    this.layoutInflater = LayoutInflater.from(context);
}


@Override
public int getCount() {
    return connectorDA.getDataHolder().getNoteListSize();
}

@Override
public Object getItem(int i) {
    return null;
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder;

    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.activity_list_adapter1, null);
        viewHolder = new ViewHolder();
        viewHolder.namesTextView = (TextView) convertView.findViewById(R.id.name_textview);                
        viewHolder.distancesTextView = (TextView) convertView.findViewById(R.id.distances_textview);
        viewHolder.timesView = (TextView) convertView.findViewById(R.id.times_textview);
        viewHolder.accept = (Button) convertView.findViewById(R.id.buttonAccept);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }



    viewHolder.accept.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            final double userId = connectorDA.getDataHolder().getSignInUserId();
            HashMap<String, Object> fields = new HashMap<String, Object>();
            fields.put(pass_taxiID, userId);

            QBCustomObject qbCustomObject = new QBCustomObject();
            qbCustomObject.setClassName(class_passenger);
            qbCustomObject.getId();   // here i should put the selected id
            qbCustomObject.setFields(fields);


            QBCustomObjects.updateObject(qbCustomObject, new QBEntityCallbackImpl<QBCustomObject>() {
                @Override
                public void onSuccess(QBCustomObject qbCustomObject, Bundle bundle) {

                    passengerDA.getDataHolder().addPassToList(qbCustomObject);
                    qbCustomObject.getId();
                    goToTrack();
                }

                @Override
                public void onError(List<String> errors) {

                    //DialogUtils.showLong(BaseActivity, errors.get(0));
                }
            });

        }
    });

    applyDistances(viewHolder.distancesTextView, position);
    applyTimes(viewHolder.timesView, position);
    applyId(viewHolder.namesTextView, position);

    return convertView;
}

private static class ViewHolder {

    TextView namesTextView;
    TextView distancesTextView;
    TextView timesView;
    Button accept;
}



private void applyId(TextView status, int position) {
    status.setText(connectorDA.getDataHolder().getId(position));
}

private void applyDistances(TextView status, int position) {
    status.setText(connectorDA.getDataHolder().getDistances(position));
}

private void applyTimes(TextView date, int position) {
    date.setText(connectorDA.getDataHolder().getTimes(position));
}

public void goToTrack()
{
    Intent intent = new Intent(ctx,MapsActivityTrack.class);
    ctx.startActivity(intent);


}

}

My intent is also not working. If I press the button, it will unfortunately stop because my intent returns a null exception.


Solution

  • Here You can use View Tag (Tags are essentially an extra piece of information that can be associated with a view).

    viewHolder.accept.setTag(connectorDA.getDataHolder().getId(position));
    

    And In your onClick Method:

    @Override
    public void onClick(View v) {
         String id = (String)v.getTag();
    }