获取json数据并设置Model类此数据设置列表视图或recyclerview详细信息获取json数据并设置Model类此数据设置列表视图或recyclerview详细信息获取json数据并设置Model类此数据设置列表视图或recyclerview细节
private void getdata() {
String url = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
final StringRequest placeReq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String Response) {
Log.e("Place Response", Response.toString());
pDialog.dismiss();
try {
JSONObject json = new JSONObject(Response);
JSONArray jArray = json.getJSONArray("history");
Log.e("jArray", "" + jArray);
ArrayList<ModelClass> modelClassArrayList = new ArrayList<ModelClass>();
ModelClass modelClass = new ModelClass();
for (int i = 0; i < jArray.length(); i++) {
try {
JSONObject oneObject = jArray.getJSONObject(i);
String oneObjectsItem = oneObject.getString("type");
if (oneObjectsItem.equals("approval")) {
modelClass.setTransactionid(oneObject.getString("transactionid"));
modelClass.setName(oneObject.getString("name"));
modelClass.setAddress(oneObject.getString("address"));
modelClass.setType(oneObject.getString("type"));
modelClass.setTag(oneObject.getString("tag"));
modelClass.setStatus(oneObject.getString("status"));
modelClass.setDate(oneObject.getString("date"));
modelClass.setImage(oneObject.getString("image"));
modelClassArrayList.add(modelClass);
Log.e("name", "" + oneObject.getString("name"));
Log.e("address", "" + oneObject.getString("address"));
Log.e("type", "" + oneObject.getString("type"));
Log.e("status", "" + oneObject.getString("status"));
Log.e("date", "" + oneObject.getString("date"));
}
} catch (Exception e) {
Log.e("error", "" + e);
}
}
for (int j = 0; j < modelClassArrayList.size(); j++) {
Log.e("name", "" + modelClassArrayList.get(j).getDate());
}
Log.e("Size ", "" + modelClassArrayList.size());
} catch (Exception e1) {
Log.e("Error", "" + e1);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Place", "Error Response: " + error.getMessage());
pDialog.dismiss();
}
})
{
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("userid", "1");
Log.e("Restaurant", "Posting params: " + params.toString());
return params;
}
};
Volley.newRequestQueue(this).add(placeReq);
}
私有类Connection扩展了AsyncTask { static final String URL =&#34; http://api.androidhive.info/pizza/?format=xml&#34 ;; static final String KEY_ITEM =&#34; item&#34 ;; //父节点 static final String KEY_ID =&#34; id&#34 ;; static final String KEY_NAME =&#34; name&#34 ;; static final String KEY_COST =&#34; cost&#34 ;; static final String KEY_DESC =&#34; description&#34 ;;
@Override
protected Object doInBackground(Object... arg0) {
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Log.e("xml", "" + xml);
Document doc = parser.getDomElement(xml); // getting DOM element
Log.e("doc", "" + doc.toString());
NodeList nl = doc.getElementsByTagName("item");
Log.e("nl", "" + nl.toString());
for (int i = 0; i < nl.getLength(); i++) {
ModelClass modelClass = new ModelClass();
Element e = (Element) nl.item(i);
modelClass.setId(parser.getValue(e, KEY_ID));
modelClass.setName(parser.getValue(e, KEY_NAME));
modelClass.setCost("Rs." + parser.getValue(e, KEY_COST));
modelClass.setDescription(parser.getValue(e, KEY_DESC));
modelClass.setDate(parser.getValue(e, "date"));
modelClassArrayList.add(modelClass);
Log.e("Size", "" + modelClassArrayList.size() + "" + i);
}
for (int j = 0; j < modelClassArrayList.size(); j++) {
Log.e("Id", modelClassArrayList.get(j).getId());
Log.e("Name", modelClassArrayList.get(j).getName());
Log.e("Cost", modelClassArrayList.get(j).getCost());
Log.e("Desc", modelClassArrayList.get(j).getDescription());
Log.e("date", modelClassArrayList.get(j).getDate());
}
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
}
}
class XMLParser{
public XMLParser(){
}
public String getXmlFromUrl(String url) {
String xml = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return xml;
}
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
}
`public class FavouriteAnimalListAdapter扩展了RecyclerView.Adapter {
Context context;
ArrayList<FavouritreAnimal> favouriteAnimalArray;
FavouritreAnimal animalfavourite;
public FavouriteAnimalListAdapter(Context context, ArrayList<FavouritreAnimal> favouriteAnimalArray) {
this.context = context;
this.favouriteAnimalArray = favouriteAnimalArray;
}
@Override
public favouriteListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.item_card_vew_favourite_list_view, parent, false);
favouriteListViewHolder favouriteListViewHolder = new favouriteListViewHolder(v);
return favouriteListViewHolder;
}
@Override
public void onBindViewHolder(favouriteListViewHolder holder, final int position) {
animalfavourite = favouriteAnimalArray.get(position);
holder.tvFavouriteAnimalName.setText(animalfavourite.getAnimalname());
Glide.with(context)
.load(animalfavourite.getAnimalimage())
.into(holder.ivFavouriteAnimalImage);
String animaldesc = Html.fromHtml(context.getString(animalfavourite.getAnimal_description())).toString();
if (animaldesc.length() > 100) {
String animaldesc1 = Html.fromHtml(animaldesc).toString();
animaldesc = animaldesc1.substring(0, 110) + "\n" + "...";
}
holder.tvFavouriteAnimalDescription.setText(animaldesc);
holder.cvFavouriteanimal.setTag(R.string.app_name, animalfavourite.getAnimalid());
holder.cvFavouriteanimal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CardView cv = (CardView) v;
int id = (int) cv.getTag(R.string.app_name);
Intent intent = new Intent(context, FavourityAnimalViewPager.class);
intent.putExtra(Utils.ANIMAL_ID, position);
context.startActivity(intent);
((Activity)context).finish();
}
});
}
@Override
public int getItemCount() {
return favouriteAnimalArray.size();
}
static class favouriteListViewHolder extends RecyclerView.ViewHolder {
CardView cvFavouriteanimal;
TextView tvFavouriteAnimalName, tvFavouriteAnimalDescription;
ImageView ivFavouriteAnimalImage;
public favouriteListViewHolder(View itemView) {
super(itemView);
cvFavouriteanimal = (CardView) itemView.findViewById(R.id.cv_favourite_animal);
tvFavouriteAnimalName = (TextView) itemView.findViewById(R.id.tv_favourite_animal_name);
ivFavouriteAnimalImage = (ImageView) itemView.findViewById(R.id.ib_favourite_animal_image);
tvFavouriteAnimalDescription = (TextView) itemView.findViewById(R.id.tv_favourite_animal_description);
}
}
}`
答案 0 :(得分:0)
类MainActivity扩展了AppCompatActivity {
ProgressDialog pDialog;
RecyclerView recyclerView;
EditText search;
ArrayList<ModelClass> modelClassArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
search = (EditText) findViewById(R.id.search);
getDataToServer();
addFnction();
}
void addFnction() {
search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
charSequence = charSequence.toString();
ArrayList<ModelClass> filteredList = new ArrayList<ModelClass>();
for (int j = 0; j < modelClassArrayList.size(); j++) {
final String text = modelClassArrayList.get(j).getName().toLowerCase();
if (text.contains(charSequence)) {
filteredList.add(modelClassArrayList.get(j));
}
}
recyclerView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(llm);
AdapterClass adapterClass = new AdapterClass(MainActivity.this, filteredList);
recyclerView.setAdapter(adapterClass);
adapterClass.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
private void getDataToServer() {
String url = "http://theappguruz.in/php/DemoJSON/api/user/contacts#sthash.LouFoc7n.dpuf";
pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
final StringRequest placeReq = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String Response) {
Log.e("Place Response", Response.toString());
pDialog.dismiss();
try {
JSONObject json = new JSONObject(Response);
JSONArray jArray = json.getJSONArray("Payload");
String msg = json.getString("Message");
Log.e("Masssssss", "" + msg);
Log.e("jArray", "" + jArray);
modelClassArrayList = new ArrayList<ModelClass>();
for (int i = 0; i < jArray.length(); i++) {
ModelClass modelClass = new ModelClass();
JSONObject oneObject = jArray.getJSONObject(i);
modelClass.setName(oneObject.getString("FirstName"));
modelClass.setState(oneObject.getString("LastName"));
JSONObject twoObject = oneObject.getJSONObject("Phone");
modelClass.setDescription(twoObject.getString("Mobile"));
modelClassArrayList.add(modelClass);
}
recyclerView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(llm);
Collections.sort(modelClassArrayList, new Comparator<ModelClass>() {
public int compare(ModelClass p1, ModelClass p2) {
return (p1.getName()).compareTo(p2.getName());
}
});
AdapterClass adapterClass = new AdapterClass(MainActivity.this, modelClassArrayList);
recyclerView.setAdapter(adapterClass);
} catch (Exception e) {
Log.e("Exxx", "" + e);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Place", "Error Response: " + error.getMessage());
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
HashMap<String, String> headers = new HashMap<String, String>();
// headers.put("Content-Type", "application/json; charset=utf-8");
return params;
}
};
Volley.newRequestQueue(this).add(placeReq);
}
}
答案 1 :(得分:0)
public class AdapterClass扩展了RecyclerView.Adapter { 上下文背景; ArrayList modelClassArrayList; ModelClass modelClass;
public AdapterClass(Context context, ArrayList<ModelClass> modelClassArrayList) {
this.context = context;
this.modelClassArrayList = modelClassArrayList;
}
@Override
public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.item_card_vew_list_view, parent, false);
ListViewHolder listViewHolder = new ListViewHolder(v);
return listViewHolder;
}
@Override
public void onBindViewHolder(ListViewHolder holder, final int position) {
modelClass = modelClassArrayList.get(position);
holder.textViewcity.setText(modelClass.getName());
holder.textViewstate.setText(modelClass.getState());
holder.textViewdscri.setText(modelClass.getDescription());
holder.cvView.setTag(position);
holder.cvView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("Position", "" + position);
Log.e("Name", "" + modelClassArrayList.get(position).getName());
Log.e("State", "" + modelClassArrayList.get(position).getState());
Log.e("Description", "" + modelClassArrayList.get(position).getDescription());
Intent intent = new Intent(context, SecondAct.class);
intent.putExtra("name", modelClassArrayList.get(position).getName());
intent.putExtra("last", modelClassArrayList.get(position).getState());
intent.putExtra("phone", modelClassArrayList.get(position).getDescription());
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return modelClassArrayList.size();
}
static class ListViewHolder extends RecyclerView.ViewHolder {
CardView cvView;
TextView textViewdscri, textViewstate, textViewcity;
public ListViewHolder(View itemView) {
super(itemView);
cvView = (CardView) itemView.findViewById(R.id.card_view);
textViewcity = (TextView) itemView.findViewById(R.id.city);
textViewstate = (TextView) itemView.findViewById(R.id.state);
textViewdscri = (TextView) itemView.findViewById(R.id.dscri);
}
}
}
答案 2 :(得分:0)
public class static_data扩展了AppCompatActivity {
args.length
}