我有一个从我的适配器充气的列表视图。 当我点击其中一个按钮时,我得到了错误的项目。 它用于工作o.k,直到我加入了whatsApp
按钮。列表视图点击获取错误的项目
这是我的适配器:
public class shawarmaddapter extends ArrayAdapter<shawarma> {
String nname;
double lat,lon;
public shawarmaddapter(Context context, List<shawarma> resource) {
super(context,0, resource);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
....
if (convertView==null)
{
convertView= LayoutInflater.from(getContext()).inflate(R.layout.shawarmaddapter,parent,false);
}
....
waze= (ImageButton)convertView.findViewById(R.id.adapterWazeBTN);
whats = (ImageButton)convertView.findViewById(R.id.whats);
locName=name.getText().toString();
....
nname = sh1.getName();
name.setText(sh1.getName());
....
if (sh1.getParking()==1)
park.setChecked(true);
else
park.setChecked(false);
rate.setRating((float) sh1.getRank());
rate.setIsIndicator(true);
listener lis = new listener();
waze.setOnClickListener(lis);
whats.setOnClickListener(lis);
lat= sh1.getLat();
lon= sh1.getLon();
return convertView;
}
有一个onClickListener在代码中添加多数民众赞成应该如果按下WhatsApp的按钮,检查Waze的按钮被按下的位置或名称。 这是代码
class listener implements View.OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.adapterWazeBTN:
Log.i("a7", "button clicked");
// Log.i("a7", lat+" lat, "+lon+" lon"+ "distance: "+ sh1.getDistance());
try
{
String url = "waze://?ll="+lat+","+lon+"&navigate=yes";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
shawarmaList.con. startActivity(intent);
}
catch (ActivityNotFoundException ex )
{
Intent intent =
new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.waze"));
shawarmaList.con.startActivity(intent);
}
catch (Exception ex)
{
Log.i("a7",ex.toString());
}
break;
case R.id.whats:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "do you want to Join me to "+nname+"?");
sendIntent.setType("text/plain");
try {
Toast.makeText(shawarmaList.con.getApplicationContext(),nname,Toast.LENGTH_SHORT).show();
shawarmaList.con.startActivity(sendIntent);
} catch (Exception e) {
Log.i("a7",e.getMessage());
}
break;
}
}
}
我怎样才能改变这种以得到正确的项目?
您还没有发送您的项目的索引到您的收听。听众如何知道您点击了哪个索引?此外,您还需要在适配器上使用ViewHolder Pattern以获得性能和其他错误的项目问题。 –
我该如何发送索引? 在我添加whatsapp按钮之前它是如何工作的? 你能帮助观众吗? –
你需要学习如何研究。检查这些链接:https://developer.android.com/training/improving-layouts/smooth-scrolling.html和http://stackoverflow.com/questions/20541821/get-listview-item-position-on-button-点击 –