我使用ListView使用mysql从wamp服务器显示我的数据,也使用JSON解析,以下是我的代码....
public class JSONTask extends AsyncTask<String, String, List<PumpModel>> {
@Override
protected List<PumpModel> doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String finalJSON = buffer.toString();
JSONObject parentObject = new JSONObject(finalJSON);
JSONArray parentArray = parentObject.getJSONArray("server_response");
List<PumpModel> pumpModelList = new ArrayList<>();
for (int i = 0; i < parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
PumpModel pumpModel = new PumpModel();
pumpModel.setPump(finalObject.getString("Pump"));
pumpModel.setAvailable(finalObject.getString("Available"));
pumpModelList.add(pumpModel);
}
return pumpModelList;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(List<PumpModel> result) {
super.onPostExecute(result);
//TODO need to set the data to the list
PumpAdapter adapter = new PumpAdapter(getApplicationContext(), R.layout.row, result);
lvPump.setAdapter(adapter);
}
}
public class PumpAdapter extends ArrayAdapter {
private List<PumpModel> pumpModelList;
private int resource;
private LayoutInflater inflater;
public PumpAdapter(Context context, int resource, List<PumpModel> objects) {
super(context, resource, objects);
pumpModelList = objects;
this.resource = resource;
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(resource, null);
}
ImageView ivIcon;
TextView tvPump;
ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
tvPump = (TextView) convertView.findViewById(R.id.tvPump);
// Then later, when you want to display image
ImageLoader.getInstance().displayImage(pumpModelList.get(position).getAvailable(), ivIcon); // Default options will be used
tvPump.setText(pumpModelList.get(position).getPump());
return convertView;
}
}
现在我想为列表视图创建一个onclick监听器,可以用来打开另一个活动,并在TextView中显示泵的描述。
答案 0 :(得分:0)
在您的活动中,您定义了列表视图
你可以写
listview.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?>adapter,View v, int position){
ItemClicked item = adapter.getItemAtPosition(position);
Intent intent = new Intent(Activity.this,destinationActivity.class);
//based on item add info to intent
startActivity(intent);
}
});
public ItemClicked getItem(int position){
return items.get(position);
}
答案 1 :(得分:0)
ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
答案 2 :(得分:0)
在您的活动中,您定义了列表视图
你写了listview.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?>adapter,View v, int position){
ItemClicked item = adapter.getItemAtPosition(position);
Intent intent = new Intent(Activity.this,destinationActivity.class);
//based on item add info to intent
startActivity(intent);
}
});
在适配器的getItem中编写
public ItemClicked getItem(int position){
return items.get(position);
}
答案 3 :(得分:0)
实施OnItemClickListener
(适用于您的适配器的最佳位置)&amp;在ListView
对象setOnItemClickListener(yourItemClickListener);
参考: https://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html