2016-05-17 40 views
0

我想从适配器获取TextView值并对其进行更改。访问父GridView适配器内的活动获取TextView值并更改它

我有适配器内的按钮,当我按下该按钮时,我想从适配器内更改TextView值。那可能吗 ?

这里是我的适配器:

public class CustomGridView3 extends BaseAdapter { 
    private ArrayList<ListItem> listData = new ArrayList<ListItem>(); 
    private LayoutInflater layoutInflater; 
    private Context context; 
    private int count = 0; 
    CustomGridView3 adapter = this; 
    int hargax,qtyx,totalx; 
    public CustomGridView3(Context context, ArrayList<ListItem> listData) { 
     this.listData = listData; 
     layoutInflater = LayoutInflater.from(context); 
     this.context = context; 
    } 

    @Override 
    public int getCount() { 
     return listData.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return listData.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(final int position, View convertView, ViewGroup parent) { 
     final ViewHolder holder; 
     if (convertView == null) { 
      convertView = layoutInflater.inflate(R.layout.afterlogin_product_gridview, null); 
      holder = new ViewHolder(); 
      holder.headlineView = (TextView) convertView.findViewById(R.id.nama_produk); 
      holder.teaserView = (TextView) convertView.findViewById(R.id.harga); 
      holder.imageView = (ImageView) convertView.findViewById(R.id.img_produk); 
      holder.cmdMinus = (Button) convertView.findViewById(R.id.btn_min); 
      holder.cmdPlus = (Button) convertView.findViewById(R.id.btn_plus); 
      holder.qty = (TextView) convertView.findViewById(R.id.lbl_qty); 
      holder.layout1 = (LinearLayout) convertView.findViewById(R.id.layout1); 
      holder.harga = (TextView) convertView.findViewById(R.id.harga); 
      holder.satuan = (TextView) convertView.findViewById(R.id.satuan); 
      convertView.setTag(holder); 

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



     final ListItem newsItem = listData.get(position); 
     String satuan = newsItem.getSatuan().toString(); 
     String harga = newsItem.getReporterName().toString(); 
     harga = "Rp. " + harga + "/" + satuan; 
     holder.headlineView.setText(newsItem.getHeadline().toUpperCase()); 
     holder.teaserView.setText(harga); 

     SharedPreferences pref = context.getSharedPreferences("MyPref", 0); // 0 - for private mode 
     Integer qtys = Integer.parseInt(pref.getString(newsItem.getHeadline() + "_003", "0")); 
     newsItem.setQuantity(qtys); 
     holder.qty.setText(String.valueOf(newsItem.getQuantity())); 
     String a = newsItem.getUrl(); 

     holder.cmdMinus.setTag(position); 
     holder.cmdPlus.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       count = newsItem.getQuantity(); 
       count++; 
       newsItem.setQuantity(count); 
       holder.qty.setText("" + count); 
       SharedPreferences pref = context.getSharedPreferences("MyPref", 0); // 0 - for private mode 
       SharedPreferences.Editor editor = pref.edit(); 
       editor.putString(newsItem.getHeadline() + "_001", newsItem.getReporterName()); // harga 
       editor.putString(newsItem.getHeadline() + "_002", newsItem.getHeadline()); //nama 
       editor.putString(newsItem.getHeadline() + "_003", String.valueOf(count)); //quantity 
       editor.putString(newsItem.getHeadline() + "_004", newsItem.getSatuan()); //satuan 
       editor.putString(newsItem.getHeadline() + "_005", newsItem.getUrl()); //url 
       editor.commit(); 
      } 
     }); 

     holder.cmdMinus.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       count = newsItem.getQuantity(); 
       count--; 
        newsItem.setQuantity(count); 
        holder.qty.setText("" + count); 
        SharedPreferences pref = context.getSharedPreferences("MyPref", 0); // 0 - for private mode 
        SharedPreferences.Editor editor = pref.edit(); 
        editor.putString(newsItem.getHeadline() + "_001", newsItem.getReporterName()); // harga 
        editor.putString(newsItem.getHeadline() + "_002", newsItem.getHeadline()); //nama 
        editor.putString(newsItem.getHeadline() + "_003", String.valueOf(count)); //quantity 
        editor.putString(newsItem.getHeadline() + "_004", newsItem.getSatuan()); //satuan 
        editor.putString(newsItem.getHeadline() + "_005", newsItem.getUrl()); //url 
        editor.commit(); 

