2016-04-15 56 views
0

我正在嘗試進行一項活動,提供緊急電話號碼列表,以便用戶添加自己的自定義條目並保存它們。出於某種原因,ListView不會出現在活動中。我很確定我在CustomAdapter類中做了一些錯誤的事情,我在ListView的每個段中都保存了兩個文本框。ListView not appear

我也在嘗試設置listView以開始一個電話活動與任何段被觸摸的電話號碼,我不確定我是否正確地做到了這一點。

PhoneList.java:

public class PhoneList extends AppCompatActivity { 

    ArrayList<String> customList; 
    ArrayList<String> numList; 
    Bundle b; 
    TinyDB tinydb; 
    CustomAdapter dataAdapter; 
    ListView phoneListView; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_phone_list); 
     phoneListView = (ListView)findViewById(R.id.listViewPhone); 
     Integer num = null; 
     String label = ""; 
     customList = null; 
     numList = null; 
     tinydb = new TinyDB(this); 
     Button saveContact = (Button)(findViewById(R.id.button4)); 

     b = new Bundle(); 


     customList = tinydb.getListString("label"); 
     ArrayList<Integer> temp = tinydb.getListInt("num"); 
     for(int i = 0; i < temp.size(); i++){ 
      numList.add(temp.get(i).toString()); 
     } 


     saveContact.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       EditText enteredLabel = (EditText)findViewById(R.id.editText2); 
       EditText enteredNum = (EditText)findViewById(R.id.editText); 
       String label = enteredLabel.getText().toString(); 
       Integer num = Integer.parseInt(enteredNum.getText().toString()); 
       b.putString("label",label); 
       b.putInt("num",num); 
       addPhoneItem(); 


      } 
     }); 


     ArrayList<String> phoneList = new ArrayList<>(); 
     phoneList.add("Emergencies"); 
     phoneList.add("Travel Info "); 
     phoneList.add("Poison Control "); 
     phoneList.add("AAA: 1(800)836-2582"); 
     if(customList != null && customList.size() > 0) phoneList.addAll(customList); 

     ArrayList<String> numberList = new ArrayList<>(); 
     numberList.add("911"); 
     numberList.add("511"); 
     numberList.add("18002221222"); 
     numberList.add("18008362582"); 
     if(numList != null && numList.size()>0) { 
      for (int i = 0; i < numList.size(); i++) { 
       numberList.add(numList.get(i).toString()); 
      } 
     } 



     dataAdapter = new CustomAdapter(this,numberList,phoneList); 
     phoneListView.setAdapter(dataAdapter); 


    } 
    public void addPhoneItem(){ 
     customList.add(b.getString("label")); 
     numList.add(b.getString("num")); 
     tinydb.putListString("label",customList); 
     tinydb.putListString("num",numList); 
     dataAdapter = new CustomAdapter(this,numList,customList); 
     phoneListView.setAdapter(dataAdapter); 

    } 
    private class CustomAdapter extends ArrayAdapter<String> { 
     ArrayList<String> labels; 
     ArrayList<String> nums; 
     Context context; 

     public CustomAdapter(Context context, ArrayList<String> n,ArrayList<String> l) { 
      super(context,R.layout.phone_item); 
      this.context = context; 
      labels = new ArrayList<String>(); 
      labels.addAll(l); 
      nums = new ArrayList<String>(); 
      nums.addAll(n); 
     } 

     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      try { 
       ViewHolder holder = null; 
       if (convertView == null) { 
        LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        convertView = vi.inflate(R.layout.phone_item, null); 
        holder = new ViewHolder(); 
        holder.viewLabel = (TextView) convertView.findViewById(R.id.editText); 
        holder.viewNumber = (TextView) convertView.findViewById(R.id.editText2); 
        convertView.setTag(holder); 


       } else { 
        holder = (ViewHolder) convertView.getTag(); 
       } 

       String label = labels.get(position); 
       String num = nums.get(position); 
       holder.viewLabel.setText(label); 
       holder.viewNumber.setText(num); 
       holder.viewNumber.setTag(num); 
       holder.viewNumber.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         String str = "tel:" + (EditText) v.getTag(); 
         Intent intent = new Intent(Intent.ACTION_CALL); 
         intent.setData(Uri.parse(str)); 
         try { 
          startActivity(intent); 
         } catch (Exception e) { 
          Toast.makeText(getContext(),"Exception Caught in CustomAdapter",Toast.LENGTH_SHORT).show(); 
         } 
        } 
       }); 



      } 
      catch(Exception e){ 
       Toast.makeText(getContext(),"Exception caught",Toast.LENGTH_SHORT).show(); 

      } 
      return convertView; 

     } 

    } 
    private class ViewHolder{ 
     TextView viewLabel; 
     TextView viewNumber; 
    } 
} 

activity_phone_list.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: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.rvtripapp.dempsey.rvtripapp.PhoneList"> 

<ListView 
    android:layout_width="wrap_content" 
    android:layout_height="400dp" 
    android:id="@+id/listViewPhone" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" /> 

<EditText 
    android:layout_width="200dp" 
    android:layout_height="wrap_content" 
    android:id="@+id/editText" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:editable="true" 
    android:hint="Label" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:inputType="number" 
    android:ems="10" 
    android:id="@+id/editText2" 
    android:layout_below="@+id/editText" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:hint="Phone Number" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="+" 
    android:id="@+id/button4" 
    android:textSize="40dp" 
    android:background="#44fe66" 
    android:layout_alignBottom="@+id/editText2" 
    android:layout_alignRight="@+id/listView" 
    android:layout_alignEnd="@+id/listView" /> 
</RelativeLayout> 

phone_item.xml:

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

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="New Text" 
    android:id="@+id/textView2" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="New Text" 
    android:id="@+id/textView3" /> 
</LinearLayout> 

任何幫助非常感謝,謝謝。

+0

首先檢查'「http://schemas.android.com/apk/res\'''''''android''。你在那裏反攻。其次,讓ListView像'android:background =「#ff9900」'這樣的瘋狂顏色來檢查它是否真的可見。 – Knossos

回答

0

更改super關鍵字行

super(context,R.layout.phone_item, n); 
0

你試過覆蓋CustomAdapter類的getCount將方法?

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

您設置的onCreate之外您的自定義適配器()嘗試設置它的onCreate()內。