我有一个活动,其中有两个列表视图。
一个是带有imageview和textview insdie的自定义列表视图,另一个是找到滚动解决方案后的按钮。
基本上左边是食物及其图片的清单,右边是购买它的清单 现在我在同时滚动它们时遇到了问题。
public class list_activity extends AppCompatActivity {
String[] itemname ={
"Safari",
"Camera",
"Global",
"FireFox",
"UC Browser",
"Android Folder",
"VLC Player",
"Cold War",
"Whatsapp",
"Google",
"Java"
};
String[] buy={"one","two","two","two","two","two","two","two","two","two","two","two","two","two","two","two","two"};
Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_list);
ListAdapter adapter = new com.example.azaabudeen.restoapp.CustomListView(this,itemname);
ListView listView= (ListView)findViewById(R.id.listview);
ListView secondlistview= (ListView)findViewById(R.id.foodlistBuyButton);
ListAdapter secondA= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,buy);
secondlistview.setAdapter(secondA);
listView.setAdapter(adapter);
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.view);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String currentItem= String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(list_activity.this,currentItem,Toast.LENGTH_SHORT).show();
/// final TextView text = (TextView)findViewById(R.id.text);
/// final Button myButton=(Button)findViewById(R.id.dialogButtonOK);
/// text.setText("Image of the list");
/// myButton.setOnClickListener(new View.OnClickListener() {
/// @Override
/// public void onClick(View v) {
//// dialog.dismiss();
// }
/// });
dialog.show();
}
});
}
}
更新
我是通过将onClickListners单独添加到Customadapter来实现的
这是代码
class CustomListView extends ArrayAdapter {
public CustomListView(Context context, String[] resource) {
super(context,R.layout.custom_view , resource);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater MyInflater = LayoutInflater.from(getContext());
View CustomView = MyInflater.inflate(R.layout.custom_view, parent, false);
String SingleItem= (String) getItem(position);
TextView text =(TextView)CustomView.findViewById(R.id.Itemname);
ImageView Image= (ImageView)CustomView.findViewById(R.id.icon);
text.setText(SingleItem);
Image.setImageResource(R.drawable.myimage);
final Button Buybutton= (Button)CustomView.findViewById(R.id.BuyButton);
Buybutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "Bought", Toast.LENGTH_SHORT).show();
}
});
Image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(),"Image",Toast.LENGTH_SHORT).show();
}
});
return CustomView;
}
}
答案 0 :(得分:1)
您应该只使用一个ListView
。
您可以在自定义适配器中设置onClickListener
。
这里有一个例子:ListView with clickListener
为您的imageView和按钮设置onClickListener
。