我的代码是-1] CustomerList.java
public class CustomerList extends Activity {
private String TAG = CustomerList.class.getSimpleName();
private ProgressDialog progressDialog;
private ListView listView;
ListViewAdapter listViewAdapter;
ArrayList<HashMap<String, String>> getAllCustomers;
static String CUSTOMER_ID= "customer_id";
static String CUSTOMER_CONTACT_NUMBER="customer_contact_number";
static String CUSTOMER_NAME="customer_name";
static String ZONE="zone";
private static String url = "http://103.229.245.235/cb.selectnetworks.in/getAllCustomers";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
getAllCustomers = new ArrayList<>();
listView = (ListView) findViewById(R.id.ListViewCustomerList);
new GetCustomerData().execute();
}
public class GetCustomerData extends AsyncTask<URL, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog=new ProgressDialog(CustomerList.this);
progressDialog.setMessage("please wait");
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected String doInBackground(URL... urls) {
HttpHandler httpHandler= new HttpHandler();
String jsonStr=httpHandler.makeServiceCall(url);
Log.e(TAG,"Response from url: " +jsonStr);
if(jsonStr !=null){
try {
JSONObject jsonObject= new JSONObject(new String(jsonStr));
JSONArray arr= jsonObject.getJSONArray("data");
for(int i=0;i<arr.length();i++){
JSONObject obj = arr.optJSONObject(i);// (JSONObject) arr.get(i);
String customer_id = obj.getString("customer_id");
String customer_name = obj.getString("customer_name");
String customer_contact_number = obj.getString("customer_contact_number");
String zone = obj.getString("zone_id");
// Toast.makeText(CustomerList.this,customer_id,Toast.LENGTH_LONG).show();
HashMap<String,String> customData = new HashMap<>();
customData.put("customer_id",customer_id);
customData.put("customer_name",customer_name);
customData.put("customer_contact_number",customer_contact_number);
customData.put("zone",zone);
getAllCustomers.add(customData);
}
} catch (final JSONException e) {
Log.e(TAG,"json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),"json error : " + e.getMessage(),Toast.LENGTH_LONG).show();
}
});
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String s) {
if(progressDialog.isShowing())
progressDialog.dismiss();
Toast.makeText(CustomerList.this,getAllCustomers.toString(),Toast.LENGTH_LONG).show();
// ListAdapter listAdapter = new SimpleAdapter(
// CustomerList.this,getAllCustomers,R.layout.activity_listviewitem,new String[]{"customer_id","customer_name",
//"customer_contact_number", "zone"}, new int[] {R.id.customer_id,R.id.customer_name,R.id.customer_mobile,R.id.customer_zone});
// listView.setAdapter(listAdapter);
listView = (ListView)findViewById(R.id.ListViewCustomerList);
listViewAdapter = new ListViewAdapter(CustomerList.this,getAllCustomers);
listView.setAdapter(listViewAdapter);
}
}
}
2] ListViewAdapter.java
public class ListViewAdapter extends BaseAdapter {
Context context;
LayoutInflater layoutInflater;
ArrayList<HashMap<String, String>> data;
HashMap<String, String> result = new HashMap<String, String>();
public ListViewAdapter(Context context,ArrayList<HashMap<String , String>> getAllCustomers){
this.context=context;
data=getAllCustomers;
}
@Override
public int getCount() {
data.size();
return 0;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
TextView customer_id;
TextView customer_name;
TextView customer_contact_no;
TextView zone;
layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemview = layoutInflater.inflate(R.layout.activity_listviewitem,viewGroup,false);
// get the position
result= data.get(i);
// assign variables values
customer_id= (TextView) itemview.findViewById(R.id.TextViewcustomer_id);
customer_name=(TextView) itemview.findViewById(R.id.TextVIewcustomer_name);
customer_contact_no =(TextView) itemview.findViewById(R.id.TextViewcustomer_mobile);
zone=(TextView) itemview.findViewById(R.id.TextViewcustomer_zone);
// set the result into text view
customer_id.setText(result.get(CustomerList.CUSTOMER_ID));
customer_name.setText(result.get(CustomerList.CUSTOMER_NAME));
customer_contact_no.setText(result.get(CustomerList.CUSTOMER_CONTACT_NUMBER));
zone.setText(result.get(CustomerList.ZONE));
itemview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
result=data.get(i);
}
});
return itemview;
}
}
3] HttpHandler类
public class HttpHandler {
public HttpHandler(){
}
public String makeServiceCall(String reqUrl){
String response=null;
try {
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException: " + e.getMessage());
} catch (ProtocolException e) {
Log.e(TAG, "ProtocolException: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
return response;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
4]xml file-activity_list:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/EditTextSearch"
/>
<ListView
android:id="@+id/ListViewCustomerList"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
5]actvity_listviewitem:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/TextViewcustomer_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="@color/colorPrimaryDark"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/TextVIewcustomer_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:textColor="@color/colorAccent" />
<TextView
android:id="@+id/TextViewcustomer_mobile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:textStyle="bold" />
<TextView
android:id="@+id/TextViewcustomer_zone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:textStyle="bold" />
</LinearLayout>
答案 0 :(得分:0)
更改此方法
@Override
public int getCount() {
return data.size();
}
这应该可以解决您的问题
getCount()
方法应该返回列表的大小。