Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Welcome extends ActionBarActivity {

    EditText inputSearch;
    Button signout;
    Button btn, btnhid;
    ListView lstcountry;
    SimpleAdapter ADAhere;
    /***********
     * CONNECTION DATABASE VARIABLES
     **************/
    ConnectionClass connectionclass;
    String usernameS;
    String datets;
    String call, db, un, passwords;
    Connection connect;
    ResultSet rs;


    @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;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        initcontrol();
    }

    private void initcontrol() {
        signout = (Button) findViewById(R.id.btnlogout);
        lstcountry = (ListView) findViewById(R.id.lstcountry);
        btn = (Button) findViewById(R.id.btnview);
        btnhid = (Button) findViewById(R.id.btnhide);
        inputSearch = (EditText) findViewById(R.id.inputSearch);
        /************* CONNECTION DATABASE VARIABLES ***************/
        connectionclass = new ConnectionClass();
        call = connectionclass.getip();
        un = connectionclass.getun();
        passwords = connectionclass.getpassword();
        db = connectionclass.getdb();
        connect = CONN(un, passwords, db, call);

        signout.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                SignOut(v);
            }
        });
        btnhid.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                inputSearch.setVisibility(View.INVISIBLE);
                lstcountry.setVisibility(View.INVISIBLE);
            }
        });
        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
                Welcome.this.ADAhere.getFilter().filter(cs);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
            }
        });
        btn.setOnClickListener(new View.OnClickListener() {
                                   @Override
                                   public void onClick(View v) {
                                       inputSearch.setVisibility(View.VISIBLE);
                                       lstcountry.setVisibility(View.VISIBLE);
                                       // TODO Auto-generated method stub
                                       String querycmd = "select * from UserDetails";
                                       try {
                                           Statement statement = connect.createStatement();
                                           rs = statement.executeQuery(querycmd);

                                           List<Map<String, String>> data = null;
                                           data = new ArrayList<Map<String, String>>();
                                           while (rs.next()) {
                                               Map<String, String> datanum = new HashMap<String, String>();

                                               datanum.put("A", rs.getString("Photo"));
                                               datanum.put("B", rs.getString("UserName"));
                                               data.add(datanum);

                                           }
                                           String[] fromwhere = {"A", "B"};
                                           int[] viewswhere = {R.id.ivuserimg, R.id.lblcountryname};
                                           ADAhere = new SimpleAdapter(Welcome.this, data,
                                                   R.layout.listtemplate, fromwhere, viewswhere);
                                           lstcountry.setAdapter(ADAhere);
                                       } catch (
                                               SQLException e
                                               )

                                       {
                                           Toast.makeText(Welcome.this, e.getMessage().toString(),
                                                   Toast.LENGTH_LONG).show();
                                       }
                                   }
                               }

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

            }
        });

    }

    private void SignOut(View v) {
        Intent i_register = new Intent(Welcome.this, Login.class);
        startActivity(i_register);
    }

    @Override
    public void onBackPressed() {
        Welcome.this.finish();
    }


}
Posted
Updated 22-Oct-15 20:57pm
v2

1 solution

Use these logic inside your ListView adapter, basic process is straightforward.
Android Storing Images in MS SQL Server[^]
Android Retrieving Images stored in Base64 value from MS SQL Server[^]
 
Share this answer
 
Comments
pritamb 24-Oct-15 5:14am    
I store image in varchar.
I get conversion problem and fetching image into listview.
I already use suggested url

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900