3 回答
TA贡献1794条经验 获得超8个赞
我看不到你在哪里申报context。出于意图创建的目的,您可以使用MainActivity.this
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(MainActivity.this, SendMessage.class);
String message = "abc";
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});
单击后要检索对象,可以使用AdapterView:
ListEntry entry = (ListEntry) parent.getItemAtPosition(position);
TA贡献1776条经验 获得超12个赞
ListView具有Item单击侦听器回调。您应该在onItemClickListener中设置ListView。回调包含AdapterView和position作为参数。哪个可以给你ListEntry。
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
ListEntry entry= (ListEntry) parent.getAdapter().getItem(position);
Intent intent = new Intent(MainActivity.this, SendMessage.class);
String message = entry.getMessage();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});
TA贡献1785条经验 获得超4个赞
如您所说,此语句中的代码中出现错误
Intent intent = new Intent(context, SendMessage.class);
这是由于您向Intent构造函数中提供了OnItemClickListener匿名类的上下文,但根据Intent的构造函数
android.content.Intent.Intent(Context packageContext, Class<?> cls)
您必须提供您使用Intent的活动上下文,即MainActivity类上下文。因此,出现错误的语句将被转换为
Intent intent = new Intent(MainActivity.this, SendMessage.class);
另外,要将您的消息从此MainActivity发送到SendMessage类,请参见以下代码
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
ListEntry entry= (ListEntry) parent.getAdapter().getItem(position);
Intent intent = new Intent(MainActivity.this, SendMessage.class);
intent.putExtra(EXTRA_MESSAGE, entry.getMessage());
startActivity(intent);
}
});
请告诉我这是否对您有帮助
编辑:- 如果您发现某些问题以获得列表的值,请执行一件事清除您的数组列表
ArrayList<ListEntry> members = new ArrayList<ListEntry>();
全局,即在oncreate和更改您的监听器之前,如下所示
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(MainActivity.this, SendMessage.class);
intent.putExtra(EXTRA_MESSAGE, members.get(position));
startActivity(intent);
}
});
所以你的整个代码看起来像
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.ListViewTest.MESSAGE";
ArrayList<ListEntry> members = new ArrayList<ListEntry>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
members.add(new ListEntry("BBB","AAA",R.drawable.tab1_hdpi));
members.add(new ListEntry("ccc","ddd",R.drawable.tab2_hdpi));
members.add(new ListEntry("assa","cxv",R.drawable.tab3_hdpi));
members.add(new ListEntry("BcxsadvBB","AcxdxvAA"));
members.add(new ListEntry("BcxvadsBB","AcxzvAA"));
members.add(new ListEntry("BcxvBB","AcxvAA"));
members.add(new ListEntry("BvBB","AcxsvAA"));
members.add(new ListEntry("BcxvBB","AcxsvzAA"));
members.add(new ListEntry("Bcxadv","AcsxvAA"));
members.add(new ListEntry("BcxcxB","AcxsvAA"));
ListView lv = (ListView)findViewById(R.id.listView1);
Log.i("testTag","before start adapter");
StringArrayAdapter ad = new StringArrayAdapter (members,this);
Log.i("testTag","after start adapter");
Log.i("testTag","set adapter");
lv.setAdapter(ad);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(MainActivity.this, SendMessage.class);
intent.putExtra(EXTRA_MESSAGE, members.get(position).getMessage());
startActivity(intent);
}
});
}
其中getMessage()是在ListEntry类中指定的getter方法,用于获取先前设置的消息。
- 3 回答
- 0 关注
- 466 浏览
添加回答
举报