convertView = inflater.inflate (R.layout. listview_layout, null); ImageView imageView = convertView.findViewById(R.id.imageView); TextView serialTextView = convertView.findViewById(R.id.textView1); TextView nameTextView = convertView.findViewById(R.id.textView2); TextView scoreTextView convertView.findViewById(R.id.textViews); imageView.setImageResource (flags [position]); serialTextView.setText (serialNo [position]); nameTextView.setText (names [position]); scoreTextView.setText (scores [position]); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Bitmap bitmap = BitmapFactory.decodeResource (context.getResources (), flags [position]); try { // Get the directory for the app's private pictures directory. File file = new File(context.getExternalFiles Dir(s: null), names [position] + ".jpg"); FileOutputStream fos = new FileOutputStream(file); bitmap.compress (Bitmap.CompressFormat.JPEG, 90, fos); fos.close(); Toast.makeText(context, "Image saved successfully", Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(context, "Error saving image", Toast.LENGTH_SHORT).show();
customeradaper class is like this
package com.example.myapplication;
import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.zip.Inflater;
public class CustomAdapter extends BaseAdapter {
Context context;
ListView listView;
String[] serialNo;
int[] flags;
String[] names;
String[] scores;
public CustomAdapter(Context applicationContext, ListView simpleList, String[] serialNo, int[] flags, String[] names, String[] score) {
this.context = applicationContext;
this.listView = simpleList;
this.serialNo = serialNo;
this.flags = flags;
this.names = names;
this.scores = score;
}
@Override
public int getCount() {
return flags.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listview_layout, null);
ImageView imageView = convertView.findViewById(R.id.imageView);
TextView serialTextView = convertView.findViewById(R.id.textView1);
TextView nameTextView = convertView.findViewById(R.id.textView2);
TextView scoreTextView = convertView.findViewById(R.id.textView3);
imageView.setImageResource(flags[position]);
serialTextView.setText(serialNo[position]);
nameTextView.setText(names[position]);
scoreTextView.setText(scores[position]);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), flags[position]);
try {
// Get the directory for the app's private pictures directory.
File file = new File(context.getExternalFilesDir(null), names[position] + ".jpg");
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.close();
Toast.makeText(context, "Image saved successfully", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Error saving image", Toast.LENGTH_SHORT).show();
}
}
});
return convertView;
}
}
---------------------------------------
there are some errors in this code ,whats wrong with these red parts and how to fix them .
Step by step
Solved in 2 steps