2015-07-12 50 views
-1

對不起,如果你發現這個問題重複。但我已經看過所有其他的解決方案,我無法找到解決我應用notifyDataSetChanged()的問題的任何解決方案。無法應用自定義適配器notifyDataSetChanged()

我當前的代碼:從VenueSQL1活動我打電話定義適配器

protected void onPostExecute(Void result) { 
     try{ 
     super.onPostExecute(result); 
     // Dismiss the progress dialog 

     if(count==1){ 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 
      count++; 
     } 


     state = lv.onSaveInstanceState(); 
     final ListAdapter myadapter=new customAdapterVenues(VenueSQL1.this,contactList1); 

      runOnUiThread(new Runnable() { 
       public void run() { 

        lv.setAdapter(myadapter); 
       } 
      }); 
      lv.onRestoreInstanceState(state); 
     } 

customAdapterVenues

/** * 創建者Shubham於2015年6月21日。 */

public class customAdapterVenues extends ArrayAdapter<HashMap<String, String>>{ 

private Context mcontext; 
public String unique_id1,Cost1,Type1,Name1; 
public ArrayList<Integer> flags=new ArrayList<>(); 
static final ArrayList<Integer> set=new ArrayList<>(); 
String android_id,android_id1; 
String unique_id,Name,Type="weddinghalls",Cost; 
int flag=0; 
int pos=0; 





public customAdapterVenues(Context context, ArrayList<HashMap<String, String>> main) { 

    super(context, R.layout.list_item, main); 
    // this.notifyDataSetChanged(); 
    mcontext=context; 


} 

public View getView(final int position, View convertView, ViewGroup parent) { 
    LayoutInflater shubhamsinflator=LayoutInflater.from(getContext()); 
    View customview=shubhamsinflator.inflate(R.layout.list_item, parent, false); 
    android_id = Settings.Secure.getString(getContext().getContentResolver(), Settings.Secure.ANDROID_ID); 
    //this.notifyDataSetChanged(); 
    try { 
     flag=0; 
     flags.add(position,0); 
     // set.add(position,0); 


     HashMap<String, String> map = (HashMap<String, String>) getItem(position); 

     final String name1 = map.get("Name"); 
     Name=name1; 
     Log.d("flag:",String.valueOf(position)); 

     String address1 = map.get("Area"); 
     String image = map.get("Image"); 
     String cost = map.get("Cost"); 
     // if(set.get(position)==0) { 
      unique_id = map.get("UniqueId"); 
      // set.set(position, 1); 
     //} 
     android_id1=map.get("UserId"); 
     Cost=cost; 
     final String phone = map.get("Telephone"); 
     //phone=phone.replace(" ",""); 
     // String name1 = map.get("name"); 
     //String name1=getItem(position). 
     TextView shubhamsText = (TextView) customview.findViewById(R.id.name); 
     TextView shubhamsText2 = (TextView) customview.findViewById(R.id.address); 
     TextView shubhamsText3 = (TextView) customview.findViewById(R.id.price); 
     TextView shubhamsText4 = (TextView) customview.findViewById(R.id.price1); 
     ImageView shubhamsimage = (ImageView) customview.findViewById(R.id.imageView2); 
     com.example.shubham.myapp.CircleImageView call = (com.example.shubham.myapp.CircleImageView) customview.findViewById(R.id.but); 
     call.setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         // Intent callIntent = new Intent(Intent.ACTION_CALL); 
         //callIntent.setData(Uri.parse("tel:" + phone)); 
         //getContext().startActivity(callIntent); 
         myDialog(name1, phone); 


        } 


       } 
     ); 


     shubhamsText.setText(name1); 
     shubhamsText2.setText(address1); 
     //cost = cost.replace("Per Plate", ""); 
     shubhamsText3.setText("Rs. "+cost); 
     shubhamsText4.setText("Per Plate"); 
     if (cost.equals("0")) { 
      shubhamsText3.setText("Price on"); 
      shubhamsText4.setText("Request"); 
      // shubhamsText3.setText(" "); 

     if (!image.equals("NA")) 
      Picasso.with(getContext()).load(image).into((ImageView) customview.findViewById(R.id.imageView2)); 
     else 
      shubhamsimage.setImageResource(R.drawable.ina); 

     //shubhamsimage.setImageResource(p); 
    }catch (Exception e){ 
     Log.d("Hello","hello"); 

    } 
     return customview; 

    //return super.getView(position, convertView, parent); 
} 
private ProgressDialog pDialog; 
private AlertDialog.Builder dialogBuilder; 
public static final int CONNECTION_TIMEOUT=1000*15; 
public static final String SERVER_ADDRESS="http://shubhamsapp.esy.es/"; 

private void myDialog(String name1, final String phone) 
{ 
    dialogBuilder=new AlertDialog.Builder(getContext()); 
    dialogBuilder.setTitle(name1+"\n"+" " +phone); 

    dialogBuilder.setPositiveButton("Call", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int w) { 
      Intent callIntent = new Intent(Intent.ACTION_CALL); 
      callIntent.setData(Uri.parse("tel:" + phone)); 
      getContext().startActivity(callIntent); 
     } 
    }); 
    dialogBuilder.setNegativeButton("Don't Call",new DialogInterface.OnClickListener(){ 
     @Override 
     public void onClick(DialogInterface dialog, int w) { 
      //Intent callIntent = new Intent(Intent.ACTION_CALL); 
      //callIntent.setData(Uri.parse("tel:" + phone)); 
      //getContext().startActivity(callIntent); 
     } 
    }); 
    AlertDialog dialog=dialogBuilder.create(); 
    //dialog.setTitle("Hey"); 
    dialog.show(); 


} 

}

這是爲我工作。但是問題在於,每次將項目添加到列表視圖中時,適配器將從開始位置開始打印,從而在應用程序中產生一個小滯後。我想要的是讓它開始從已添加的列表項目開始顯示,並且以前的項目保持原樣並且不受其影響。 我能做到這一點與notifyDataSetChanged以及在何處添加它,因爲我到處都試過,但它不工作。 有時也被我拿到error'the化內容的最適配器已經改變的,但是,列表視圖 - 沒 - 不接受-A-通知」。爲此,我在VenueSQL1活動中添加了線程。這是對的嗎?

回答

0

的滯後getView方法 裏面你的程序,你可以使用ViewHolder模式,只要你對你的ArrayList JST使用你適配器對象與notifyDataSetChanged() 添加新的數據將工作

+0

謝謝。你解決了我從開始就遇到的問題。 – shubhamgarg1

0

只需添加到數據結構中的新項目在您的情況是HashMap,每當增加新的項目。

而且不要忘記調用。 myadapter.notifysetdatachanged(); 它會反映你的列表視圖中的新數據。

+0

我還說到列表中。但我myadapter.notifyDataSetCahanged()甚至沒有被編譯器定義。它只能在使用this.notifydatasetcanged()的customadaptervenues類中應用。但我無法理解在哪裏應用它。 – shubhamgarg1

+0

您可以使用它在同一類,你定義適配器.. – logan

+0

我試過,但它是沒有用的。 – shubhamgarg1

相關問題