2017-08-17 41 views
0

我創建的應用程序只是在ListView中顯示一些數據,應使用適配器將此數據添加到Listview中。 在這個適配器中,我試圖設置一個項目在列表中被點擊時的權重。 我通過將權重設置爲1來獲得項目,但是當我嘗試將權重設置爲0時,沒有任何反應。Android設置BaseAdapter類中的佈局權重

在這部分代碼中,將佈局權重設置爲1的第一部分確實有效,但將其設置回0不起作用。

stat_item_container.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Log.w("HiscoreAdapter","Stat item height "+gained_tracker_container.getHeight()); 
      if(gained_tracker_container.getHeight() == 0){ 
       gained_tracker_container.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,1)); 
      }else{ 
       Log.w("HiscoreAdapter","Setting stat item weight to 0"); 
       gained_tracker_container.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,0)); 
      } 
     } 
    }); 

適配器類

public class HiscoreAdapter extends BaseAdapter { 
 

 
    private Context context; 
 
    private ArrayList<HiscoreItem> items; 
 

 
    public HiscoreAdapter(Context context, ArrayList<HiscoreItem> items){ 
 
     this.context = context; 
 
     this.items = items; 
 
    } 
 

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

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

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

 
    @Override 
 
    public View getView(int position, View convertView, ViewGroup parent) { 
 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
 
     View row = inflater.inflate(R.layout.stats_item, parent, false); 
 

 
     final LinearLayout stat_item_container = (LinearLayout) row.findViewById(R.id.stat_item_container); 
 
     final LinearLayout gained_tracker_container = (LinearLayout) row.findViewById(R.id.gained_tracker_container); 
 
     TypedArray images = row.getResources().obtainTypedArray(R.array.skill_icons); 
 
     ImageView stats_icon = (ImageView) row.findViewById(R.id.stats_icon); 
 
     TextView rank_textview = (TextView) row.findViewById(R.id.rank_textview); 
 
     TextView level_textview = (TextView) row.findViewById(R.id.level_textview); 
 
     TextView exp_textview = (TextView) row.findViewById(R.id.exp_textview); 
 
     TextView daily_levels_textview = (TextView) row.findViewById(R.id.daily_levels_textview); 
 
     TextView weekly_levels_textview = (TextView) row.findViewById(R.id.weekly_levels_textview); 
 
     TextView monthly_levels_textview = (TextView) row.findViewById(R.id.monthly_levels_textview); 
 
     TextView daily_exp_textview = (TextView) row.findViewById(R.id.daily_exp_textview); 
 
     TextView weekly_exp_textview = (TextView) row.findViewById(R.id.weekly_exp_textview); 
 
     TextView monthly_exp_textview = (TextView) row.findViewById(R.id.monthly_exp_textview); 
 

 
     Picasso.with(row.getContext()).load("http://www.runescape.com/img/rsp777/hiscores/skill_icon_attack1.gif").into(stats_icon); 
 
     //stats_icon.setBackgroundResource(images.getResourceId(position, defaultValue)); 
 
     rank_textview.setText(String.valueOf(items.get(position).getRank())); 
 
     level_textview.setText(String.valueOf(items.get(position).getLevel())); 
 
     exp_textview.setText(String.valueOf(items.get(position).getXp())); 
 
     daily_levels_textview.setText("+"+String.valueOf(items.get(position).getDayLevelGains())+" lvl"); 
 
     weekly_levels_textview.setText("+"+String.valueOf(items.get(position).getWeekLevelGains())+" lvl"); 
 
     monthly_levels_textview.setText("+"+String.valueOf(items.get(position).getMonthLevelGains())+" lvl"); 
 
     daily_exp_textview.setText("+"+String.valueOf(items.get(position).getDayGains())+" exp"); 
 
     weekly_exp_textview.setText("+"+String.valueOf(items.get(position).getWeekGains())+" exp"); 
 
     monthly_exp_textview.setText("+"+String.valueOf(items.get(position).getMonthGains())+" exp"); 
 

 
     stat_item_container.setOnClickListener(new View.OnClickListener() { 
 
      @Override 
 
      public void onClick(View v) { 
 
       Log.w("HiscoreAdapter","Stat item height "+gained_tracker_container.getHeight()); 
 
       if(gained_tracker_container.getHeight() == 0){ 
 
        gained_tracker_container.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,1)); 
 
       }else{ 
 
        Log.w("HiscoreAdapter","Setting stat item weight to 0"); 
 
        gained_tracker_container.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,0)); 
 
       } 
 
      } 
 
     }); 
 

 
     return row; 
 
    } 
 
}

+0

你的意思是你想刷新列表視圖? –

+0

@AminMemariani你認爲這樣可以解決嗎? –

+0

是的,我認爲是的! –

回答

0
ArrayListObject.clear(); 
    ArrayListObject.addAll(DBClass.listAll(DBClass.class)); 
    AdapterObject.notifyDataSetChanged(); 
    listViewObject.invalidateViews(); 
    listViewObject.refreshDrawableState(); 

試試這個代碼刷新的ListView。將它添加到onItemClickedListener中的return語句之前。

在第二行中,我用SugarORM調用了我的數據

+0

你可以看到這個代碼作爲一個例子:https://github.com/AminMemariani/ bMICalculator /斑點/主/應用程序/ SRC /主/ JAVA/COM/aminmemariani /應用/ bmicalculator/History.java –