我已经制作了一个自定义适配器,用于将解析后的数据绑定到listView中,我试过如下,但它只给我第一个值。请帮助我。我想将所有值绑定到listView但是目前我只能只绑定1 \一个值.. 的 Registration.java
package com.epe.yehki.ui;
import java.util.ArrayList;
import java.util.HashMap;
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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.epe.yehki.adapter.CountryAdapter;
import com.epe.yehki.adapter.WholesaleProductAdapter;
import com.epe.yehki.backend.BackendAPIService;
import com.epe.yehki.backend.RegisterationAPI;
import com.epe.yehki.backend.ResponseListener;
import com.epe.yehki.uc.Menu;
import com.epe.yehki.util.Const;
import com.epe.yehki.util.Const.API_RESULT;
import com.example.yehki.R;
public class RegistrationActivity extends Activity {
public RegisterationAPI registartionAPI;
private EditText fName;
private EditText lName;
private EditText eMail;
private Button register;
private EditText contact;
private EditText password;
private ListView countrylist;
private ListView statelist;
private TextView tv_country;
private TextView state;
JSONArray countries = null;
// Fetching List
JSONObject jsonObj;
JSONArray contries = null;
ArrayList<HashMap<String, String>> countryList;
private CountryAdapter countryContent;
// Hashmap for ListView
public com.epe.yehki.uc.Menu regMenu;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_registration);
// FINDING IDS...
fName = (EditText) findViewById(R.id.et_fname);
lName = (EditText) findViewById(R.id.et_lname);
eMail = (EditText) findViewById(R.id.et_email);
contact = (EditText) findViewById(R.id.et_contact);
password = (EditText) findViewById(R.id.et_pwd);
tv_country = (TextView) findViewById(R.id.et_country);
state = (TextView) findViewById(R.id.et_state);
regMenu = (Menu) findViewById(R.id.menuReg);
register = (Button) findViewById(R.id.btn_register);
countrylist = (ListView) findViewById(R.id.country_list);
countrylist.setVisibility(View.GONE);
statelist = (ListView) findViewById(R.id.state_list);
countryList = new ArrayList<HashMap<String, String>>();
regMenu.setSelectedTab(1);
new GetCountries().execute();
countrylist.setVisibility(View.GONE);
// COUNTRY LIST CLICK EVENT...!!!
countrylist.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// getting values from selected ListItem
String countryname = ((TextView) view.findViewById(R.id.name)).getText().toString();
tv_country.setText(countryname);
// countrylist.setVisibility(view.GONE);
}
});
// REGISTER BUTTOM CLICK EVENT.....!
register.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (!fName.getText().toString().equals("") && fName.getText().toString() != null) {
if ((!lName.getText().toString().equals("") && lName.getText().toString() != null)) {
if ((!tv_country.getText().toString().equals("") && tv_country.getText().toString() != null)) {
if ((!state.getText().toString().equals("") && state.getText().toString() != null)) {
if ((!eMail.getText().toString().equals("") && eMail.getText().toString() != null)) {
if ((!password.getText().toString().equals("") && password.getText().toString() != null)) {
if ((!contact.getText().toString().equals("") && contact.getText().toString() != null)) {
} else {// CONTACT NUMBER
validation(getResources().getString(R.string.valid_number));
password.requestFocus();
}
} else {// password...
validation(getResources().getString(R.string.valid_pwd));
password.requestFocus();
}
} else {// EMAIL
validation(getResources().getString(R.string.valid_mail));
eMail.requestFocus();
}
} else {// STATE
validation(getResources().getString(R.string.valid_state));
state.requestFocus();
}
} else {// COUNTRY
validation(getResources().getString(R.string.valid_country));
tv_country.requestFocus();
}
} else {// LAST NAME
validation(getResources().getString(R.string.valid_lname));
lName.requestFocus();
}
} else {// FIRST NAME
validation(getResources().getString(R.string.valid_fname));
fName.requestFocus();
}
}
});
// COUINTRY LIST CLICK EVENT...!!!!
tv_country.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
countrylist.setVisibility(View.VISIBLE);
}
});
}
void validation(String msg) {
Toast.makeText(RegistrationActivity.this, msg, Toast.LENGTH_SHORT).show();
}
ResponseListener responseListener = new ResponseListener() {
@Override
public void onResponce(String api, API_RESULT result, Object obj) {
System.out.println("::::::::::::::inside response Listener::::::::");
if (progressDialog != null && progressDialog.isShowing())
progressDialog.dismiss();
if (api.equals(Const.API_LOGIN)) {
System.out.println("::::::::::::::inside response Listener::::::::1");
if (result == Const.API_RESULT.SUCCESS) {
registartionAPI = null;
// as
// doctor
System.out.println("success Login::::::::::>>>>>>>>>>>>>>>>>>" + result);
startActivity(new Intent(RegistrationActivity.this, HomeActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
} else {
System.out.println("failed Login::::::::::>>>>>>>>>>>>>>>>>>" + result);
}
}
}
};
// ASYNC TASK FOR GETTING COUNTRY LIST...!!!
private class GetCountries extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
BackendAPIService sh = new BackendAPIService();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(Const.API_COUTRY, BackendAPIService.GET);
Log.d("Response: ", "> " + jsonStr);
try {
if (jsonStr != null) {
jsonObj = new JSONObject(jsonStr);
if (jsonObj.has(Const.TAG_COUNTY_LIST)) {
contries = jsonObj.getJSONArray(Const.TAG_COUNTY_LIST);
System.out.println(":::::::::PRODUCT lENGTH:::::::::::" + contries.length());
if (contries != null && contries.length() != 0) {
// looping through All Contacts
for (int i = 0; i < contries.length(); i++) {
JSONObject c = contries.getJSONObject(i);
String Con_id = c.getString(Const.TAG_COUNTRY_ID);
System.out.println("::::::::::::::::::PARSING PRODUCT ID:::::::::::::" + Con_id);
String CountryName = c.getString(Const.TAG_COUNTRY_NAME);
HashMap<String, String> country = new HashMap<String, String>();
// adding each child node to HashMap key =>
// value
country.put(Const.TAG_COUNTRY_ID, Con_id);
country.put(Const.TAG_COUNTRY_NAME, CountryName);
countryList.add(country);
}
}
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
} catch (JSONException e) {
e.printStackTrace();
System.out.println("::::::::::::::::::got an error::::::::::::");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
countryContent = new CountryAdapter(RegistrationActivity.this, countryList);
countrylist.setAdapter(countryContent);
}
}
}
Adapter.java
package com.epe.yehki.adapter;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.epe.yehki.util.Const;
import com.example.yehki.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class CountryAdapter extends BaseAdapter {
public ArrayList<HashMap<String, String>> countryArray;
private Context mContext;
public CountryAdapter(Context paramContext, ArrayList<HashMap<String, String>> countryList) {
this.mContext = paramContext;
this.countryArray = countryList;
}
public int getCount() {
return this.countryArray.size();
}
public Object getItem(int paramInt) {
return Integer.valueOf(paramInt);
}
public long getItemId(int paramInt) {
return paramInt;
}
@SuppressWarnings("static-access")
public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
LayoutInflater localLayoutInflater = (LayoutInflater) this.mContext.getSystemService("layout_inflater");
Viewholder localViewholder = null;
if (paramView == null) {
paramView = localLayoutInflater.inflate(R.layout.list_item, paramViewGroup, false);
localViewholder = new Viewholder();
localViewholder.con_id = ((TextView) paramView.findViewById(R.id.cat_id));
localViewholder.con_name = ((TextView) paramView.findViewById(R.id.name));
paramView.setTag(localViewholder);
} else {
localViewholder = new Viewholder();
localViewholder = (Viewholder) paramView.getTag();
}
localViewholder.con_name.setText(countryArray.get(paramInt).get(Const.TAG_COUNTRY_NAME));
System.out.println("::::::::::::::::::;PRDUCT name" + countryArray.get(paramInt).get(Const.TAG_COUNTRY_NAME));
return paramView;
}
static class Viewholder {
TextView con_id;
TextView con_name;
}
}
答案 0 :(得分:0)
从getView else条件中删除localViewholder = new Viewholder();
。在这里,您每次都要创建一个新的持有者对象。
应该是
else {
localViewholder = (Viewholder) paramView.getTag();
}
答案 1 :(得分:0)
请更新
行LayoutInflater localLayoutInflater = (LayoutInflater) this.mContext.getSystemService("layout_inflater");
到
LayoutInflater localLayoutInflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
答案 2 :(得分:0)
请尝试以下课程:
public class CountryAdapter extends BaseAdapter {
public ArrayList<HashMap<String, String>> countryArray;
private Context mContext;
public a(Context paramContext, ArrayList<HashMap<String, String>> countryList) {
this.mContext = paramContext;
this.countryArray = countryList;
}
public int getCount() {
return countryArray.size();
}
public Object getItem(int paramInt) {
return countryArray.get(paramInt);
}
public long getItemId(int paramInt) {
return paramInt;
}
@SuppressWarnings("static-access")
public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
Viewholder localViewholder;
LayoutInflater localLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (paramView == null) {
paramView = localLayoutInflater.inflate(R.layout.list_item, paramViewGroup, false);
localViewholder = new Viewholder();
localViewholder.con_id = ((TextView) paramView.findViewById(R.id.cat_id));
localViewholder.con_name = ((TextView) paramView.findViewById(R.id.name));
paramView.setTag(localViewholder);
} else {
localViewholder = (Viewholder) paramView.getTag();
}
HashMap<String, String> data = (HashMap<String, String>) getItem(paramInt);
localViewholder.con_name.setText(data.get(Const.TAG_COUNTRY_NAME));
return paramView;
}
static class Viewholder {
TextView con_id;
TextView con_name;
}
}
请注意,您无法在System.out.println
上使用Android
,请改为使用Log
课程:Log.d("myTag", "message")
。