#java #android #listview
Есть ListView. Он заполняется через адаптер. При нажатие на каждый элемент ListView появляется новая активити где выбираем ширину и фон кнопки, на которую нажали. Я передаю данные через синглтон в адаптер . Не получается изменить ширину элемента ListVIew, кнопка никак не реагирует на setWidth. Фон же через setBackgroundColor успешно изменяется. Подскажите как быть? list_item.xmlCustomAdapter.java public class CustomAdapter extends ArrayAdapter { private int myColor, myWidth; private String myItem; private final Context context; private final String[] values; public CustomAdapter(Context context, String[] values) { super(context, R.layout.list_item, values); this.context = context; this.values = values; } @Override public View getView(int position, View convertView, ViewGroup parent) { // return super.getView(position, convertView, parent); LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.list_item, parent, false); TextView textView = (TextView) rowView.findViewById(R.id.ColorTextButton); ImageView imageView = (ImageView) rowView.findViewById(R.id.imageViewIcon); TextView textView2= (TextView) rowView.findViewById(R.id.HelpButton); textView.setText(values[position]); myColor=DataProvider.getInstance().getColor(); myWidth=DataProvider.getInstance().getWeight(); myItem=DataProvider.getInstance().getItem(); String s = values[position]; System.out.println(s); if (s.equals("Sunday")) { imageView.setImageResource(R.drawable.arrow); if (s.equals(myItem)){ textView.setWidth(MyWidth); textView.setBackgroundColor(myColor); } } return rowView; } } ButtonListActivity.java public class ButtonListActivity extends ListActivity { TextView textView; int Proportion, BackColor; String[] DayOfWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main); // ListView myListView = (ListView)findViewById(R.id.listView); ListView listView = getListView(); listView.setTextFilterEnabled(true); CustomAdapter forData= new CustomAdapter(this,DayOfWeek); setListAdapter(new CustomAdapter(this,DayOfWeek)); } protected void onListItemClick(ListView l, View v, int position, long id) { String item = (String) getListAdapter().getItem(position); DataProvider.getInstance().putItem(item); Intent intent = new Intent(ButtonListActivity.this,SeekBarWork.class); startActivity(intent); // Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show(); } } SeekBarWork.java public class SeekBarWork extends ActionBarActivity implements SeekBar.OnSeekBarChangeListener { RelativeLayout myscreen; // SEEKBAR SeekBar rsb, gsb, bsb; // SEEKBAR int red, green, blue,backgroundColor,prop; //SEEKBAR Button gotoscreen1; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_choise_color); gotoscreen1=(Button)findViewById(R.id.ColorDone); myscreen = (RelativeLayout) findViewById(R.id.mylayout); //SEEKBAR rsb = (SeekBar) findViewById(R.id.RedSeekBar); //SEEKBAR gsb = (SeekBar) findViewById(R.id.GreenSeekBar);//SEEKBAR bsb = (SeekBar) findViewById(R.id.BlueSeekBar); //SEEKBAR rsb.setOnSeekBarChangeListener(this); //SEEKBAR gsb.setOnSeekBarChangeListener(this); //SEEKBAR bsb.setOnSeekBarChangeListener(this); //SEEKBAR textView =(TextView)findViewById(R.id.textView1); } public int updatebackground(){ //SEEKBAR int colorr; red = rsb.getProgress(); green = gsb.getProgress(); blue = bsb.getProgress(); colorr=0xff000000 + red * 0x10000 + green * 0x100 + blue; // myscreen.setBackgroundColor(colorr); return colorr; } public int max(){ int maxNum; if (red>=green) { if (red>=blue) maxNum = red; else maxNum=blue; } else if (green>=blue) maxNum=green; else maxNum=blue; if (maxNum==0) maxNum=1; return maxNum; } public int min(){ int minNum; if (red<=green) { if (red<=blue) minNum = red; else minNum=blue; } else if (green<=blue) minNum=green; else minNum=blue; if (minNum==0) minNum=1; return minNum; } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { backgroundColor=(updatebackground()); // str=Integer.toString(s); // gotoscreen1.setText(str); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } public void onEnterButton(View view) { Intent intent = new Intent(SeekBarWork.this,ButtonListActivity.class); prop=(int)(((min()*100)/max())); DataProvider.getInstance().setData(prop,backgroundColor); startActivity(intent); } }
Ответы
Ответ 1
Попробуйте вместо textView.setWidth(MyWidth); использовать LayoutParams: RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)textView.getLayoutParams(); params.width = MyWidth; textView.setLayoutParams(params);
Комментариев нет:
Отправить комментарий