How do you change one GridView element?



  • How is this example, a GridView cell to change its contents?

    e.g. Чили(выбран)

    http://metanit.com/java/android/5.5.php

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"> 
    
    &lt;GridView
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="100dp"
    android:numColumns="2"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center" /&gt;
    

    </LinearLayout>

    MainActivity:

    public class MainActivity extends ActionBarActivity{

    String[] countries = { "Бразилия", "Аргентина", "Чили", "Колумбия", "Уругвай", "Парагвай"};
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.activity_main);
    
        // получаем элемент ListView
        GridView countriesList = (GridView) findViewById(R.id.gridview);
    
        // создаем адаптер
        ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, countries);
        countriesList.setAdapter(adapter);
    
        OnItemClickListener itemListener = new OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView&lt;?&gt; parent, View v,
                                int position, long id) {
                Toast.makeText(getApplicationContext(),"Вы выбрали "
                            + parent.getItemAtPosition(position).toString(),
                    Toast.LENGTH_SHORT).show();
            }
        };
        countriesList.setOnItemClickListener(itemListener);
    }
    

    }



  • There's a view that working directly with the String list because of their immutability is not very right, maybe we should turn them into the model. Then it'll work like this. https://stackoverflow.com/a/30711959/5892568

    public void onItemClick(AdapterView<?> parent, View view, int position, long id){
    
            String beforeName = itemListAdapter.getItem(position).getItemName().toString();
    
            String changedName = beforeName.concat("(chosen)");
            itemListAdapter.getItem(position).setItemName(changedName);
            itemListAdapter.notifyDataSetChanged();
    
        }
    

    For GridView, we must also change the list itself:

    public void onItemClick(AdapterView<?> parent, View view, int position, long id){

            String beforeName = itemListAdapter.getItem(position);
    
            String changedName = beforeName..getItemName().toString().concat("(chosen)");
    
            itemListAdapter.remove(beforeName);
            itemListAdapter.insert(new Item(changedName), position);
    
            itemListAdapter.notifyDataSetChanged();
    
        } 
    


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2