如何在列表视图中单击ImageButton时打开新活动?目前我的OnClickListener在我的ArrayAdapter类中,但从那里我无法开始新的活动。那么我怎样才能在MainActivity中获得OnClickListener以及如何在那里处理" name"和" ID"?
继承我的ArrayAdapter:
public class MessageArrayAdapter extends ArrayAdapter<MessageHome> {
private TextView playername;
private ImageView profile;
private ImageButton chat;
private List<MessageHome> MessageList = new ArrayList<MessageHome>();
private LinearLayout singleMessageContainer;
@Override
public void add(MessageHome object) {
MessageList.add(object);
super.add(object);
}
public MessageArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public int getCount() {
return this.MessageList.size();
}
public MessageHome getItem(int index) {
return this.MessageList.get(index);
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.activity_chat_singlemessage, parent, false);
}
singleMessageContainer = (LinearLayout) row.findViewById(R.id.singleMessageContainer);
MessageHome chatMessageObj = getItem(position);
playername = (TextView) row.findViewById(R.id.playername_);
playername.setText(chatMessageObj.name);
final String ID = chatMessageObj.ID;
final String name = chatMessageObj.name;
profile = (ImageView) row.findViewById(R.id.imageView2);
if(chatMessageObj.newm == 1)
profile.setImageResource(R.drawable.player);
else
profile.setImageResource(R.drawable.player2);
chat = (ImageButton) row.findViewById(R.id.ib_message);
chat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("log_tag",name+ID);
// Here start new activity, but doesn't work here
}
});
return row;
}
}
这是我的MainActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getIntent();
setContentView(R.layout.activity_message);
listView = (ListView) findViewById(R.id.listView1);
messageArrayAdapter = new MessageArrayAdapter(getApplicationContext(), R.layout.activity_message_single);
listView.setAdapter(messageArrayAdapter);
listView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
listView.setAdapter(messageArrayAdapter);
//to scroll the list view to bottom on data change
messageArrayAdapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
listView.setSelection(messageArrayAdapter.getCount() - 1);
}
});
}
感谢您的帮助;)
答案 0 :(得分:3)
所有Views
都有Context
:
chat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("log_tag",name+ID);
Intent intent = new Intent(v.getContext(), NextActivity.class);
v.getContext().startActivity(intent);
}
});
答案 1 :(得分:1)
尝试将以下行放在您的activity_chat_singlemessage.xml
中android:onClick="nameOfFunction"
并将此功能放入您的活动中
public void nameOfFunction(final View view){
startActivity(new Intent(this, YourNewActivity.class));
}
答案 2 :(得分:0)
:
Uncaught ReferenceError: $ is not defined
并在你的听众中:
private Context context;
public MessageArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
this.context = context;
}
答案 3 :(得分:0)
我认为你有一些问题。但首先,您的适配器中只有一个图像按钮实例。因此,我认为只有列表中的最后一项才会绑定图像按钮。 您应该为列表中的每个项目创建一个视图持有者,因为您需要将事件绑定到列表中的每个项目,以便将项目的索引与该视图所有者一起存储。所以创建一个内部类(我将删除你的一些细节):
public static class ViewHolder{
public TextView playerName;
public ImageButton profileButton;
}
在你的adpater中,getView属性处理viewHolder:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder row;
if (convert View == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.activity_chat_singlemessage, parent, false);
row.playerName = (TextView) row.findViewById(R.id.playername_);
row.profileButton = (ImageButton) row.findViewById(R.id.ib_message);
convertView.setTag(row);
} else row = (ViewHolder) convertView.GetTag();
row.setText(chatMessageObj.name);
row.profileButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("log_tag",name+ID);
Intent intent = new Intent(v.getContext(), NextActivity.class);
v.getContext().startActivity(intent);
}
});
希望能让你前进。 (从asadmshah复制了意图代码)