以下是带适配器的列表视图的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_online_users);
final ListView listview = (ListView) findViewById(R.id.listviewusers);
//onlineUsers.setAdapter()
//final ListView listview = (ListView) findViewById(R.id.listview);
Intent intent = getIntent();
String username = intent.getStringExtra(connection.SERVER_ONLINE_USERS);
String[] values = username.split(",");
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
// view.setAlpha(1);
}
});
}
我想要做的是获取将在listview中单击的String项,启动一个新的intent并将字符串项传递给它,
类似于:
Intent i = new Intent(this, activity.class);
sign.putExtra(KEY, value);
startActivity(i);
但是当我在Intent
中使用上述onItemClickListener()
代码时,我收到错误。
"The Constructor Intent(New AdapterView.OnItemClickListener(){}, Class<activity>) is undefined"
建议的修复方法是从意图中删除参数并将其设置为
Intent i = new Intent();
答案 0 :(得分:2)
我对这个问题很困惑,因为你似乎知道该怎么做。所以我们把它放在一起
@Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
// view.setAlpha(1);
Intent i = new Intent(view.getContext(), activity.class); // get a valid context
i.putExtra("someKey", item); //I don't know where sign came from
startActivity(i);
}
然后在Activity
中,例如在onCreate()
中
Intent itent = getIntent();
String value = intent.getStringExtra("someKey");