使用intent将数据从clicklistener发送到活动

时间:2014-08-08 17:14:06

标签: android android-intent

我在自定义适配器中放置了一个按钮单击侦听器,单击该按钮后,我打开一个活动并通过intent向该活动发送一些数据,但它没有获取值,每次都为零。 这是我的点击监听器。

comments.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int p=(Integer)upvote.getTag();
                Intent i = new Intent(ctx, Comments.class);
                i.putExtra("taskid",tasks.get(p)
                        .get_id());
                i.putExtra("torc",1);
                ctx.startActivity(i);
            } 

这是获取这些值的活动

public class Comments extends Activity {
int taskid,torc;
Comments_DAO cd;
CAforComments ca;
SharedPreferences sf;
int userId;
ArrayList<String> s;
ArrayList<Comments_table> comments;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_comments);
    sf=this.getSharedPreferences("SP", Context.MODE_PRIVATE);
    comments = new ArrayList<Comments_table>();
     s = new ArrayList<String>();
    ListView lv = (ListView) findViewById(R.id.listView1);
    cd = new Comments_DAO(this);
    Intent i = getIntent();
    i.getIntExtra("taskid", taskid);
    i.getIntExtra("torc", torc);
    Toast.makeText(this,""+taskid+" "+torc,Toast.LENGTH_SHORT).show();
    comments = cd.getComments(taskid,torc);
    for(int i1=0;i1<comments.size();i1++)
        s.add("");
    ca = new CAforComments(this, comments, s);
    lv.setAdapter(ca);
}

public void comment(View view) {
    EditText et1 = (EditText) findViewById(R.id.editText1);
    String s1 = new String();
    s1 = et1.getText().toString();
    Comments_table ct=cd.createComment(s1,sf.getInt("userId",userId),torc,taskid);
    et1.setText("");
    comments.add(ct);
    s.add("");
    ca.notifyDataSetChanged();
}

}

2 个答案:

答案 0 :(得分:0)

i.getIntExtra("taskid", taskid);
i.getIntExtra("torc", torc);

更改为

taskid=i.getIntExtra("taskid", taskid);
torc=i.getIntExtra("torc", torc);

答案 1 :(得分:0)

更改这些

Intent i = getIntent();
i.getIntExtra("taskid", taskid);
i.getIntExtra("torc", torc);

Bundle b = getIntent().getExtras();     
taskid= b.getInt("taskid");
torc= b.getInt("torc");