android传递参数到onclick事件

时间:2012-09-26 11:06:49

标签: android android-intent

在下面的代码中,如何将pwd传递给onclick事件

String pwd ="/root";   

final ListView lv = (ListView) findViewById(R.id.list);
lv.setOnItemClickListener(new OnItemClickListener() {   
  public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng)
        Toast.makeText(MainActivity.this,pwd , Toast.LENGTH_SHORT).show();
    }

编辑1:

final String pwd = null;

string pwd ="root"; //Error remove final keyword
final ListView lv = (ListView) findViewById(R.id.list);
lv.setOnItemClickListener(new OnItemClickListener() {   
   public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng)
        Toast.makeText(MainActivity.this,pwd , Toast.LENGTH_SHORT).show();
    }

4 个答案:

答案 0 :(得分:1)

根据MAC建议将pwd声明为Globle解决了问题@MAC

答案 1 :(得分:0)

在onCreate方法之上声明你的String pwd。

class demo extends Activity
{String pwd;
onCreate()
{pwd="/root";
final ListView lv = (ListView) findViewById(R.id.list);

lv.setOnItemClickListener(new OnItemClickListener() {   
public void onClick(View arg0) {
Toast.makeText(MainActivity.this,pwd , Toast.LENGTH_SHORT).show();

    }
        }

答案 2 :(得分:0)

这就是我曾经做过的一次

public class NatigationOnClick implements OnClickListener {

    String title;
    String html;
    private QuickActionWeb webQuickAction;
    private Context context;
    private View row;

    public NatigationOnClick(String title, String html, Context context,
            View row) {
        this.title = title;
        this.html = html;
        this.context = context;
        this.row = row;
    }

    public void onClick(View v) {

        webQuickAction = new QuickActionWeb(context, QuickActionWeb.VERTICAL,
                title + "<br/>" + html);
        webQuickAction.show(row);

    }

}

现在...

view.setOnClickListener(new NatigationOnClick(title, html, context, row));

希望有帮助

答案 3 :(得分:0)

也许这会对你有所帮助:

final ListView lv = (ListView) findViewById(R.id.list);
lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity.this, getPwd(), Toast.LENGTH_SHORT).show();
        }
});


String pwd = "root";

private String getPwd()
{
    return pwd;
}