2016-11-13 101 views
-1

我有一个从我的适配器充气的列表视图。 当我点击其中一个按钮时,我得到了错误的项目。 它用于工作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; 


     } 
    } 
} 

我怎样才能改变这种以得到正确的项目?

+0

您还没有发送您的项目的索引到您的收听。听众如何知道您点击了哪个索引?此外,您还需要在适配器上使用ViewHolder Pattern以获得性能和其他错误的项目问题。 –

+0

我该如何发送索引? 在我添加whatsapp按钮之前它是如何工作的? 你能帮助观众吗? –

+0

你需要学习如何研究。检查这些链接:https://developer.android.com/training/improving-layouts/smooth-scrolling.html和http://stackoverflow.com/questions/20541821/get-listview-item-position-on-button-点击 –

回答

0

你可以试试这个方法

package com.example.listview; 

import java.util.ArrayList; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.ClipData.Item; 
import android.view.Menu; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 
    ListView list; 
    ArrayList<String> data; 
    ArrayAdapter<String> adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     list=(ListView)findViewById(R.id.listView1); 
     data=new ArrayList<String>(); 
     data.add("List item 1"); 
     data.add("List item 2"); 
     data.add("List item 3"); 
     data.add("List item 4"); 
     adapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,data); 
     list.setAdapter(adapter); 

     list.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 

      String s= Integer.toString(arg2) ; //here arg2 selects the list item 
      Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show(); 
      // TODO Auto-generated method stub 

     } 
    }); 
    } 




    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 
0

试试这个:

class listener implements View.OnClickListener{ 

    shawarma sh; 

    listener(shawarma sh1) { 
     sh = sh1; 
    } 

    @Override 
    public void onClick(View v) { 
     switch (v.getId()) 
     { 
      case R.id.adapterWazeBTN: 
       Log.i("a7", "button clicked"); 

       // Log.i("a7", sh.getLat()+" lat, "+sh.getLon()+" lon"+ "distance: "+ sh.getDistance()); 

       try 
       { 
        String url = "waze://?ll="+sh.getLat()+","+sh.getLon()+"&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 "+sh.getName()+"?"); 
       sendIntent.setType("text/plain"); 

       try { 
        Toast.makeText(shawarmaList.con.getApplicationContext(),sh.getName(),Toast.LENGTH_SHORT).show(); 
        shawarmaList.con.startActivity(sendIntent); 
       } catch (Exception e) { 
        Log.i("a7",e.getMessage()); 
       } 

       break; 


     } 
    } 
} 

然后,当您创建的侦听器只是通过俐铁板沙威玛对象是这样的:

listener lis = new listener(sh1); 
+0

我试过了,但它仍然挑选错误的项目 –

0

相反的,

waze.setOnClickListener(lis); 
whats.setOnClickListener(lis); 

试试这个,

waze.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     try { 
      String url = "waze://?ll="+sh1.getLat()+","+sh1.getLon()+"&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()); 
     } 
    } 
}); 

whats.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 

     try { 
      Intent sendIntent = new Intent(); 
      sendIntent.setAction(Intent.ACTION_SEND); 
      sendIntent.putExtra(Intent.EXTRA_TEXT, "do you want to Join me to "+sh1.getName()+"?"); 
      sendIntent.setType("text/plain"); 

      Toast.makeText(shawarmaList.con.getApplicationContext(),sh1.getName(),Toast.LENGTH_SHORT).show(); 
      shawarmaList.con.startActivity(sendIntent); 
     } catch (Exception e) { 
      Log.i("a7",e.getMessage()); 
     } 
    } 
}); 

你会希望sh1作为最终,

final shawarma sh1 = getItem(position); 
相关问题