此代码显示用户名但不显示图像。如何在SQL Server的列表视图中显示图像?
问题描述:
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();
}
}
答
在ListView适配器中使用这些逻辑,基本过程很简单。
Android在MS SQL中存储图像服务器 [ ^ ]
Android从MS SQL Server中检索存储在Base64值中的图像 [ ^ ]
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[^]