ich erfolgreich hat setzten Elemente in ListView
BaseAdapter
verwenden, und was ich will, ist fragen ... ist es möglich, zu prüfen, ob ListView
in onCreate()
leer ist, oder die gesamte Liste Zahl in onCreate()
bekommen? Vielen Dank.Checkliste Artikel in Listview Android
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomAdapter extends BaseAdapter {
Context context;
ArrayList<Contact> list;
private static LayoutInflater inflater;
public CustomAdapter(Context context,ArrayList<Contact> list){
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(inflater == null)
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null)
convertView = inflater.inflate(R.layout.list_contact,null);
TextView id = (TextView) convertView.findViewById(R.id.id_keranjang);
TextView name = (TextView) convertView.findViewById(R.id.name);
TextView address = (TextView) convertView.findViewById(R.id.contact);
int contact_id = list.get(position).getId();
String contact_name = list.get(position).getName();
String contact_address = list.get(position).getAddress();
id.setText(String.valueOf(contact_id));
kode.setText(contact_name);
judul.setText(contact_address);
return convertView;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
private static final String URL = "http://domain/contact.php";
CustomAdapter customAdapter;
int success = 0;
JSONParser jsonParser = new JSONParser();
ArrayList<Contact> listContact = new ArrayList<Contact>();
JSONObject json=null;
JSONArray contact=null;ListView li;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
li = (ListView) findViewById(R.id.listView);
new getContactAsync().execute();
//int c = li.getAdapter().getCount(); not working
}
class getContactAsync extends AsyncTask<String,String,ArrayList<Contact>>{
@Override
protected ArrayList doInBackground(String... params) {
json = jsonParser.getFromURL(URL);
try {
contact = json.getJSONArray("contact");
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
for (int i = 0; i < contact.length(); i++) {
JSONObject c = contact.getJSONObject(i);
Contact a = new Contact();
a.setId(c.optInt(TAG_ID));
a.setName(c.optString(TAG_NAME));
a.setAddress(c.optString(TAG_ADDRESS));
listContact.add(a);
}
}
} catch (JSONException e) {
Log.d("ERROR", e.toString());
}
return listContact;
}
@Override
protected void onPostExecute(ArrayList<Contact> s) {
super.onPostExecute(s);
customAdapter = new CustomAdapter(MainActivity.this,listContact);
li.setAdapter(customAdapter);
}
}
dank ... thats, gearbeitet .. –
Sie herzlich willkommen ... @ –