【发布时间】:2015-02-21 03:07:31
【问题描述】:
我有一个列表视图,它在通过 Php/JSON 显示数据库中的所有文本时效果很好。但是,在数据库中有一个与我想在列表视图中显示的每个项目相关联的图像链接。基本上它的作用是一个人单击搜索按钮,然后在他们输入单词的位置出现操作菜单中的默认 android 搜索栏。然后它去服务器并发送单词并尝试从数据库中找到所有匹配项并将其拉出。现在它可以完美地显示文本视图,但由于某种原因我无法显示图像。有人可以告诉我怎么做吗?我尝试了几次但失败了,因此我提供给您的代码是我的最新代码:
public class ListResult extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> UserBooksList;
// url to get the idiom list
private static String url_search =
"http://randomurl.com/arandomapp/search.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_UserBooks = "UserBooks";
private static final String TAG_BName = "BName";
private static final String TAG_NAME = "name";
// THIS TAG BELOW IS THE ONE WHICH SHOWS THE IMAGE URL.
private static final String TAG_IMG = "Id";
// products JSONArray
JSONArray user_books = null;
// The keyword send to server and displays the result.
public String search_key;
TextView mError;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_result);
// get the action bar
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
search_key = null;
} else {
search_key = extras.getString("keyword");
}
} else {
search_key = (String) savedInstanceState.getSerializable("keyword");
}
UserBooksList = new ArrayList<>();
// Loading idioms in Background Thread
new LoadIdioms().execute();
mError = (TextView) findViewById(R.id.error);
mError.setVisibility(View.GONE);
actionBar.setTitle("Relevant Searches For: " + search_key);
actionBar.setIcon(R.drawable.ic_action_search);
}
class LoadIdioms extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ListResult.this);
pDialog.setMessage("Loading Data. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<>();
//value captured from previous intent
params.add(new BasicNameValuePair("keyword", search_key));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_search, "GET",
params);
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
user_books = json.getJSONArray(TAG_UserBooks);
for (int i = 0; i < user_books.length(); i++) {
JSONObject c = user_books.getJSONObject(i);
// Storing each json item in variable
String bName = c.getString(TAG_BName);
String Name = c.getString(TAG_NAME);
String b_img = c.getString(TAG_IMG);
// creating new HashMap
HashMap<String, String> map = new HashMap<>();
// adding each child node to HashMap key => value
map.put(TAG_BName, bName);
map.put(Tag_NAME, Name);
map.put(TAG_IMG, b_img);
// adding HashList to ArrayList
UserBooksList.add(map);
}
} else {
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting the related idioms
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
**/
ListAdapter adapter = new SimpleAdapter(
ListResult.this, UserBooksList,
R.layout.list_item, new String[] {TAG_BName, TAG_Name},
new int[] {R.id.BName, R.id.Name});
// updating listview
setListAdapter(adapter);
}
});
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null){
mError = (TextView) findViewById(R.id.error);
mError.setVisibility(View.VISIBLE);
mError.setText("Sorry, No Books were found. Please try again.");
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions_two, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_help:
// help action
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
任何人都可以向我建议我如何结合在加载时设置图像的想法吗?我的意思是当我尝试它时,图像的 url 被完美地获取。
我有一个list_item.xml,其中包含名称的文本视图和一个 ID:ImageView3 的 imageView。
谢谢:)
【问题讨论】:
标签: php android json listview android-listview