我有listview,其子项来自服务器。我已经设法在每个平滑滚动后获得10个列表项但我的问题是在添加新项目之后,之前的数据被清除。我也想要旧儿童用品。我也使用了MyListAdapter().notifyDataSetChanged();
但没有获得理想的结果。
伙计们请帮帮我。先感谢您。
这是我的代码片段:
Get_list.java:
private class getbride extends AsyncTask<String, String, String>{
ProgressDialog pDialog;
@Override
protected void onPreExecute(){
pDialog = new ProgressDialog(User_list.this);
pDialog.setMessage("Loading...");
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try{
String link = "http://Webservice.php";
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost();
request.setURI(new URI(link));
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
String lazy=String.valueOf(lcnt);
postParameters.add(new BasicNameValuePair("lazy_cnt", lazy));
UrlEncodedFormEntity form = new UrlEncodedFormEntity(postParameters);
request.setEntity(form);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
result=EntityUtils.toString(entity);
result1 = result;
System.out.println("Result: "+result);
SharedPreferences pref1 = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
Editor ed = pref1.edit();
ed.putString("search_res", result);
ed.commit();
}catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
pDialog.dismiss();
lv=(ListView)findViewById(R.id.listreq15);
try {
if(User_list.result!=null){
jArray=new JSONArray(User_list.result);
prgmNameList =new ArrayList<String>();
prgmImages=new ArrayList<String>();
if(jArray.length()>0)
{
for(int j=0;j<jArray.length();j++){
prgmNameList.add(new JSONObject(jArray.getString(j)).get("name")+"");
prgmImages.add(new JSONObject(jArray.getString(j)).get("education")+"");
}
System.out.println("#########"+prgmNameList);
CustomAdapter cadapt = new CustomAdapter(c, prgmNameList, prgmImages);
flag_loading=false;
lv.setAdapter(cadapt);
lv.invalidateViews();
cadapt.notifyDataSetChanged();
int count=lv.getAdapter().getCount();
String cnt_bride=String.valueOf(count);
TextView list_cnt=(TextView)findViewById(R.id.cnt);
list_cnt.setText(cnt_bride);
lv.setOnScrollListener(new OnScrollListener() {
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
{
if(flag_loading == false)
{
flag_loading = true;
new getbride().execute("param1","param2","param3");
lcnt++;
int count=lv.getAdapter().getCount();
String cnt_bride=String.valueOf(count);
TextView list_cnt=(TextView)findViewById(R.id.cnt);
list_cnt.setText(cnt_bride);
}
}
}
@Override
public void onScrollStateChanged(AbsListView arg0,
int arg1) {
// TODO Auto-generated method stub
}
});
}
}else{
Toast.makeText(getApplicationContext(),"No internet connection...", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Activity.java:
public class User_list extends Activity {
ListView lv;
JSONArray jArray;
Context c;
public static int cnt,lcnt;
public static String result,result1,partial,u_id,result2,edu,f_name;
int age,ht;
boolean flag_loading;
public static List<String> prgmImages;
public static List<String> prgmNameList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_list);
Intent i = getIntent();
String call=i.getStringExtra("call_for");
if(call==null){
try {
search();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(call.equalsIgnoreCase("br")){
new getbride().execute("param1","param2","param3");
lcnt=0;
}
else{
new getgroom().execute("param1","param2","param3");
lcnt=0;
}
c=this; }
答案 0 :(得分:0)
声明并设置CustomAdapter
,并初始化ArrayLists
中的Activity
:
public class User_list extends Activity {
ListView lv;
JSONArray jArray;
Context c;
public static int cnt,lcnt;
public static String result,result1,partial,u_id,result2,edu,f_name;
int age,ht;
boolean flag_loading;
public static List<String> prgmImages;
public static List<String> prgmNameList;
CustomAdapter cadapt; //Added declaration here
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_list);
lv=(ListView)findViewById(R.id.listreq15); //added
prgmNameList =new ArrayList<String>(); //added
prgmImages=new ArrayList<String>(); //added
cadapt = new CustomAdapter(c, prgmNameList, prgmImages); //initialize this here
lv.setAdapter(cadapt); //added
Intent i = getIntent();
String call=i.getStringExtra("call_for");
if(call==null){
try {
search();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(call.equalsIgnoreCase("br")){
new getbride().execute("param1","param2","param3");
lcnt=0;
}
else{
new getgroom().execute("param1","param2","param3");
lcnt=0;
}
c=this;
}
然后在您的AsyncTask
中,只需在数据集中添加新值后调用notifyDataSetChanged()
:
@Override
protected void onPostExecute(String result) {
pDialog.dismiss();
//lv=(ListView)findViewById(R.id.listreq15); //remove
try {
if(User_list.result!=null){
jArray=new JSONArray(User_list.result);
//prgmNameList =new ArrayList<String>(); //remove
//prgmImages=new ArrayList<String>(); //remove
if(jArray.length()>0)
{
for(int j=0;j<jArray.length();j++){
prgmNameList.add(new JSONObject(jArray.getString(j)).get("name")+"");
prgmImages.add(new JSONObject(jArray.getString(j)).get("education")+"");
}
System.out.println("#########"+prgmNameList);
//CustomAdapter cadapt = new CustomAdapter(c, prgmNameList, prgmImages); //remove
flag_loading=false;
//lv.setAdapter(cadapt); //remove
//lv.invalidateViews(); //don't think this is needed
cadapt.notifyDataSetChanged(); //This is needed!
int count=lv.getAdapter().getCount();
String cnt_bride=String.valueOf(count);
TextView list_cnt=(TextView)findViewById(R.id.cnt);
list_cnt.setText(cnt_bride);
lv.setOnScrollListener(new OnScrollListener() {
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
{
if(flag_loading == false)
{
flag_loading = true;
new getbride().execute("param1","param2","param3");
lcnt++;
int count=lv.getAdapter().getCount();
String cnt_bride=String.valueOf(count);
TextView list_cnt=(TextView)findViewById(R.id.cnt);
list_cnt.setText(cnt_bride);
}
}
}
@Override
public void onScrollStateChanged(AbsListView arg0,
int arg1) {
// TODO Auto-generated method stub
}
});
}
}else{
Toast.makeText(getApplicationContext(),"No internet connection...", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
请注意,我不认为此处需要调用invalidateViews()
。
请参阅此帖子:Is there any difference between `ListView.invalidateViews()` and 'Adapter.notifyDataSetChanged()'?