我想用rowwise显示文字图片。这是我的xml文件
和In RowItem类包含我设置位图对象
现在我使用的是硬编码的String数组,但我想使用URL的String数组进行设置 我使用httpconnection获取此url表单网络,我在andoridManifest.xml文件中添加了permisions
我很困惑我没有找到任何正确的解决方案,请帮助我
我想要显示图片的网址 http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-arrayadapter/ 我想做同样只使用网址而不是硬编码图像
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:contentDescription="@string/image"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<TextView
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_toRightOf="@+id/icon"
android:paddingLeft="10dp"
android:paddingTop="20dp"
android:textColor="#3399FF"
android:textSize="14dp" />
</RelativeLayout>
我的Java代码
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class BdaylistActivity extends Activity implements
OnItemClickListener {
private ProgressDialog m_ProgressDialog = null;
ListView listView;
List<RowItem> rowItems;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bdaylist);
m_ProgressDialog = ProgressDialog.show(this,
"Please wait...", "Retrieving data ...", true);
new BirthdayTask().execute("https://gdirectoryqa.appspot.com/gDirectory/get_user_profile.htm");
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(this, ProfileViewActivity.class);
intent.putExtra("BDay", true);
startActivity(intent);
}
class BirthdayTask extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (Exception e) {
e.printStackTrace();
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
JSONArray jArr;
ArrayList<String> firstLastNames=new ArrayList<String>();
ArrayList<Integer> images=new ArrayList<Integer>();
try {
jArr = new JSONArray(result);
for (int count = 0; count < jArr.length(); count++) {
JSONObject obj = jArr.getJSONObject(count);
JSONObject userGeneralInfoArray = jArr.getJSONObject(count).getJSONObject("userGeneralInfo");
firstLastNames.add(userGeneralInfoArray.getString("firstName")+" "+userGeneralInfoArray.getString("lastName"));
images.add(R.drawable.ic_launcher);
}
System.out.println("firstName="+firstLastNames);
rowItems = new ArrayList<RowItem>();
if(firstLastNames.size() == 0){
firstLastNames.add("No Results");
images.add(1);
}
String[] images=new String[]{"http://lh6.ggpht.com/K9UV3dTXSNPk9bGvshrOPPwIl7ExDUEpG2SzdOv8tGBqX4QFpyLRWgvFiyjZ29LNfrBMQPiTVdy14b7eOrodWsLNJDl4zGQ","http://lh6.ggpht.com/K9UV3dTXSNPk9bGvshrOPPwIl7ExDUEpG2SzdOv8tGBqX4QFpyLRWgvFiyjZ29LNfrBMQPiTVdy14b7eOrodWsLNJDl4zGQ","http://theopentutorials.com/wp-content/uploads/totlogo.png"};
for (int i = 0; i < firstLastNames.size(); i++) {
RowItem item = new RowItem(images.get(i), null, firstLastNames.get(i));
rowItems.add(item);
}
listView = (ListView) findViewById(R.id.list);
CustomListViewAdapter adapter = new CustomListViewAdapter(BdaylistActivity.this,
R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(BdaylistActivity.this);
m_ProgressDialog.dismiss();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
答案 0 :(得分:0)
希望这可能有所帮助。
当我想从互联网上获取图像并将其显示在我的Android应用程序中时,我总是使用“Aquery”
https://code.google.com/p/android-query/
它快速,易于使用,只需很少的代码就可以实现神奇的效果。 : - )
答案 1 :(得分:0)
在您的CustomListViewAdapter类方法
中public View getView(int position,View convertView,ViewGroup parent){ ViewHolder holder = null; RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtDesc.setText(rowItem.getDesc());
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
return convertView;
}
holder.imageView.setImageResource(rowItem.getImageId());
在此行中首先将url图像转换为位图 converting image from url to bitmap 然后在imageview中设置这个位图
holder.imageView.setImageBitmap(位图)