无法单击ArrayList项目



[我创建了一个使用ArrayList存储数据的项目,当我点击列表视图上的每个项目时,它会启动一个新的活动,然后我将图像按钮添加到ListView现在应用程序正在正确地吃午饭,但当我点击项目时,什么也没发生。][1]

主要java代码:

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Create an ArrayList of MusicApp objects
ArrayList<Music> music = new ArrayList<Music>();
music.add(new Music(R.drawable.cover1, "Title1", R.drawable.cover1));
music.add(new Music(R.drawable.cover2, "Title2", R.drawable.cover1));
music.add(new Music(R.drawable.cover3, "Title3", R.drawable.cover1));
music.add(new Music(R.drawable.cover4, "Title4", R.drawable.cover1));
music.add(new Music(R.drawable.cover5, "Title5", R.drawable.cover1));
music.add(new Music(R.drawable.cover6, "Title6", R.drawable.cover1));

AndroidMusicAdapter musicAdapter = new AndroidMusicAdapter(this, music);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(musicAdapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startActivity(position);
}
});
}

public void startActivity(int position) {

if (position == 0) {
startActivity(new Intent(MainActivity.this, Song1Activity.class));
}
if (position == 1) {
startActivity(new Intent(MainActivity.this, Song2Activity.class));
}
if (position == 2) {
startActivity(new Intent(MainActivity.this, Song3Activity.class));
}
if (position == 3) {
startActivity(new Intent(MainActivity.this, Song4Activity.class));
}
if (position == 4) {
startActivity(new Intent(MainActivity.this, Song5Activity.class));
}
if (position == 5) {
startActivity(new Intent(MainActivity.this, Song6Activity.class));
}
}
}

适配器代码

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class AndroidMusicAdapter extends ArrayAdapter<Music> {
public AndroidMusicAdapter(Activity context, ArrayList<Music> music) {

super(context, 0, music);
}

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

// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}

// Get the {@link Music} object located at this position in the list
Music currentMusic = getItem(position);

// Find the ImageView in the list_item.xml layout with the ID list_item_icon
ImageView iconView = (ImageView) listItemView.findViewById(R.id.list_item_icon);
// set the image to iconView
iconView.setImageResource(currentMusic.getImageResourceId());

// Find the TextView in the list_item.xml layout with the ID song_name
TextView nameTextView = (TextView) listItemView.findViewById(R.id.song_name);// Get the version name from the current AndroidFlavor object and
// set this text on the name TextView
nameTextView.setText(currentMusic.getSongName());

ImageButton playButton = (ImageButton) listItemView.findViewById(R.id.playButton);
playButton.setImageResource(currentMusic.getPlay());

//Return the whole list item layout (containing 1 textView &nd 1 imageView)
return listItemView;

}
}

方法代码

public class Music {

// Drawable resource ID
private int mImageResourceId;

// Name of the song
private String mSongName;

// play button
private int mPlay;

public Music(int imageResourceId, String songName, int play) {
mImageResourceId = imageResourceId;
mSongName = songName;
mPlay = play;
}

public int getImageResourceId() {
return mImageResourceId;
}

public String getSongName() {
return mSongName;
}

public int getPlay() {
return mPlay;
}
}

布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:padding="16dp">

<ImageButton
android:id="@+id/playButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="TODO" />

<ImageView
android:id="@+id/list_item_icon"
android:layout_width="50dp"
android:layout_height="50dp" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:paddingLeft="16dp">
<TextView
android:id="@+id/song_name"
style="@style/titleStyle"/>


</LinearLayout>

列表查看代码

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
tools:context=".MainActivity">

</ListView>

您的代码需要改进,但为了解决您的问题,您可以添加到适配器代码listItemView.setOnClickListner(and implement method click listener)中,并将函数startActivity移动到适配器代码中,最后将startActivity放入listItemView.setOnClickListner中,并从主java代码中删除listView.setOnItemClickListener

注意:移动到适配器后,用new Intent(getcontext(), Song1Activity.class)编辑startActivitynew Intent(MainActivity.this, Song1Activity.class)

public void startActivity(int position) {

if (position == 0) {
startActivity(new Intent(getcontext(), Song1Activity.class));
}
if (position == 1) {
startActivity(new Intent(getcontext(), Song2Activity.class));
}
if (position == 2) {
startActivity(new Intent(getcontext(), Song3Activity.class));
}
if (position == 3) {
startActivity(new Intent(getcontext(), Song4Activity.class));
}
if (position == 4) {
startActivity(new Intent(getcontext(), Song5Activity.class));
}
if (position == 5) {
startActivity(new Intent(getcontext(), Song6Activity.class));
}
}
}

相关内容

  • 没有找到相关文章

最新更新