你好朋友meeh一直在杀头,实现呃终止这段代码尝试了一切,但没有任何事情来找我..
我认为我在这部分中有一个错误
ImageAdapter ImageAdapter = new ImageAdapter();
lstView1.setAdapter (ImageAdapter);
应该很好但是没能达到呃!
lstView1.setAdapter (new ImageAdapter (this, MyArrList));
我感谢所有帮助我正在尝试寻找名称的应用程序,并在其他活动中传递参数并在mysql数据库中显示listview alamacenados文本和图像。使用json
public class MainActivity extends Activity {
private ProgressDialog pDialog;
public String searchkey;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1b1b1b")));
Intent myIntent = getIntent();
searchkey = myIntent.getStringExtra("texto");
final String url = "http://10.0.2.2/test/getJSON.php?keyword=" + searchkey ;
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 11) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
// listView1
final ListView lstView1 = (ListView)findViewById(R.id.listView1);
class pbar extends AsyncTask<String, String, String> {
public String getJSONUrl (String url) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Download OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download file..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Cargando Productos.Por Favor Espere.");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting Idioms from url
* */
protected String doInBackground(String... args) {
try {
JSONArray data = new JSONArray(getJSONUrl(url));
final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("ImageID", c.getString("ImageID"));
map.put("ImageDesc", c.getString("ImageDesc"));
map.put("ImagePath", c.getString("ImagePath"));
MyArrList.add(map);
// hidePDialog();
}
ImageAdapter ImageAdapter = new ImageAdapter();
lstView1.setAdapter(ImageAdapter);
} catch (JSONException e) {
// TODO Auto-generated catch block
// hidePDialog();
e.printStackTrace();
}
new pbar().execute();
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting the related idioms
pDialog.dismiss();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Context context;
private ArrayList<HashMap<String, String>> MyArr = new ArrayList<HashMap<String, String>>();
// constructor
public ImageAdapter() {
}
public ImageAdapter(Context c, ArrayList<HashMap<String, String>> list) {
// TODO Auto-generated method stub
context = c;
MyArr = list;
}
public int getCount() {
// TODO Auto-generated method stub
return MyArr.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_column, null);
}
// ColImage
ImageView imageView = (ImageView) convertView.findViewById(R.id.ColImgPath);
imageView.getLayoutParams().height = 100;
imageView.getLayoutParams().width = 100;
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
try
{
imageView.setImageBitmap(loadBitmap(MyArr.get(position).get("ImagePath")));
} catch (Exception e) {
// When Error
imageView.setImageResource(android.R.drawable.ic_menu_report_image);
}
// ColPosition
TextView txtPosition = (TextView) convertView.findViewById(R.id.ColImgID);
txtPosition.setPadding(10, 0, 0, 0);
txtPosition.setText("ID : " + MyArr.get(position).get("ImageID"));
// ColPicname
TextView txtPicName = (TextView) convertView.findViewById(R.id.ColImgDesc);
txtPicName.setPadding(50, 0, 0, 0);
txtPicName.setText("Desc : " + MyArr.get(position).get("ImageDesc"));
//hidePDialog();
return convertView;
}
private static final String TAG = "ERROR";
private static final int IO_BUFFER_SIZE = 4 * 1024;
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
private static void closeStream(Closeable stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
android.util.Log.e(TAG, "Could not close stream", e);
}
}
}
private static void copy(InputStream in, OutputStream out) throws IOException {
byte[] b = new byte[IO_BUFFER_SIZE];
int read;
while ((read = in.read(b)) != -1) {
out.write(b, 0, read);
}
}
}
答案 0 :(得分:0)
我无法测试它,所以我会指出一个错误,你肯定在那里: 换行:
ImageAdapter ImageAdapter = new ImageAdapter();
lstView1.setAdapter(ImageAdapter);
例如:
ImageAdapter imageAdapter = new ImageAdapter();
lstView1.setAdapter(imageAdapter);
编辑:
然后尝试:
ImageAdapter imageAdapter = new ImageAdapter(MainActivity.this, MyArrList);
lstView1.setAdapter(imageAdapter);