0

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.

seaotternerd
  • 6,298
  • 2
  • 47
  • 58
syareen
  • 328
  • 2
  • 4
  • 15
  • 1
    intent is not working because you did not init value for ctx variable. in your constructor of the adapter, you should add : this.ctx = context; – ThaiPD May 07 '15 at 04:06
  • yeah!! you right.. thank you so much.. :) – syareen May 07 '15 at 04:31
  • Check my answer for [Single selection in RecyclerView](http://stackoverflow.com/questions/28972049/single-selection-in-recyclerview/29030776#29030776) to get idea – Xcihnegn May 07 '15 at 05:38

2 Answers2

0

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();
}
Dhaval Patel
  • 10,119
  • 5
  • 43
  • 46
0

if you want to get the id at specific position, in your adapter you should overide 2 functions belows:

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

this method you should return the object in your model at possition i example if your model is a list of object, it's named data, you should return data.get(i);

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

This method should return the id of the object at possition i, usually return i; but if you have other id as your implementation you should return your id. Example : return data.get(i).getId();

ThaiPD
  • 3,503
  • 3
  • 30
  • 48