        if(count == 0) 
        { 
         SharedPreferences prefs = context.getSharedPreferences("MyPref", 0); // 0 - for private mode 
         SharedPreferences.Editor editors = prefs.edit(); 
         editors.remove(newsItem.getHeadline() + "_001"); 
         editors.remove(newsItem.getHeadline() + "_002"); 
         editors.remove(newsItem.getHeadline() + "_003"); 
         editors.remove(newsItem.getHeadline() + "_004"); 
         editors.remove(newsItem.getHeadline() + "_005"); 
         editors.commit(); 
         listData.remove(position); 
         adapter.notifyDataSetChanged(); 
        } 
      } 
     }); 

     holder.qty.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       hargax = Integer.parseInt(newsItem.getReporterName()); 
       qtyx = newsItem.getQuantity(); 
       hargax = hargax * qtyx; 
       if (context instanceof AfterLogin_Cart) 
       { 
        ((AfterLogin_Cart)context).getPrice(); 
       } 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 
      ***** Change TextView from Parent Activity ******* 
      } 
     }); 

     if (holder.imageView != null) { 
      //new ImageDownloaderTask(holder.imageView).execute(newsItem.getUrl()); 
      Picasso 
        .with(context) 
        .load(a) 
        .fit() 
        .noFade() 
        .into(holder.imageView); 
     } 

     return convertView; 
    } 

    static class ViewHolder { 
     TextView headlineView; 
     TextView teaserView; 
     ImageView imageView; 
     TextView qty; 
     Button cmdPlus,cmdMinus; 
     LinearLayout layout1; 
     TextView harga,satuan; 
    } 

从addTextChangeListener,我想改变家长的活动TextView的价值。我已经在寻找的只是执行某种方法。

if (context instanceof AfterLogin_Cart) 
{ 
    ((AfterLogin_Cart)context).getPrice(); 
} 
+1

您可以创建适配器作为一个内部类在你活动课,在那里你可以很容易地到达TextView的和改变它。 –

+0

您可以在您的适配器中将'TextWatcher'作为变量。然后,在创建适配器时,您从实现“TextWatcher”的活动传递此参数。最后,在适配器内部,您只需将所需的方法委托给'TextWatcher'实例 – x0r

+0

@EliranTutia我会试试 –

回答

0

好的,这里有一些示例代码来解释我上面的评论。所以,首先你在你的适配器类声明一个代理变量:

public class CustomGridView3 extends BaseAdapter { 
private ArrayList<ListItem> listData = new ArrayList<ListItem>(); 
private LayoutInflater layoutInflater; 
private Context context; 
private TextWatcher textWatcherDelegate; 
private int count = 0; 
CustomGridView3 adapter = this; 
int hargax,qtyx,totalx; 
public CustomGridView3(Context context, TextWatcher textWatcherDelegate, ArrayList<ListItem> listData) { 
    this.listData = listData; 
    this.textWatcherDelegate = textWatcherDelegate; 
    layoutInflater = LayoutInflater.from(context); 
    this.context = context; 
} 
当然

,你需要从活动传递(或任何你创建你的适配器):

CustomGridView3 adapter = new CustomGridView3(your_context, this, your_list) 

(在这里我假设您的来电者类实现TextWatcher所以我们可以使用this

,然后在您的适配器getView方法,你的委托方法调用:

holder.qty.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
      textWatcherDelegate.beforeTextChanged(s, start, count, after); 
     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      hargax = Integer.parseInt(newsItem.getReporterName()); 
      qtyx = newsItem.getQuantity(); 
      hargax = hargax * qtyx; 
      if (context instanceof AfterLogin_Cart) 
      { 
       ((AfterLogin_Cart)context).getPrice(); 
      } 

      textWatcherDelegate.onTextChanged(s, start, before, count); 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 
     ***** Change TextView from Parent Activity ******* 
      textWatcherDelegate.afterTextChanged(s); 
     } 
    }); 

因此,你可以处理TextWatcher的方法调用您的活动

相关问题