MainActivitysentencerecording

MainActivitysentencerecording

MainActivitymp3内部有带有Edittext的listview。我希望长按每个编辑文本时会显示新的MainActivitysentencerecording,然后单击关闭按钮上的值(5)放入MainActivitymp3中。
android - 从另一个 Activity 的列表文本的“编辑”文本中获取值(value)-LMLPHP
 这是MainActivitymp3活动的MycustomerAdater类

public class MyCustomAdapter extends BaseAdapter {
HashMap<String, String> newupdatelist = new HashMap<String, String>();
public Context context;

public static ArrayList<Song> listsong;
private DatabaseSQL datab;
private final String TAG = getClass().getSimpleName();

public MyCustomAdapter(Context context, ArrayList<Song> objects) {
    this.context=context;
    this.listsong=objects;
}
...
 public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder viewHolder;
    if(convertView==null){
        convertView = LayoutInflater.from(context).inflate(R.layout.song_items,parent,false);
        viewHolder = new ViewHolder();
        viewHolder.tenbai = (TextView) convertView.findViewById( R.id.text);
        viewHolder.delaytime = (EditText) convertView.findViewById(R.id.delay);
        viewHolder.timeunit = (TextView) convertView.findViewById(R.id.timeunit);
        convertView.setTag(viewHolder);
    }
    else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    Song infor = listsong.get(position);
    viewHolder.tenbai.setText(infor.getTenbai());
    viewHolder.timeunit.setText(infor.getTimeunit());
    viewHolder.delaytime.setText(infor.getDelaytime());


    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (context instanceof MainActivitymp3) {
                //play selection song
 ((MainActivitymp3)context).playMedia(listsong.get(position).getDuongdan().toString());
            }

        }
    });
    viewHolder.delaytime.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
  // update new value from edittext to listsong array
         listsong.get(position).setDelaytime(viewHolder.delaytime.getText().toString() );
         }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override
        public void afterTextChanged(Editable s) {

        }
    });
    viewHolder.delaytime.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "vi tri"+ position, Toast.LENGTH_LONG).show();
            Intent intend = new Intent(context, MainActivitysentencerecording.class);
            Activity origin = (Activity)context;
            origin.startActivityForResult(intend, 200);
       // Deleted these below lines as @David Wasser answered
          //int x=  ((MainActivitymp3)context).gio;
           // viewHolder.delaytime.setText(String.valueOf( x ));

            return true;
    });
    return convertView;
   }

public class ViewHolder{
    TextView tenbai;
    EditText delaytime;
    TextView timeunit;
 }
 }


这是MainActivitymp3类

public class MainActivitymp3 extends AppCompatActivity {
  ...
 public  int gio;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    ( new MyCustomAdapter( this, fileNames )).onActivityResult(requestCode, resultCode, data);
    if(resultCode == Activity.RESULT_OK){
        if(requestCode == 200){
            // here comes the data from activity
            gio = data.getIntExtra("timedelaysetting",0);
            Log.d("MyAdapter", "onActivityResult  "+gio);

        }
    }
}


这是MainActivitysentencerecording活动

public class MainActivitysentencerecording extends AppCompatActivity {
 ...
//close button click
 Intent resultIntent = new Intent();
            resultIntent.putExtra("timedelaysetting", sec);
            setResult( Activity.RESULT_OK, resultIntent);
            finish();
}


请伸出援助之手解决此问题。我正在寻找您的来信。谢谢

最佳答案

您似乎不了解从另一个启动一个Activity是如何工作的。在您的longclick监听器中,您具有以下代码:

origin.startActivityForResult(intend, 200);
int x=  ((MainActivitymp3)context).gio;
viewHolder.delaytime.setText(String.valueOf( x ));


这意味着您认为在调用startActivityForResult()时,控件会立即传递给MainActivitysentencerecording,并且下一行代码(int x= ((MainActivitymp3)context).gio;)仅在MainActivitysentencerecording完成和完成之后才执行。

那不是它的工作原理。当您呼叫startActivityForResult()时,您只是在要求Android框架重新获得控制权时启动目标Activity(在本例中为MainActivitysentencerecording)。接下来的几行代码在调用startActivityForResult()之后立即执行,甚至在启动MainActivitysentencerecording并显示给用户之前。

您只需在调用return之后仅startActivityForResult(),然后Android框架将重新获得控制权并启动MainActivitysentencerecordingActivity完成后,Android框架将调用MainActivitymp3.onActivityResult()。这是异步发生的,并且比您想象的要晚得多。您只能在调用onActivityResult()后更新UI。

08-18 16:49