listViewItem.setOnItemClickListener没有wroking

时间:2016-04-12 04:56:19

标签: android listview

我使用setOnitemclicklistener使用适配器进行自定义ListView,但它不起作用,请帮助我。这是HomeActivity:

 public class Home extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
TabHost tabHosts;
public ListView listView;
private Context ctx;
private ListView listViewItem;
TextView ime;
ImageView imgAppLable;
public String text = "";
TelephonyManager manager;
int ArrNameLable[] = {};
Button btnClick;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);
  // setActionBar(toolbar);


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    //get imei
    manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    ime = (TextView) findViewById(R.id.txtViewImei);
    text = manager.getDeviceId().toString();
    ime.setText("Your imei:  " + text);
    //Tab host
    TabHost host = (TabHost) findViewById(R.id.tabHost);
    host.setup();
    Resources res = getResources();
    //Tab 1
    TabHost.TabSpec spec = host.newTabSpec("Offers");
    spec.setContent(R.id.tab1);
    spec.setIndicator("Offers");
    host.addTab(spec);

    //Tab 2
    spec = host.newTabSpec("Share");
    spec.setContent(R.id.tab2);
    spec.setIndicator("Share");
    host.addTab(spec);

    //Tab 3
    spec = host.newTabSpec("My Earnings");
    spec.setContent(R.id.tab3);
    spec.setIndicator("My Earnings");
    host.addTab(spec);

    // ListVIew Item

    ctx = this;
    List<MoveData> legendList = new ArrayList<MoveData>();
    legendList.add(new MoveData("AppName", "100", "icon_dollar"));
    legendList.add(new MoveData("AppName", "150", "icon_home"));
    legendList.add(new MoveData("AppName", "200", "icon_dollar"));
    legendList.add(new MoveData("AppName", "250", "icon_home"));
    legendList.add(new MoveData("AppName", "300", "icon_dollar"));
    legendList.add(new MoveData("AppName", "350", "icon_home"));

    listViewItem = (ListView) findViewById(R.id.lvItem);
    listViewItem.setAdapter(new AdapterCustomListview(ctx, R.layout.custom_layout_single, legendList));

    // Click event for single list row

    listViewItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(), position + "is selected", Toast.LENGTH_SHORT).show();


           // String o = String.valueOf(parent.getItemAtPosition(position));
           // Toast.makeText(Home.this, o, Toast.LENGTH_SHORT).show();

        }
    });
}

这是MoveData:

 public class MoveData {


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getCoin() {
    return coin;
}

public void setCoin(String coin) {
    this.coin = coin;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

private String name;
private String coin;
private String image;

public MoveData(String AppName, String Coin, String image) {
    super();
    this.name = AppName;
    this.coin = Coin;
    this.image = image;

}
}

这是CustomLisviewDataAdapter:

public class AdapterCustomListview extends ArrayAdapter<MoveData> {
private int resource;
private LayoutInflater inflater;
private Context context;

public AdapterCustomListview(Context ctx, int resourceId, List<MoveData> objects) {
    super(ctx, resourceId, objects);
    resource = resourceId;
    inflater = LayoutInflater.from(ctx);
    context = ctx;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    convertView = (LinearLayout) inflater.inflate(resource, null);
    MoveData Legend = getItem(position);
    TextView legendName = (TextView) convertView.findViewById(R.id.txtAppName);
    legendName.setText(Legend.getName());

    TextView legendBorn = (TextView) convertView.findViewById(R.id.txtCoin);
    legendBorn.setText(Legend.getCoin());

    ImageView legendImage = (ImageView) convertView.findViewById(R.id.imgApps);
    String uri = "drawable/" + Legend.getImage();
    int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
    //noinspection deprecation

    Drawable image = ContextCompat.getDrawable(context, imageResource);
    legendImage.setImageDrawable(image);

    return convertView;
}
}

2 个答案:

答案 0 :(得分:0)

您还可以通过修改适配器内的getView()方法获取点击事件,如下所示:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    convertView = (LinearLayout) inflater.inflate(resource, null);
    MoveData Legend = getItem(position);
    TextView legendName = (TextView) convertView.findViewById(R.id.txtAppName);
    legendName.setText(Legend.getName());

    TextView legendBorn = (TextView) convertView.findViewById(R.id.txtCoin);
    legendBorn.setText(Legend.getCoin());

    ImageView legendImage = (ImageView) convertView.findViewById(R.id.imgApps);
    String uri = "drawable/" + Legend.getImage();
    int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
    //noinspection deprecation

    Drawable image = ContextCompat.getDrawable(context, imageResource);
    legendImage.setImageDrawable(image);

convertView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(getApplicationContext(), position + "is selected", Toast.LENGTH_SHORT).show();
    }
});


    return convertView;

}

答案 1 :(得分:0)

我认为你的onItemClickListener没有任何问题它应该可以正常工作但是你应该像下面的行一样修改你的Toast,因为你想要在toast中显示位置但是位置是int,所以要么首先要改变将位置转换为字符串或者您可以编写任何字符串仅用于测试目的: -

Toast.makeText(Home.this, "o", Toast.LENGTH_SHORT).show();