2014-05-01 3 views
1

ListView와 TextView로 구성된 레이아웃이 있습니다.CustomListViewAdapter - 버튼을 클릭하여 ListView 외부 TextView에 영향을줍니다.

ListView 항목에는 Button이 하나 있습니다. Button의 onClickListener에서 부모 레이아웃에있는 TextView의 텍스트를 변경할 수 있습니까?

나는 버튼의 OnClickListener를 설정했지만 버튼은 텍스트 뷰 속성 등

감사를 변경할 수있는 방법을 찾을 수 없어! :)

레이아웃 : enter image description here

+0

당신은 당신의 코드를 게시하시기 바랍니다 수 있습니까? –

+0

@Haresh 코드는 아래의 하단 코드와 비슷합니다. – user2301818

답변

0
// try this way,hope this will help you.. 

**XML** code 

**activity** 
<?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="match_parent" 
    android:padding="5dp" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/txtChnageTextColor" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="18sp" 
     android:text="Android Demo Text For TextView"/> 


    <ListView 
     android:id="@+id/lstChangeTextColor" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_marginTop="5dp" 
     android:layout_weight="1"/> 

</LinearLayout> 

**list_item** 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"> 

    <TextView 
     android:id="@+id/txtColor" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:textSize="16sp"/> 

    <Button 
     android:id="@+id/btnSet" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Set"/> 

</LinearLayout> 

**ACTIVITY** code 

**MyActivity** 
public class MyActivity extends Activity{ 

    private ListView lstChangeTextColor; 
    private TextView txtChnageTextColor; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity); 
     lstChangeTextColor = (ListView) findViewById(R.id.lstChangeTextColor); 
     txtChnageTextColor = (TextView) findViewById(R.id.txtChnageTextColor); 
     ArrayList<HashMap<String,String>> listItem = new ArrayList<HashMap<String,String>>(); 
     HashMap<String,String> map1 = new HashMap<String, String>(); 
     map1.put("name", "Red"); 
     map1.put("value", "#FF0000"); 
     HashMap<String,String> map2 = new HashMap<String, String>(); 
     map2.put("name", "Orange"); 
     map2.put("value", "#FFA500"); 
     HashMap<String,String> map3 = new HashMap<String, String>(); 
     map3.put("name", "Yellow"); 
     map3.put("value", "#FFFF00"); 
     HashMap<String,String> map4 = new HashMap<String, String>(); 
     map4.put("name", "Lime"); 
     map4.put("value", "#00FF00"); 
     HashMap<String,String> map5 = new HashMap<String, String>(); 
     map5.put("name", "Blue"); 
     map5.put("value", "#0000FF"); 
     listItem.add(map1); 
     listItem.add(map2); 
     listItem.add(map3); 
     listItem.add(map4); 
     listItem.add(map5); 
     lstChangeTextColor.setAdapter(new ListAdapter(this,listItem)); 

    } 

    class ListAdapter extends BaseAdapter{ 

     private ArrayList<HashMap<String,String>> listItem; 
     private Context context; 
     public ListAdapter (Context context,ArrayList<HashMap<String,String>> listItem) { 
      this.listItem = listItem; 
      this.context = context; 
     } 

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

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

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

     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      ViewHolder holder; 
      if(convertView==null) { 
       holder = new ViewHolder(); 
       convertView = LayoutInflater.from(context).inflate(R.layout.list_item,null,false); 
       holder.txtColor = (TextView) convertView.findViewById(R.id.txtColor); 
       holder.btnSet = (Button) convertView.findViewById(R.id.btnSet); 
       convertView.setTag(holder); 
      }else{ 
       holder = (ViewHolder) convertView.getTag(); 
      } 
      holder.txtColor.setText(listItem.get(position).get("name")); 
      holder.btnSet.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          txtChnageTextColor.setTextColor(Color.parseColor(listItem.get(position).get("value"))); 
         } 
        }); 
       } 
      }); 
      return convertView; 
     } 
    } 

    class ViewHolder{ 
     Button btnSet; 
     TextView txtColor; 
    } 

} 
+0

'txtChnageTextColor'는'BaseAdapter'에서 전혀 선언되지 않았고'BaseAdapter'는 다른 파일에 있습니다. 이 방법으로는 올바르게 작동하지 않습니까? 'BaseAdapter' 파일에서'findViewById'를 사용하지 않으면? – user2301818

+0

@ user2301818 님, 안녕하세요. 여기서는 어댑터를 내부 클래스로 선언하므로 별도의 클래스로 어댑터를 선언하면 "txtChnageTextColor"범위에 쉽게 액세스 할 수 있습니다. 그러면 어댑터 클래스에 "txtChnageTextColor"참조를 전달해야합니다. –

관련 문제