0

firstly, i read all answers related with this problem but i didn't solve this error. My program is running but very laggy and slow. i tryed use smaller images and use async task but my problem is continued. here is my code

public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
NotificationManager manager;
public Veri veri,veri1;
int x= 0;

private Timer autoUpdate;

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


    veri1 = new Veri();
    x=veri1.getList().size();

    recyclerView= (RecyclerView) findViewById(R.id.my_recycler_view);




    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);



    manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    onResume();

    new AsyncCaller().execute();



}

@Override
protected void onStart() {
    super.onStart();
}
////////////////////////////////////////////
private class AsyncCaller extends AsyncTask<Void, Void, Void>
{


    @Override
    protected void onPreExecute() {
        super.onPreExecute();


    }
    @Override
    protected Void doInBackground(Void... params) {



            autoUpdate = new Timer();
            autoUpdate.schedule(new TimerTask() {
                @Override
                public void run() {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            updateHTML();
                        }
                    });
                }
            }, 0, 60000);


        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);


    }

}


////////////////////////////////////

private void updateHTML(){
    // your logic here
    RecyclerAdapter adapter=new RecyclerAdapter(this);
    recyclerView.setAdapter(adapter);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    veri = new Veri();
    int a = veri.getList().size();
    if(a!=x){
        generateNotification(MainActivity.this,"Listede Degisim Var");
    }
    x=a;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Take appropriate action for each action item click
    switch (item.getItemId()) {

            // search action
        case R.id.action_location_found:
            // location found
            LocationFound();
            return true;



        default:
            return super.onOptionsItemSelected(item);
    }
}

private void LocationFound() {
    Intent i = new Intent(MainActivity.this, LocationFound.class);
    startActivity(i);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_main_actions, menu);

    return super.onCreateOptionsMenu(menu);
}
private void generateNotification(Context context,String message){
    int notificationId = 001;
    Intent viewIntent = new Intent(context, MainActivity.class);

    PendingIntent viewPendingIntent =
            PendingIntent.getActivity(context, 0, viewIntent, 0);

    NotificationCompat.Builder notificationBuilder =
            (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.dikat)
                    .setContentTitle("Bildirim")
                    .setContentText(message)
                    .setContentIntent(viewPendingIntent)
                    .setAutoCancel(true)
                    .setSound(Uri.parse("android.resource://"
                            + context.getPackageName() + "/" + R.raw.bildirim))

                    .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });

    NotificationManagerCompat notificationManager =
            NotificationManagerCompat.from(context);

    notificationManager.notify(notificationId, notificationBuilder.build());
}

}

here is my data class

public class Veri extends ActionBarActivity {

NotificationManager manager;
Notification myNotication;
String ip, db, un, passwords;
Connection connect,conn;
PreparedStatement stmt;
ResultSet rs;
TextView tv;
int position1=0;
Button b1;

ArrayList<String> data = new ArrayList<String>();
ArrayList<String> data2 = new ArrayList<String>();



public Veri(){
    ip = "x.x.x.x";
    un = "x";
    passwords = "x";
    db = "xxx";

    connect = CONN(un, passwords, db, ip);
    String query = "SELECT Adsoyad,(DATEPART(hour, BeklemeSuresi) * 3600) + (DATEPART(minute, BeklemeSuresi) * 60) + DATEPART(second, BeklemeSuresi) as SecondsFromMidnight,Durum FROM [dbo].[IslemiDevamEdenHasta] (1)";



    try {
       // connect = CONN(un, passwords, db, ip);
        stmt = connect.prepareStatement(query);
        rs = stmt.executeQuery();

        while (rs.next()) {
            int b = (int)rs.getInt("SecondsFromMidnight")/60;
            if(b>50) {
                String id = rs.getString("Adsoyad");
                String id2 = rs.getString("Durum");
                String full = id + " " + id2;
                String a = "" + b;
                data.add(full);
                data2.add(a);
            }
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

}

public ArrayList<String> getList() {
    return data;
}
public ArrayList<String> getList2() {
    return data2;
}

@SuppressLint("NewApi")
private Connection CONN(String _user, String _pass, String _DB,
                        String _server) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Connection conn = null;
    String ConnURL = null;
    try {

        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        ConnURL = "jdbc:jtds:sqlserver://" + _server + ";"
                + "databaseName=" + _DB + ";user=" + _user + ";password="
                + _pass + ";";
        conn = DriverManager.getConnection(ConnURL);
    } catch (SQLException se) {
        Log.e("ERRO", se.getMessage());
    } catch (ClassNotFoundException e) {
        Log.e("ERRO", e.getMessage());
    } catch (Exception e) {
        Log.e("ERRO", e.getMessage());
    }
    return conn;
}

}

and here is my recycler class

public class RecyclerAdapter extends  RecyclerView.Adapter<RecyclerViewHolder> {

public Veri veri = new Veri();
int sayi=0;

ArrayList<String> liste = veri.getList();
ArrayList<String> liste2 = veri.getList2();
static ArrayList<String> liste3 = new ArrayList<String>() ;

Context context;
LayoutInflater inflater;
public RecyclerAdapter(Context context) {
    this.context=context;
    inflater=LayoutInflater.from(context);

}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v=inflater.inflate(R.layout.item_list, parent, false);



    RecyclerViewHolder viewHolder=new RecyclerViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {

    liste3.add("a");
    sayi = 0;
    for (int j = 0; j < liste3.size(); j++) {
        if (liste3.get(j).equals(liste.get(position))) {
            sayi++;
        }
        if (sayi == 0) {
            holder.tv1.setText(liste.get(position));
            holder.tv2.setText(liste2.get(position));
            holder.imageView.setOnClickListener(clickListener);
            holder.imageView.setTag(holder);
        } else {
            holder.tv1.setText(liste.get(position));
            holder.tv2.setText(liste2.get(position));
            holder.imageView.setOnClickListener(clickListener);
            holder.imageView.setImageResource(R.drawable.ic_checkin);
        }
    }
}


View.OnClickListener clickListener=new View.OnClickListener() {
    @Override
    public void onClick(View v) {


        RecyclerViewHolder vholder = (RecyclerViewHolder) v.getTag();
        int position = vholder.getPosition();
        //Toast.makeText(context,"This is position "+position,Toast.LENGTH_LONG ).show();
        vholder.imageView.setImageResource(R.drawable.ic_checkin);
        liste3.add(liste.get(position));
    }
};





@Override
public int getItemCount() {
    return liste.size();
}

}

what is my problem??

Engin
  • 41
  • 5

1 Answers1

0

you doing so much coding for loading image.There are some third party api so you can refer it. so it may be helpful to load your image easily and memory management control is handle by that api also here just refer this tutorial.

Milap Pancholi
  • 134
  • 1
  • 12