如何从所选行中获取值(android studio)

时间:2022-12-03 10:09:30

I am new to android studio.

我是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?

我有一个行列表,每个行都有一个按钮。单击按钮后,它将能够更新数据库中的数据(quickblox)。但是,如果我没有为其选择id,则quickblox的更新代码不起作用。我怎样才能获得身份证?

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.

我的意图也没有用。如果我按下按钮,它将不幸停止,因为我的意图返回一个空异常。

2 个解决方案

#1


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

在这里您可以使用View Tag(标签本质上是一个可以与视图关联的额外信息)。

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

And In your onClick Method:

在你的onClick方法:

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

#2


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

如果你想在特定位置获取id,在你的适配器中你应该覆盖以下2个函数:

  • Frist@Override public Object getItem(int i) { return null; }
  • 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);

这个方法你应该在你的模型中返回对象,例如,如果你的模型是一个对象列表,它是命名数据,你应该返回data.get(i);

  • Second @Override public long getItemId(int i) { return 0; }
  • 第二个@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();

此方法应该返回对象的id,通常返回i;但如果你有其他身份证作为你的实现,你应该返回你的身份。示例:return data.get(i).getId();

#1


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

在这里您可以使用View Tag(标签本质上是一个可以与视图关联的额外信息)。

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

And In your onClick Method:

在你的onClick方法:

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

#2


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

如果你想在特定位置获取id,在你的适配器中你应该覆盖以下2个函数:

  • Frist@Override public Object getItem(int i) { return null; }
  • 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);

这个方法你应该在你的模型中返回对象,例如,如果你的模型是一个对象列表,它是命名数据,你应该返回data.get(i);

  • Second @Override public long getItemId(int i) { return 0; }
  • 第二个@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();

此方法应该返回对象的id,通常返回i;但如果你有其他身份证作为你的实现,你应该返回你的身份。示例:return data.get(i).getId();