0

我有一個適配器添加數據到按鈕點擊列表視圖的問題。listview項沒有添加在按鈕上點擊

我有一個名爲createteam的活動,它具有名稱和電子郵件edittext和一個createplayer按鈕。點擊createplayer按鈕後,名稱和電子郵件應添加到列表視圖中。

我想用一個適配器來做到這一點(這樣我就可以學習)。

更新: Onclicking的按鈕中的第一項被添加,但是當我在 edittexts填補再次,列表視圖也得到更新。我通知適配器當我更新 數據,但只有第一項獲得並保持添加到列表視圖

create_team.xml

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


    <EditText 
    android:id="@+id/teamname" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textColor="@color/blue" 
    android:hint="Team Name" 
    android:gravity="center" 
    android:textStyle="bold"> 

    </EditText> 



<Button 
    android:id="@+id/addplayer" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="@color/blue" 
    android:text="Add Player" 
    android:layout_gravity="center" 
    android:textStyle="bold"> 

    </Button> 



<LinearLayout 
    android:id="@+id/view_addplayer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
> 
<Button 
    android:id="@+id/createplayer" 
     android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="@color/red" 
    android:text="Create Player" 
    android:layout_gravity="center" 
    android:textStyle="bold"> 

    </Button> 
    <EditText 
     android:id="@+id/name" 
     android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textColor="@color/blue" 
    android:hint="Name" 
    android:gravity="center" 
    android:textStyle="bold"> 

    </EditText> 
     <EditText 
    android:id="@+id/email" 
    android:layout_marginTop="10dp" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
     android:textColor="@color/blue" 

    android:hint="Email " 
    android:textStyle="bold" 

    android:gravity="center"> 

    </EditText> 



    </LinearLayout> 

     <ListView 
      android:id="@+id/android:list" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      </ListView> 


</LinearLayout> 
</ScrollView> 

add_player_listview.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="100dp" 
    android:orientation="horizontal" 
    android:weightSum="2"> 



<TextView android:id="@+id/name" 
android:textSize="20sp" 
android:textStyle="bold" 
android:color="@color/blue" 
android:textColor="#FFFF00" 
android:layout_width="wrap_content" 
android:layout_height="50dp"/> 

<TextView android:id="@+id/email" 
android:textSize="15sp" 
android:textStyle="bold" 
android:color="@color/mustard" 
android:layout_width="wrap_content" 
android:layout_height="50dp"/> 
</LinearLayout> 

CreateTeamActivity .java

package com.recscores.android; 

import java.util.ArrayList; 
import java.util.HashMap; 

import android.app.ListActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.ListView; 
import android.widget.TableLayout; 
import android.widget.Toast; 
import com.recscores.android.PlayerInfo; 

public class CreateTeamActivity extends ListActivity { 
    Button createPlayer; 
    EditText Email; 
    EditText Name; 
    private ArrayList<PlayerInfo> newList; 
    private PlayerAddAdapter newAdpt; 
    private int i = 0; 
    private ListView lv; 


    protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.create_team); 

    newList = new ArrayList<PlayerInfo>(); 

    // CREATE TEAM 
    getWindow().setBackgroundDrawableResource(R.drawable.app_background2); 

    createPlayer = (Button)findViewById(R.id.createplayer); 


    Email = (EditText)findViewById(R.id.email); 
    Name = (EditText)findViewById(R.id.name); 

    lv = (ListView)findViewById(android.R.id.list); 
    newAdpt = new PlayerAddAdapter(CreateTeamActivity.this,android.R.id.list,newList); 
    lv.setAdapter(newAdpt); 

    createPlayer.setOnClickListener(new OnClickListener() { 


     public void onClick(View v) { 
     // newList = new ArrayList<PlayerInfo>(); 
      PlayerInfo info = new PlayerInfo(); 
      info.SetName(Name.getText().toString()); 
      info.SetEmail(Email.getText().toString()); 
      newList.add(info);   
      newAdpt.notifyDataSetChanged(); 
      //Thread.sleep(2000); 

      Name.setText(""); 
      Email.setText(""); 


      } 
      }); 


    } 


} 

PlayerAddAdapter.java

package com.recscores.android; 

import java.util.ArrayList; 

import android.app.Activity; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 


public class PlayerAddAdapter extends ArrayAdapter<PlayerInfo> { 
    private Activity activity; 
    //private final Context mContext; 
    private ArrayList<PlayerInfo> items; 


    public PlayerAddAdapter(Activity a, int textViewResourceId, ArrayList<PlayerInfo> items){ 
    super(a,textViewResourceId,items); 
    this.items=items; 
    this.activity = a; 
    } 

    public static class ViewHolder{ 
     public TextView name; 
     public TextView email; 
    } 
    @Override 


    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     ViewHolder holder; 
     if (v == null) { 
      LayoutInflater vi = 
       (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       vi.inflate(R.layout.add_player_listview, null); 
      holder = new ViewHolder(); 
      holder.name = (TextView)v.findViewById(R.id.name); 
      holder.email = (TextView)v.findViewById(R.id.email); 

      v.setTag(holder); 
     } 
     else 
      holder=(ViewHolder)v.getTag(); 

     final PlayerInfo custom = items.get(position); 
     if (custom != null) { 
      **holder.name.setText(custom.GetName()); 
      holder.email.setText(custom.GetEmail()); 
     } 
     return v; 
    } 

} 

PlayerInfo.java

package com.recscores.android; 

public class PlayerInfo { 

    private String name=""; 
    private String email=""; 

     public void SetName(String name){ 
     this.name = name; 
     } 
     public String GetName(){ 
     return this.name; 
     } 

     public void SetEmail(String email){ 
     this.email = email; 
     } 
     public String GetEmail(){ 
     return this.email; 
     } 
} 
+0

你不是在膨脹'View v'變量。你需要使用'v = vi.inflate(...);' – Sajmon

回答

4
LayoutInflater vi =(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
v = vi.inflate(R.layout.add_player_listview, null); 

更改您這樣的代碼。您不是將值設置爲v變量。

+0

Rajesh CP,非常感謝你指出這個問題。現在Null指針訪問錯誤消失了。但是代碼仍然沒有做任何事情。按鈕點擊沒有任何反應。請幫忙。 –

+0

onClick應該發生什麼?它會被添加到列表底部檢查最後一個項目 – Triode

+0

列表視圖保持空白...沒有添加行 –

0

onClick您的button致電list.notifyDataSetChanged()。它會刷新你的列表。

如果不起作用,只需檢查適配器的getCount()它返回的值。如果它正在返回0即使您致電list.notifyDataSetChanged(),您的列表也不會顯示任何數據。

在您的adapter中製作一個arrayList並在其中添加新項目。並從getCount()返回大小arrayList

+0

Ankit:謝謝你的幫助..我取得了一些進展。現在第一個條目被添加了,但是當我嘗試添加另一個條目時沒有任何反應。請給我演示如何編寫getCount()方法?順便說一句,我正在刷新適配器已newAdpt.notifyDataSetChanged(); Regards –

+0

你可以發佈你的getCount方法嗎? – Ankit

+0

我已經更新了我的答案,看看是否有幫助。 – Ankit