从多列的ListView android的调用活动ListView、android

2023-09-12 05:56:35 作者:朝歌夜弦

在我的项目,我有一个多列的ListView的活动。这ListView控件从我在一个单独的Java模块已经实现了自定义的CursorAdapter绘制其数据。我有一对夫妇的ListView的行内观点的听众,而这些都是CursorAdapter的内实施。一位听众的需要编辑调用它的内容来看,后保存数据的基础数据库。该编辑需要startActivityForResult(如自定义对话框)。不过,我得到一个错误作为一项活动,只能从其他活动中调用。我试图移动startActivityForResult到父活动的方法,但是这有然后是一个静态过程从听者叫我得到一个错误的startActivityForResult不能在一个静态过程。该错误是 该方法startActivityForResult(意向,INT)是未定义的类型新View.OnClickListener(){}

有没有人从一个视图监听器,其中的观点是一个ListView的行元素调用一个活动的程序?

在code以下是我用我的CursorAdapter的过程。

 公共类CustomCursorAdapter扩展的CursorAdapter {

受保护的静态类RowViewHolder {
    公众按钮btnLap;
    公众的TextView tvtime的;
}

公共CustomCursorAdapter(上下文的背景下,光标C){
    超(背景下,C);
}

@覆盖
公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){
    LayoutInflater充气= LayoutInflater.from(parent.getContext());
    查看retView = inflater.inflate(R.layout.single_row_item,父母,假);

    RowViewHolder持有人=新RowViewHolder();
    holder.btnLap =(按钮)retView.findViewById(R.id.btn_lap);
    holder.tvTime =(TextView中)retView.findViewById(R.id.tv_time);
    holder.btnLap.setOnClickListener(btnLapOnClickListener);
    holder.tvTime.setOnClickListener(tvTimeOnClickListener);

    retView.setTag(保持器);

    返回retView;
}

...

私人OnClickListener tvTimeOnClickListener =新OnClickListener(){
    @覆盖
    公共无效的onClick(视图v){
        TextView的电视=(TextView中)V;
        。字符串strTime = tv.getText()的toString();
        意图int​​entTimeEdit =新的意图(getBaseContext(),TimeEditDialog.class);
        intentTimeEdit.putExtra(时代,strTime);
        startActivityForResult(intentTimeEdit,EDIT_TIME_REQUEST_ code);
    }
};
 

按照下面的评论中,code为OnClickListener已更正为:

  TextView的电视=(TextView中)V;
        。字符串strTime = tv.getText()的toString();
        RowViewHolder持有人=新RowViewHolder();
        支架=(RowViewHolder)((视图)v.getParent())getTag()。
        意图int​​entTimeEdit =新的意图(holder.ctx,TimeEditDialog.class);
        intentTimeEdit.putExtra(时代,strTime);
        ((活动)holder.ctx).startActivityForResult(intentTimeEdit,EDIT_TIME_REQUEST_ code);
 

解决方案

的方法startActivityForResult(意向,INT)是未定义的类型新View.OnClickListener(){} 它变得清晰,startActivityForResult或startActivity只能称为如果类扩展Acitvity ..它甚至不会接受内部类里面......

尝试

((活动)getBaseContext())startActivityForResult(intentTimeEdit,EDIT_TIME_REQUEST_ code); 并检查结果

android listview –

Within my project I have an activity with a multi-column ListView. This ListView draws its data from a custom CursorAdapter that I've implemented in a separate java module. I have listeners on a couple of the views within the ListView's rows, and these are implemented within the CursorAdapter. One of the listeners needs to edit the view content that called it and save the data back to the underlying database. This editing needs to startActivityForResult (as a custom dialog). However I get an error as an activity can only be invoked from another activity. I have tried moving the startActivityForResult to a procedure in the parent activity, but this has then to be a static procedure to be called from the listener and I get an error as startActivityForResult can't be in a static process. The error is "The method startActivityForResult(Intent, int) is undefined for the type new View.OnClickListener(){}"

Has anyone a process for invoking an activity from a view listener where the view is a row element of a ListView?

The code below is the process I'm using in my CursorAdapter.

public class CustomCursorAdapter extends CursorAdapter {

protected static class RowViewHolder {
    public Button btnLap;
    public TextView tvTime;
}

public CustomCursorAdapter(Context context, Cursor c) {
    super(context, c);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View retView = inflater.inflate(R.layout.single_row_item, parent, false);

    RowViewHolder holder = new RowViewHolder();
    holder.btnLap = (Button) retView.findViewById(R.id.btn_lap);
    holder.tvTime = (TextView) retView.findViewById(R.id.tv_time);
    holder.btnLap.setOnClickListener(btnLapOnClickListener);
    holder.tvTime.setOnClickListener(tvTimeOnClickListener);

    retView.setTag(holder);

    return retView;
}

...

private OnClickListener tvTimeOnClickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        TextView tv = (TextView) v;
        String strTime = tv.getText().toString();
        Intent intentTimeEdit = new Intent(getBaseContext(), TimeEditDialog.class);
        intentTimeEdit.putExtra("Time", strTime);
        startActivityForResult(intentTimeEdit, EDIT_TIME_REQUEST_CODE);
    }
};

As per comment below, the code for the OnClickListener has been corrected to:

        TextView tv = (TextView) v;
        String strTime = tv.getText().toString();
        RowViewHolder holder = new RowViewHolder();
        holder = (RowViewHolder) ((View) v.getParent()).getTag();
        Intent intentTimeEdit = new Intent(holder.ctx, TimeEditDialog.class);
        intentTimeEdit.putExtra("Time", strTime);
        ((Activity)holder.ctx).startActivityForResult(intentTimeEdit, EDIT_TIME_REQUEST_CODE);

解决方案

From The method startActivityForResult(Intent, int) is undefined for the type new View.OnClickListener(){} its becomes clear that startActivityForResult or startActivity can be called only if class extends Acitvity.. It won't even accept inside inner class...

try

((Activity)getBaseContext()).startActivityForResult(intentTimeEdit, EDIT_TIME_REQUEST_CODE); and check the result.