2016-12-16 100 views
1

我正在爲我的ListView使用自定義適配器,我已經不太瞭解自己想要的了。我可以在網上找到關於如何添加一行到ListView的信息,但它沒有很好地集成到我的代碼中,我猜測是因爲我使用了一個自定義適配器。我在我的主要活動中爲我的按鈕設置了我的setOnClickListener()方法,並且在那裏進行了實驗,但只是無法弄清楚,我也不確定我的方法是否在正確的位置?如何將行添加到listView按鈕在android studio中單擊?

這是在MainActivity

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

import static java.util.logging.Logger.global; 

public class MainActivity extends Activity { 

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

     final String[] Chores = {""}; 
     ListAdapter MyAdapter = new CustomAdapter(this, Chores); 
     ListView listViewObject = (ListView)findViewById(R.id.customListView_ID); 
     listViewObject.setAdapter(MyAdapter); 

     listViewObject.setOnItemClickListener(
      new AdapterView.OnItemClickListener(){ 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id){ 
        String ChoreString = String.valueOf(parent.getItemAtPosition(position)); 


       } 
      } 

     ); 

// this is where I am trying to have button click add another row to my listView  
     final Button button = (Button) findViewById(R.id.button_ID); 
     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       //this is where I am stuck 

      } 

     }); 

    } 
} 

這裏是我的CustomAdapter類

import android.content.Context; 
import android.support.annotation.NonNull; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.EditText; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.TextView; 


class CustomAdapter extends ArrayAdapter{ 

    public CustomAdapter(Context context, String[] choreText) { 
     super(context, R.layout.custon_listview_row, choreText); 
    } 

    @NonNull 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater myInflater = LayoutInflater.from(getContext()); 
     View customView = myInflater.inflate(R.layout.custon_listview_row, parent, false); 

     String singleListItem = (String) getItem(position); 
     EditText choreText = (EditText) customView.findViewById(R.id.editText_ID); 
     ImageButton imageButton = (ImageButton) customView.findViewById(R.id.imageButton_ID); 

     choreText.setText(singleListItem, TextView.BufferType.EDITABLE); 



     imageButton.setImageResource(R.drawable.clock); 

     return customView; 
    } 
} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.emilythacker.chorelist.MainActivity"> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/customListView_ID" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="50dp" /> 

    <Button 
     android:text="Add Chore" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" 
     android:id="@+id/button_ID" /> 
</RelativeLayout> 

和custom_listview_row.xml

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

    <ImageButton 
     android:layout_width="60dp" 
     android:scaleType="fitCenter" 
     app:srcCompat="@drawable/clock" 
     android:id="@+id/imageButton_ID" 
     android:layout_height="60dp" 
     android:background="@null" 
     android:layout_alignParentRight="true" 
     android:padding="5dp" 
     android:layout_weight="1" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:id="@+id/editText_ID" 
     android:layout_alignParentLeft="true" 
     android:alpha = ".5" 
     android:hint="Enter chore" 
     android:maxLines="1" 
     android:inputType="text" 
     /> 

</RelativeLayout> 
+0

請確保您不公佈姓名,密碼等! – 0X0nosugar

+0

對不起,第一篇文章不知道。 –

回答

1

首先讓你的列表進入一個ArrayList而不是字符串數組。

private ArrayList<String> arrayList; 

在按鈕的onclick

arrayList.add("New Item"); 
((ArrayAdapter)MyAdapter).notifyDataSetChanged(); 
1
 public void onClick(View v) { 

      String stringToAdd = ... // 
      MyAdapter.add(stringToAdd); 
     } 

您將需要添加final到適配器聲明這個工作:

 final ListAdapter MyAdapter = new CustomAdapter(this, Chores); 

編輯

要一次添加多行:

 public void onClick(View v) { 

      String[] rowsToAdd = ... // 
      MyAdapter.addAll(rowsToAdd); 
     } 
+1

修改適配器的後備數據後,不要忘記調用notifyDataSetChanged()。 –

+1

@AlexandruDascălu適配器擴展了'ArrayAdapter'; 'notifyDataSetChanged()'調用被烘焙到'add()'方法中,所以我們不必這樣做。雖然你可以用'setNotifyOnChange()'修改這個行爲,但是默認是調用它。見192行,https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/ArrayAdapter.java –

+0

那一刻,當我被源代碼搗毀。好點:) –

相關問題