3

我在listview行中有buttontextview在一起。我想增加textview的值,同時點擊同一行中的按鈕。下面是我的代碼,在這裏它更新多行而不是單行,我需要。在列表視圖中增加按鈕單擊的textview值

package com.example.digitalmenuactivity; 

public class ListAdapter extends BaseAdapter { 

private Activity activity; 
private Activity activity1; 
private ArrayList<HashMap<String, String>> data; 
private static LayoutInflater inflater=null; 
public ImageLoader imageLoader; 

private int mCounter1=1; 
private int counter=1; 
private int[] counters; 
int pos; 





public ListAdapter(Activity a, ArrayList<HashMap<String, String>> d) { 
    activity = a; 
    data=d; 
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    imageLoader=new ImageLoader(activity.getApplicationContext()); 
    counters = new int[5]; 
} 


static class ViewHolder { 

    protected TextView mSwitcher1=null; 
    protected Button btnDelete=null; 
    protected TextView title=null; 
    protected TextView artist=null; 
    protected TextView duration=null; 
    protected ImageView thumb_image=null; 

} 



public int getCount() { 
    return data.size(); 
} 

public Object getItem(int position) { 
    return position; 
} 

public long getItemId(int position) { 
    return position; 
} 
@Override 
public int getItemViewType(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 



public View getView(final int position,final View convertView, ViewGroup parent) { 
    View vi=convertView; 
    final ViewHolder viewHolder; 
    pos = getItemViewType(position); 
    if(convertView==null) 
    { 
     vi = inflater.inflate(R.layout.list_row, null); 


     viewHolder = new ViewHolder(); 
     viewHolder.title = (TextView)vi.findViewById(R.id.title); 
     viewHolder.artist = (TextView)vi.findViewById(R.id.description); 
     viewHolder.duration = (TextView)vi.findViewById(R.id.price); 
     viewHolder.thumb_image =(ImageView)vi.findViewById(R.id.list_image); 

    viewHolder.btnDelete = (Button)vi.findViewById(R.id.plus); 
    viewHolder.mSwitcher1 = (TextView) vi.findViewById(R.id.switcher1); 




    vi.setTag(viewHolder); 

    } 
    else { 
     viewHolder = (ViewHolder) vi.getTag(); 
    } 
    viewHolder.btnDelete.setTag(pos); 
    viewHolder.mSwitcher1.setTag(pos); 



    viewHolder.btnDelete.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View view) { 


      pos = (Integer) view.getTag(); 


      int temp=counters[pos]; 
      temp++; 
      counters[pos]= temp; 

      viewHolder.mSwitcher1.setText(String.valueOf(counters[pos])); 
      notifyDataSetChanged(); 

      Log.d("^^^^^^", "button clicked" + counters.length); 
      Log.d("^^^^^^", "temp" + temp); 

     } 

     }); 
    HashMap<String, String> song = new HashMap<String, String>(); 
    song = data.get(position); 

    viewHolder.title.setText(song.get(FoodActivity.KEY_TITLE)); 
    viewHolder.artist.setText(song.get(FoodActivity.KEY_ARTIST)); 
    viewHolder.duration.setText(song.get(FoodActivity.KEY_DURATION)); 
    imageLoader.DisplayImage(song.get(FoodActivity.KEY_THUMB_URL),viewHolder.thumb_image); 
    return vi; 
} 

}

+0

嘿guyzz。 。問題解決了。 。感謝您的支持。問題出現在這行:notifyDataSetChanged()。我評論了這條線,現在它工作正常。 :-) – Vipin 2013-04-10 09:48:27

回答

1

你正在做短錯誤:如下:

首先

使用:

viewHolder.mSwitcher1.setTag(位置);

相反:

viewHolder.mSwitcher1.setTag(Integer.valueOf(位置));

沒有必要讓視圖標籤兩次,一次是足以讓精確選定的位置,適配器代碼將看起來象下面這樣:

ListviewAdapter:

import java.util.ArrayList; 
import android.content.Context; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.TextView; 

public class ListviewAdapter extends BaseAdapter { 
    ArrayList<String> getData = new ArrayList<String>(); 
    Context c; 
    private LayoutInflater mInflater; 
    int pos; 
    int boxState[]; 
    SharedPreferences prefs; 

    private int mCounter1 = 1; 
    private int counter = 1; 
    private int[] counters; 

    public ListviewAdapter(Context cont, ArrayList<String> data) { 
     // TODO Auto-generated constructor stub 
     c = cont; 
     getData = data; 
     mInflater = LayoutInflater.from(cont); 

     counters = new int[5]; 

    } 

    public int getCount() { 
     // TODO Auto-generated method stub 
     return getData.size(); 
    } 

    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    public class ViewHolder { 
     private TextView name = null; 
     private Button button = null; 

    } 

    @Override 
    public int getItemViewType(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public int getViewTypeCount() { 
     // TODO Auto-generated method stub 
     return getData.size(); 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     final ViewHolder holder; 
     View vi = convertView; 
     pos = getItemViewType(position); 

     if (convertView == null) { 

      vi = mInflater.inflate(R.layout.row, null); 

      holder = new ViewHolder(); 

      holder.name = (TextView) vi.findViewById(R.id.textView); 
      holder.button = (Button) vi.findViewById(R.id.checkBox); 

      vi.setTag(holder); 
     } 

     else { 
      holder = (ViewHolder) vi.getTag(); 
     } 

     holder.button.setTag(pos); 
     holder.name.setTag(pos); 

     holder.button.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 

       pos = (Integer) v.getTag(); 

       int temp = counters[pos]; 
       temp++; 
       counters[pos] = temp; 

       notifyDataSetChanged(); 
       holder.name.setText(String.valueOf(counters[pos])); 

       Log.d("^^^^^^", "button clicked" + counters.length); 
       Log.d("^^^^^^", "temp" + temp); 

      } 
     }); 

     return vi; 
    } 

} 
+0

我試了一下。 .still有相同的問題 – Vipin 2013-04-10 07:16:06

+0

不可能,它對我的​​工作,你是否得到任何錯誤? – RobinHood 2013-04-10 07:22:16

+0

我沒有收到任何錯誤,但反映在多行上的更新textview值 – Vipin 2013-04-10 07:28:06

1

而你錯誤地使用ViewHolder模式。閱讀原因:

http://www.jmanzano.es/blog/?p=166

+0

跟着鏈接並修改了代碼,仍然是相同的。 。格式化的代碼在我的問題上更新 – Vipin 2013-04-10 08:23:54

+0

是的,這不是解決您的問題。我認爲你應該有一個addidional數組,其大小等於要增加值的文本視圖的數量。在這個數組中,你應該保留將在textviews中顯示的值。當你想在textview中增加一個值時,你增加數組的一個元素。 – 2013-04-10 08:50:32

+0

嘿,問題解決了。 。感謝您的支持。問題出現在這行:notifyDataSetChanged()。我評論了這條線,現在它工作正常。 :-) – Vipin 2013-04-10 09:49:28