2014-03-26 206 views
0

在我的應用程序中,有一個列表視圖,其中包含一些數據。在同一活動中有一個edittext和一個按鈕。當我在edittext中輸入某些內容時,我想將該文本添加到按鈕單擊的ListView上。如何做到這一點?請幫助...如何將項目添加到按鈕單擊列表視圖?

這裏是我的代碼示例...

這是我customList適配器

public class MainActivity extends Activity 
{ 
    ListView list; 

    String[] web = { 
    "Google Plus", 
     "Twitter", 
     "Windows", 
     "Bing", 
     "Itunes", 
     "Wordpress", 
     "Drupal" 
} ; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     EditText et = (EditText)findViewById(R.id.editText1); 
    Button b=(Button)findViewById(R.id.button1); 
    b.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
        Toast.makeText(MainActivity.this, "Button clicked...", Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    CustomList adapter = new CustomList(MainActivity.this, web); 
    list=(ListView)findViewById(R.id.list); 
    list.setAdapter(adapter); 
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
              int position, long id) { 
      Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show(); 

     } 
    }); 

這是我的適配器類

public class CustomList extends ArrayAdapter<String>{ 

private final Activity context; 
private final String[] web; 

public CustomList(Activity context, String[] web) { 
    super(context, R.layout.list_single, web); 
    this.context = context; 
    this.web = web; 

} 

@Override 
public View getView(int position, View view, ViewGroup parent) { 
    LayoutInflater inflater = context.getLayoutInflater(); 
    View rowView= inflater.inflate(R.layout.list_single, null, true); 
    TextView txtTitle = (TextView) rowView.findViewById(R.id.txt); 


    txtTitle.setText(web[position]); 


    return rowView; 
} 
} 
+2

獲取文本。將它添加到數組並在適配器上調用notifyDataSetChanged。您也可以使用ArrayList – Raghunandan

回答

4

聲明

final ArrayList<String> dataset = new ArrayList<String>(); 
Button b=(Button)findViewById(R.id.button1); 
list=(ListView)findViewById(R.id.list); 
list.setAdapter(adapter); 
final CustomList adapter = new CustomList(MainActivity.this, dataset, imageId); 
b.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View arg0) 
{ 
    dataset.add(et.getText().toString()); 
    adapter.notifyDataSetChanged(); 
} 
}); 

然後

private ArrayList<String> web; 
public CustomList(Activity context, ArrayList<String> web, Integer[] imageId) { 
super(context, R.layout.list_single, web); 

而且在getView

txtTitle.setText(web.get(position)); 

編輯:

public class MainActivity extends Activity 
{ 
EditText ed; 
CustomList adapter; 
ArrayList<String> list = new ArrayList<String>(); 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Button b = (Button) this.findViewById(R.id.button1); 
    ListView lv = (ListView) this.findViewById(R.id.listView1); 
    ed= (EditText) this.findViewById(R.id.editText1); 
    adapter = new CustomList(this,list); 
    lv.setAdapter(adapter); 
    b.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      list.add(ed.getText().toString()); 
      adapter.notifyDataSetChanged(); 
      } 

    }); 

} 
} 

activity_main.xml中;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="16dp" 
     android:ems="10" > 

     <requestFocus /> 
    </EditText> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:text="Button" /> 

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@id/button1" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/editText1" > 
    </ListView> 

</RelativeLayout> 

CustomList

public class CustomList extends ArrayAdapter<String>{ 

private ArrayList<String >web; 
LayoutInflater mInflater; 
public CustomList(Context context, ArrayList<String> web) { 
    super(context, 0, web); 
    mInflater= LayoutInflater.from(context); 
    this.web = web; 

} 
@Override 
public View getView(int position, View view, ViewGroup parent) { 

    View rowView= mInflater.inflate(R.layout.list_item,parent,false); 
    TextView txtTitle = (TextView) rowView.findViewById(R.id.textView1); 
    txtTitle.setText(web.get(position)); 
    return rowView; 
} 
} 

LIST_ITEM

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="26dp" 
     android:text="TextView" /> 

</RelativeLayout> 

enter image description here

+0

需要添加圖片資源另外還要拋出IndexOutOfBounds Exception .. –

+0

@kalyanpvs其實是一個模型類會更好。要編輯它 – Raghunandan

+0

,也許你也可以糾正op的getView,以避免雙重內存分配。 – Blackbelt

0

的ArrayList /列表/陣列添加項目,然後使用通知列表:

adapterObject.notifyDataSetChanged() 
+0

如何將該項添加到我的列表中?可以向我展示示例代碼嗎? – Nevaeh

0

你可以試試這個

ArrayList<String> web=new ArrayList<String>(); 
    b.setOnClickListener(new View.onClickListener(){ 
     @Override 
     public void onClick(View arg0) 
     { 
      web.add(et.getText().toString()); 
      adapter.notifyDataSetChanged(); 
     } 
    }); 
0

更新web和ImageId後。 您必須在適配器上調用notifyDataSetChanged。

試試這個代碼的變化:從EDITTEXT上的按鈕進行點擊

EditText et = (EditText)findViewById(R.id.editText1); 


      Button b=(Button)findViewById(R.id.button1); 
    CustomList adapter = new CustomList(MainActivity.this, web, imageId); 
      b.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View arg0) 
       { 
    //update the data on the web and imageID 
        Toast.makeText(MainActivity.this, "Button clicked...", 
    Toast.LENGTH_SHORT).show(); 
    //call the notifydatasetChanged on the adapter 
    adapter.notifyDataSetChanged(); 
        } 
       }); 


     list=(ListView)findViewById(R.id.list); 
     list.setAdapter(adapter); 
     list.setOnItemClickListener(new AdapterView.OnItemClickListener() 
     { 

      @Override 
        public void onItemClick(AdapterView<?> parent, View view, 
              int position, long id) { 
         Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show(); 

        } 
       }); 
+0

從哪裏可以獲得editText中的文字? – Nevaeh

+0

這也是onclick事件的按鈕,但在調用notifyDAtaSetChanged方法之前。 @Nevaeh –

相關問題