2015-12-15 84 views
0

這是我onItemClickListener方法無法更改的TextView,ListView中

//list item listener 
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
@Override 
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
     LinearLayout parent = (LinearLayout) view; 
     TextView t = (TextView) parent.findViewById(R.id.grocery_item_name); 

     t.setText("Something"); 
} 

編輯:這是適配器類

代碼來調用適配器:

adapter = new CustomList(this,R.layout.listitems_improved,Items); 

listview.setAdapter(adapter); 

private class CustomList extends ArrayAdapter{ 

     ArrayList<Item> items; 
     Activity myActivity; 
     LayoutInflater layoutinflater; 
     View myview; 

     public ProductsCustomList(Context context, int resource, ArrayList<item> i) { 
      super(context, resource, p); 
      this.items = i; 
      this.myActivity = (Activity) context; 
     } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
      //Inflate the custom row layout in the view 
      layoutinflater = myActivity.getLayoutInflater(); 
      myview = layoutinflater.inflate(R.layout.groceryitems_custom_listview, null); 

      //instantiate the views in custom row layout 
      TextView tv = (TextView) myview.findViewById(R.id.tv); 

      tv.setText(items.get(position).getItemName()); 


      return myview; 
     } 
} 

當我打電話給這條線t.setText("Something"); textview不會在ListView中更改,沒有任何反應。爲什麼它不在這裏?

+0

打印裏面是什麼' items.get(position).getItemName()',可能getItemName爲空 – diogojme

+0

't.setText(「Something」);'use' yourAdapter.notifyDataSetChanged();' – diogojme

+0

@diogojme文本已經在ListView中設置了,我想要改變e文本一旦被設置,'getItemName()'方法沒有什麼錯誤 – yousafkh

回答

1

你可以做這樣的事情:

final Adapter yourAdapter = new Adapter(context, items); 

listview.setAdapter(yourAdapter); 

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
@Override 
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){ 

    //change value for item select 
    items.get(i).setItemName("yourText"); 

    yourAdapter.notifyDataSetChanged(); 
    //if the this line don't refresh the list, you can recreate adapter: 
    //listview.setAdapter(new Adapter(context, items); 

} 

問候。

+0

這樣做的技巧,但它是不可行的,當列表是巨大的,其次它也改變了對象的值,我只想改變textview,同時保留原始值在'項目' – yousafkh

0

您也可以嘗試以包括適配器監聽器:在您的適配器類

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
     //Inflate the custom row layout in the view 
     layoutinflater = myActivity.getLayoutInflater(); 
     myview = layoutinflater.inflate(R.layout.groceryitems_custom_listview, null); 

     //instantiate the views in custom row layout 
     TextView tv = (TextView) myview.findViewById(R.id.tv); 

     tv.setText(items.get(position).getItemName()); 


     tv.setOnClickListener(new View.OnClickListener(){ 
       @Override 
       public void onClick(View arg0){     
        tv.setText("selected");     
       }    
     }); 


     return myview; 
    } 
1

請試試這個在您的getView()方法:

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
      //Inflate the custom row layout in the view 
      layoutinflater = myActivity.getLayoutInflater(); 
      myview = layoutinflater.inflate(R.layout.groceryitems_custom_listview, null); 

      //instantiate the views in custom row layout 
      TextView tv = (TextView) myview.findViewById(R.id.tv); 
      tv.setText(items.get(position).getItemName()); 


      myview.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       TextView view = (TextView) v.findViewById(R.id.your_text_id); 
       view.setText("text_changed"); 
      } 
     }); 


      return myview; 
     } 
0
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
@Override 
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
     LinearLayout parent = (LinearLayout) view; 
     TextView t = (TextView) parent.getChilldId(i).findViewById(R.id.grocery_item_name); 

     t.setText("Something"); 
} 
相關問題