Reputation: 45
I am new to android, I've made a listview to view songs from memory the listview is displaying all the songs and scrolling fine but the onclickitemlistener is not working, I saw all the answers related to this topic on the site but I couldn't solve it. Any help will be appreciated
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);
}
}}
activity layout 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" />
custom layout for listview items
<?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>
Upvotes: 0
Views: 131
Reputation: 517
try this.. its working fine to me:
1.In your CustomAdapter Class inside getView() method ,put this following code.
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ctx,"Position :"+position+" clicked",Toast.LENGTH_LONG).show();
}
});
I hope this too help you.Let me know if it is working or not
Upvotes: 1
Reputation: 1648
Use this:
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();
}
});
Instead of
listView.setOnItemClickListener(this);
Upvotes: 0
Reputation:
make an interface to handle click event into custom adapter like below ..
onItemClickListner onItemClickListner;
public void setOnItemClickListner(CommentsAdapter.onItemClickListner onItemClickListner) {
this.onItemClickListner = onItemClickListner;
}
public interface onItemClickListner {
void onClick(String str);//pass your object types.
}
and In adapter in GetView() method add this below line to click event ..
// outerContiner is view object
outerContiner.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemClickListner.onClick(data.get(position).itemName);
}
});
then after in activity after adapter bind into list view call below code ..
adapter.setOnItemClickListner(new CommentsAdapter.onItemClickListner() {
@Override
public void onClick(String str) {
Log.d("Value of Click",str);
}
});
Upvotes: 0