Java 图像的自定义列表视图适配器语法
我是Android开发新手,正在创建我的第一个应用程序。该应用程序拍摄一张图片,预览该图片,从数据库中随机选择一张图片上传到服务器,并在listview中显示JSON文本信息 我试图使用一个自定义列表视图适配器来允许JSON信息的对应图片出现在LISTVIEW中,而不是imageview中 我已经彻底研究了listview适配器,以了解语法以及在何处放置代码,从而正确地实现带有图片的listview。但是,我收到四条错误消息,它们阻止了代码的编译。我意识到这个网站上还有其他关于类似主题的帖子,但我一直在围绕这些帖子进行挖掘,并试图实施各州的解决方案,但没有成功 下面是我的代码:Java 图像的自定义列表视图适配器语法,java,android,json,listview,android-adapter,Java,Android,Json,Listview,Android Adapter,我是Android开发新手,正在创建我的第一个应用程序。该应用程序拍摄一张图片,预览该图片,从数据库中随机选择一张图片上传到服务器,并在listview中显示JSON文本信息 我试图使用一个自定义列表视图适配器来允许JSON信息的对应图片出现在LISTVIEW中,而不是imageview中 我已经彻底研究了listview适配器,以了解语法以及在何处放置代码,从而正确地实现带有图片的listview。但是,我收到四条错误消息,它们阻止了代码的编译。我意识到这个网站上还有其他关于类似主题的帖子,但
public class JSONBuilderActivity extends ListActivity {
private ProgressDialog pDialog;
//URL to get JSON
private static String url = ".........";
//JSON Node names
private static final String TAG_CARS = "cars"; //root
private static final String TAG_CARID = "CarID";
private static final String TAG_CARVIN = "CarVIN";
private static final String TAG_IMG = "CarMainImage";
CustomListViewAdapter adapter;
JSONArray carid = null; //Initializes JSON array
private CustomListViewAdapter clva = null;
ListView lv;
List<Item> item = new ArrayList<JSONBuilderActivity.Item>();
static String response = null;
//Hashmap for ListView
ArrayList<HashMap<String, Object>> caridList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lv = getListView();
//ArrayList<Item> item;
lv.setAdapter(adapter);
lv = (ListView) findViewById(R.id.list_item);
adapter = new CustomListViewAdapter(this,item);
lv.setAdapter(adapter);
//Listview on item click listener
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Gets values from selected ListItem
String cars = ((TextView) view.findViewById(R.id.cars)).getText().toString();
String car_id = ((TextView) view.findViewById(R.id.car_id)).getText().toString();
String car_vin = ((TextView) view.findViewById(R.id.car_vin)).getText().toString();
String model_img = ((ImageView) view.findViewById(R.id.model_img)).getTag().toString();
Intent in = new Intent(JSONBuilderActivity.this, MainActivity.class);
//Sends data to MainActivity
in.putExtra("TAG_CARS", cars);
in.putExtra("TAG_CARID", car_id);
in.putExtra("TAG_CarVin", car_vin);
in.putExtra("TAG_IMG", model_img);
startActivity(in);
}
});
//Calls async task to get json
new GetCars().execute();
}
public class ServiceHandler {
public final static int GET = 1;
public final static int POST = 2;
public ServiceHandler() {
}
/**
* Makes service call
*
* @url - url to make request
* @method - http request method
*/
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method, null);
}
/**
* Makes service call
*
* @url - url to make request
* @method - http request method
* @params - http request params
*/
public String makeServiceCall(String url, int method, ArrayList<NameValuePair> params) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
//Checks http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
//Adds post params
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
//Appends params to url
if (params != null) {
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "CarMainImage", null);
return Uri.parse(path);
}
public void saveBmpToFile(File filename, Bitmap bmp) {
FileOutputStream out = null;
try {
out = new FileOutputStream(filename);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public boolean renameFileExtension(String source, String newExtension) {
String target;
String currentExtension = getFileExtension(source);
if (currentExtension.equals("")) {
target = source + "." + newExtension;
} else {
target = source.replaceFirst(Pattern.quote("." +
currentExtension) + "$", Matcher.quoteReplacement("." + newExtension));
}
return new File(source).renameTo(new File(target));
}
public String getFileExtension(String f) {
String ext = "";
int i = f.lastIndexOf('.');
if (i > 0 && i < f.length() - 1) {
ext = f.substring(i + 1);
}
return ext;
}
/*
* Async task class to get json by making HTTP call
*/
private class GetCars extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
caridList = new ArrayList<HashMap<String, Object>>();
//Shows progress dialog
pDialog = new ProgressDialog(JSONBuilderActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
//Creates service handler class instance
ServiceHandler sh = new ServiceHandler();
//Makes a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
//Prints the json response in the log
Log.d("GetCars response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
Log.d("try", "in the try");
JSONObject jsonObj = new JSONObject(jsonStr);
Log.d("jsonObject", "new json Object");
//Gets JSON Array node
carid = jsonObj.getJSONArray(TAG_CARS);
Log.d("json array", "user point array");
int len = carid.length();
Log.d("len", "get array length");
for (int i = 0; i < carid.length(); i++) {
JSONObject c = carid.getJSONObject(i);
String car_id = c.getString(TAG_CARID);
Log.d("car_id", car_id);
String car_vin = c.getString(TAG_CARVIN);
Log.d("car_vin", car_vin);
String model_img = c.getString(TAG_IMG);
Log.d("model_img", model_img);
//CustomListViewAdapter adapter = new CustomListViewAdapter(this, R.layout.list_item, item);
// String model_img = c.getString(TAG_IMG);
//Log.d("model_img", model_img);
//Hashmap for single match
HashMap<String, Object> matchGetCars = new HashMap<String, Object>();
//Adds each child node to HashMap key => value
matchGetCars.put(TAG_CARID, car_id);
matchGetCars.put(TAG_CARVIN, car_vin);
matchGetCars.put(TAG_IMG, model_img);
caridList.add(matchGetCars);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
//Dismisses the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updates parsed JSON data into ListView
* */
//ListAdapter adapter = new SimpleAdapter(JSONBuilderActivity.this, caridList, R.layout.list_item,
// new String[]{TAG_CARID, TAG_CARVIN, TAG_IMG}, new int[]{R.id.car_id, R.id.car_vin, R.id.model_img});
// setListAdapter(adapter);
ListView lv = (ListView)findViewById(R.id.list_item);
clva = new CustomListViewAdapter();
lv.setAdapter(clva);
}
}
public class CustomListViewAdapter extends ArrayAdapter<Item> {
private ArrayList<Item> objitem;
Activity context;
public CustomListViewAdapter(ArrayList<Item> item, int ResourceId, Activity context){
super(context, ResourceId, item);
this.context = context;
this.objitem = item;
}
private class ViewHolder {
ImageView model_img;
TextView car_id;
TextView car_vin;
}
public View getView ( int position, View convertView, ViewGroup parent){
ViewHolder holder = null;
final Item item = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder.car_id = (TextView) convertView.findViewById(R.id.car_id);
holder.car_vin = (TextView) convertView.findViewById(R.id.car_vin);
holder.model_img = (ImageView) convertView.findViewById(R.id.model_img);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.car_id.setText(item.getVin());
holder.car_vin.setText(item.getId());
holder.model_img.setImageResource(item.getmodelimg());
return convertView;
}
}
public class Item {
private int model_img;
private String car_id;
private String car_vin;
public Item(int model_img, String car_id, String car_vin) {
this.model_img = model_img;
this.car_id = car_id;
this.car_vin = car_vin;
}
/* Getters
*/
public int getmodelimg() {
return model_img;
}
public String getVin() {
return car_vin;
}
public String getId() {
return car_id;
}
/* Setters
*/
public void setmodelimg(int model_img) {
this.model_img = model_img;
}
public void setVin(String car_vin) {
this.car_vin = car_vin;
}
public void setId(String car_id) {
this.car_id = car_id;
}
}
}
我很难理解我在做什么,所以不正确。我感谢你的任何意见。谢谢。您正在尝试实例化具有此构造函数的适配器
public CustomListViewAdapter(ArrayList<Item> item, int ResourceId, Activity context)
问题:参数不匹配
应该是这样的
adapter = new CustomListViewAdapter(item, R.layout.list_row, this);
请注意,列表行是我编造的,应该是您的应用程序或android sdk中的实际布局文件您好,谢谢您可以理解的回答。我用自己的布局文件应用了更改。然而,我仍然收到这个错误。更详细的错误分析表明,例外参数分为三类:item、ResourceId和context。项目类别仍然不正确。该消息声明,例外项参数为:java…JSONBuilderActivity.item>,实际参数为:item(java…JSONBuilderActivity.item>)。有什么区别?你能详细说明一下吗?谢谢@我发现的唯一区别是构造函数是
CustomListViewAdapter(ArrayList项
,然后您将其传递给ArrayList。请注意类前缀。我不认为这会破坏它,但您可以尝试使它们都相同,以确保它们都相同。不幸的是,将语法更改为匹配并不能解决问题。任何其他建议都将不胜感激。@johnwillams您在onPostEx中有此项。)ecute():clva=new CustomListViewAdapter()
,需要匹配,如果项
与JSONBuilderActivity.Item
@johnnaw是同一类,那么如果没有导入就很难说。您好。我将导入添加到我的帖子中。如果您能看一看,我将不胜感激,因为下面提供的答案即使有额外的评论也仍然不成功。谢谢。
adapter = new CustomListViewAdapter(this,item);
adapter = new CustomListViewAdapter(item, R.layout.list_row, this);