我是android新手,我已经制作了一个listview来查看内存中的歌曲listview显示所有歌曲并滚动正常但是onclickitemlistener无法正常工作,我在网站上看到了与此主题相关的所有答案但是我无法解决它。 任何帮助将不胜感激
public class MainActivity extends Activity implements AdapterView.OnItemClickListener{
private boolean scannedSongs;
public ArrayList<Song> musiclist=null;
private boolean scanningSongs;
ListView listView;
MainActivity.customeadapter cus = new MainActivity.customeadapter();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
listView.setOnItemClickListener(this);
// imp permission request
while (ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
MMB.initialize(getBaseContext());
if(!MMB.songs.isInitialized())
{
scanSongs(false);
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Click ListItem Number " + position, Toast.LENGTH_LONG).show();
}
class customeadapter extends BaseAdapter {
@Override
public int getCount() {
return musiclist.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@SuppressLint("ViewHolder")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
convertView=inflater.inflate(R.layout.customlayout,null);
ImageView Custom_imageView=(ImageView)convertView.findViewById(R.id.Custom_imageView);
TextView custom_songname=(TextView)convertView.findViewById(R.id.custom_songname);
TextView custom_artistname=(TextView)convertView.findViewById(R.id.custom_artistname);
Uri thumbnailuri=Uri.parse(musiclist.get(position).getFilePath());
Custom_imageView.setImageURI(thumbnailuri);
custom_songname.setText(((Song)musiclist.get(position)).getTitle());
custom_artistname.setText(((Song)musiclist.get(position)).getArtist());
return convertView;
}
}
void scanSongs(boolean forceScan) {
// Loading all the songs from the device on a different thread.
// We'll only actually do it if they weren't loaded already
//
// See the implementation right at the end of this class.
if ((forceScan) || (! MMB.songs.isInitialized())) {
SingleToast.show(MainActivity.this, getString(R.string.menu_main_scanning), Toast.LENGTH_LONG);
new ScanSongs().execute();
}
}
class ScanSongs extends AsyncTask<String, Integer, String> {
/**
* The action we'll do in the background.
*/
@Override
protected String doInBackground(String... params) {
try {
// Will scan all songs on the device
MMB.songs.scanSongs(MainActivity.this, "external");
return MainActivity.this.getString(R.string.menu_main_scanning_ok);
}
catch (Exception e) {
Log.e("exception", e.toString());
e.printStackTrace();
return MainActivity.this.getString(R.string.menu_main_scanning_not_ok);
}
}
/**
* Called once the background processing is done.
*/
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
musiclist=MMB.songs.getSongs();
SingleToast.show(MainActivity.this, result, Toast.LENGTH_LONG);
listView.setAdapter(cus);
}
}}
活动布局XML
tools:context="com.example.sreyash.musicplayer.MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="380dp"
android:layout_height="468dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:clickable="false"
android:focusable="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/textView2"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:fontFamily="@font/seguisym"
android:text="Songs"
android:textAlignment="center"
android:textColor="@color/segeo"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.016" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="20dp"
android:layout_height="22dp"
android:background="@drawable/swipe"
android:focusable="false"
android:focusableInTouchMode="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.952"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.04000002" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="30dp"
android:layout_height="26dp"
android:background="@drawable/sidebar"
android:focusable="false"
android:focusableInTouchMode="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.048"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.04000002" />
列表视图项目的自定义布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/linearLayout">
android:focusable="false"
android:focusableInTouchMode="false"
<ImageView
android:id="@+id/Custom_imageView"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_alignParentStart="true"
android:layout_alignTop="@+id/Custom_songname"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:focusable="false"
android:focusableInTouchMode="false"
app:srcCompat="@drawable/album_art" />
</LinearLayout>
<TextView
android:id="@+id/custom_songname"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="22dp"
android:layout_marginTop="12dp"
android:layout_toEndOf="@+id/linearLayout"
android:fontFamily="@font/comicsansms"
android:text="TextView"
android:textColor="@color/segeo" />
<TextView
android:id="@+id/custom_artistname"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/custom_songname"
android:layout_below="@+id/custom_songname"
android:fontFamily="@font/seguisym"
android:text="TextView"
android:textColor="@color/segeo"
android:textSize="10sp"
android:typeface="normal" />
<ImageButton
android:id="@+id/custome_sharebutton"
android:layout_width="3dp"
android:layout_height="14dp"
android:layout_alignParentEnd="true"
android:layout_alignTop="@+id/custom_songname"
android:layout_marginEnd="39dp"
android:layout_marginTop="16dp"
android:background="@drawable/share"
android:focusable="false"
android:focusableInTouchMode="false"
/>
</RelativeLayout>
答案 0 :(得分:1)
试试这个..它对我来说很好用:
1.在getView()方法内的CustomAdapter类中,输入以下代码。
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ctx,"Position :"+position+" clicked",Toast.LENGTH_LONG).show();
}
});
我希望这也能帮助你。让我知道它是否有效
答案 1 :(得分:0)
创建一个界面来处理点击事件到自定义适配器,如下所示..
onItemClickListner onItemClickListner;
public void setOnItemClickListner(CommentsAdapter.onItemClickListner onItemClickListner) {
this.onItemClickListner = onItemClickListner;
}
public interface onItemClickListner {
void onClick(String str);//pass your object types.
}
并在GetView()方法的适配器中添加以下行以单击事件..
// outerContiner is view object
outerContiner.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemClickListner.onClick(data.get(position).itemName);
}
});
然后在活动之后将适配器绑定到列表视图调用下面的代码..
adapter.setOnItemClickListner(new CommentsAdapter.onItemClickListner() {
@Override
public void onClick(String str) {
Log.d("Value of Click",str);
}
});
答案 2 :(得分:0)
使用此:
llistView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position,long arg3) {
Toast.makeText(MainActivity.this, "Click ListItem Number " + position, Toast.LENGTH_LONG).show();
}
});
而不是
listView.setOnItemClickListener(this);