I try to create a list that includes buttons / button. But every time there is a little vacation.

It should look like:

Expectation

It actually looks like:

Reality

Markup:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <Button android:id="@+id/Text1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="+" android:layout_marginRight="10dp"/> <Button android:id="@+id/Text2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="-" android:layout_marginLeft="10dp"/> </LinearLayout> 

Activation code:

 import android.app.*; import android.os.*; import android.widget.*; import java.util.*; public class GameActivity extends Activity { ArrayList<Choose> products = new ArrayList(); ListView ChooseList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.games); if(products.size()==0){ products.add(new Choose("", "")); products.add(new Choose("", "")); products.add(new Choose("", "")); products.add(new Choose("", "")); products.add(new Choose("", "")); } ChooseList = (ListView) findViewById(R.id.Choose); ChooseAdapter adapter = new ChooseAdapter(this, R.layout.game, products); ChooseList.setAdapter(adapter); } } 

Object class:

 import android.widget.*; public class Choose { private String Text_1, Text_2; TextView names; public String choose_1="Some Text",choose_2="Some Text"; Choose(String Text_1, String Text_2){ this.Text_1 = choose_1; this.Text_2 = choose_2; } public String Text_1Set(){ return Text_1; } public String Text_2Set(){ return Text_2; } } 

Adapter:

 import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.TextView; import java.util.ArrayList; class ChooseAdapter extends ArrayAdapter<Choose> { private LayoutInflater inflater; private int layout; private ArrayList<Choose> ChooseList; ChooseAdapter(Context context, int resource, ArrayList<Choose> products) { super(context, resource, products); this.ChooseList = products; this.layout = resource; this.inflater = LayoutInflater.from(context); } public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder viewHolder; if(convertView==null){ convertView = inflater.inflate(this.layout, parent, false); viewHolder = new ViewHolder(convertView); convertView.setTag(viewHolder); } else{ viewHolder = (ViewHolder) convertView.getTag(); } final Choose product = ChooseList.get(position); viewHolder.first_t.setText(product.Text_1Set()); viewHolder.second_t.setText(product.Text_2Set()); viewHolder.first.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { viewHolder.first_t.setText("-"); } }); viewHolder.second.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { viewHolder.second_t.setText("+"); } }); return convertView; } private class ViewHolder { final Button first, second; final TextView first_t, second_t; ViewHolder(View view){ first = (Button) view.findViewById(R.id.Text1); second = (Button) view.findViewById(R.id.Text2); first_t = (TextView) view.findViewById(R.id.Text1); second_t = (TextView) view.findViewById(R.id.Text2); } } } 

Question : how can you realize that there are two (one) buttons (s) that will be stretched over the entire surface?

  • In this code there are no problems that could lead to the result in the second screenshot. Either the problem is in another place, or the code in the question does not correspond to the code in the project, or it is some kind of glitch, for example, during the assembly, the changes made are not taken into account. But you have a problem, that changes in items during clicks are not saved anywhere, when scrolling, these changes will be lost. For 5 items in the list it does not matter, but if the list is long enough to scroll, then the problem will need to be solved. - pavlofff
  • @pavlofff thanks!) And how can I save, I just think to make a rather large list? - EvgeniyDodg
  • In your case, it will probably be the simplest thing to do in the model is a setter and write there a new value along with changing the contents of the widget when processing a click - pavlofff

0