2017-08-12 2 views
0

Ich versuche, eine ListView mit 2 TextView zu erstellen. Ich bin nicht sehr gut in Java, deshalb folge ich normalerweise vielen Tutorials und kombiniere sie, um das zu erschaffen, was ich brauche.Mehrere TextView auf einer Listview

Aber ich habe versucht, ohne viel Erfolg 2 Führer zusammen zu kombinieren ...

Hier ist das Tutorial ich die zweite TextView zu folgen bin versucht, hinzuzufügen: https://www.youtube.com/watch?annotation_id=annotation_3104328239&feature=iv&src_vid=8K-6gdTlGEA&v=E6vE8fqQPTE

Aber das doesn‘ t mir wirklich helfen, da ich Schwierigkeiten habe zu verstehen, wie ich umsetzen kann, was er tut.

Bisher, was ich verstanden habe ist, dass ich so meine Artikel hinzufügen müssen:

countryList.add(new EntryItem("Électron","1/6")); 

Dazu werde ich EntryItem und meine Klasse zu ändern, die BaseAdapter

Aber ich erstreckt war damit nicht erfolgreich.

Was ist der beste Weg, um meine zweite TextView hinzuzufügen?

Fragment Aktivität

public class Tab1Fragment extends Fragment { 

ListView lvCountry; 

@RequiresApi(api = Build.VERSION_CODES.M) 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.activity_main,container,false); 

     lvCountry = (ListView) view.findViewById(R.id.lvCountry); 

     ArrayList<Item> countryList = new ArrayList<Tab1Fragment.Item>(); 
     // Header 
    countryList.add(new SectionItem("Atomes")); // 0 
    // State Name 
    countryList.add(new EntryItem("Électron")); // 1 
    countryList.add(new EntryItem("Monstre")); // 2 
    countryList.add(new EntryItem("Neutron")); //3 
    countryList.add(new EntryItem("Proton")); //4 
    countryList.add(new EntryItem("Stigmate")); //5 

    // Header 
    countryList.add(new SectionItem("Chaos")); //6 
    // State Name 
    countryList.add(new EntryItem("Casbah")); //7 
    countryList.add(new EntryItem("Chaos")); //8 
    countryList.add(new EntryItem("Gaufrette"));//9 
    countryList.add(new EntryItem("Lorenz")); //10 

    // Header 
    countryList.add(new SectionItem("Monolith")); //11 
    // State Name 
    countryList.add(new EntryItem("Chanceux")); //12 
    countryList.add(new EntryItem("Monolith")); //13 
    countryList.add(new EntryItem("Ubik")); //14 

    // Header 
    countryList.add(new SectionItem("Autres")); //15 
    // State Name 
    countryList.add(new EntryItem("Beau")); //16 
    countryList.add(new EntryItem("Enclume")); //17 
    countryList.add(new EntryItem("Huitre")); //18 

    // set adapter 
     final CountryAdapter adapter = new CountryAdapter(getActivity(), countryList); 
     lvCountry.setAdapter(adapter); 

    com.melnykov.fab.FloatingActionButton fab = (com.melnykov.fab.FloatingActionButton) view.findViewById(R.id.fab); 
    fab.attachToListView(lvCountry); 

    lvCountry.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      int i = 0; 
      for (i= 0; i < 16 ; i++) { 


       if(position== 0||position==6||position==11||position==15){ 


       } 
       else { 
        Intent myintent = new Intent(view.getContext(), MainActivity.class); 
        startActivityForResult(myintent, i); 
       } 


      } 
     } 
    }); 

     lvCountry.setTextFilterEnabled(true); 

     // filter on text change 



    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 

     } 
    }); 
    return view; 
} 





    /** 
    * row item 
    */ 
    public interface Item { 
     public boolean isSection(); 
     public String getTitle(); 
    } 

    /** 
    * Section Item 
    */ 
    public class SectionItem implements Item { 
     private final String title; 

     public SectionItem(String title) { 
      this.title = title; 
     } 

     public String getTitle() { 
      return title; 
     } 

     @Override 
     public boolean isSection() { 
      return true; 
     } 
    } 

    /** 
    * Entry Item 
    */ 
    public class EntryItem implements Item { 
     public final String title; 

     public EntryItem(String title) { 
      this.title = title; 
     } 

     public String getTitle() { 
      return title; 
     } 

     @Override 
     public boolean isSection() { 
      return false; 
     } 
    } 

    /** 
    * Adapter 
    */ 
    public class CountryAdapter extends BaseAdapter { 
     private Context context; 
     private ArrayList<Item> item; 
     private ArrayList<Item> originalItem; 

     public CountryAdapter(Tab1Fragment tab1Fragment, ArrayList<Item> countryList) { 
      super(); 
     } 

     public CountryAdapter(Context context, ArrayList<Item> item) { 
      this.context = context; 
      this.item = item; 
      //this.originalItem = item; 
     } 

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

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

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

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) 
     { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      if (item.get(position).isSection()) { 
       // if section header 
       convertView = inflater.inflate(R.layout.layout_section, parent, false); 
       TextView tvSectionTitle = (TextView) convertView.findViewById(R.id.tvSectionTitle); 
       tvSectionTitle.setText(((SectionItem) item.get(position)).getTitle()); 
      } 
      else 
      { 
       // if item 
       convertView = inflater.inflate(R.layout.layout_item, parent, false); 
       TextView tvItemTitle = (TextView) convertView.findViewById(R.id.tvItemTitle); 
       tvItemTitle.setText(((EntryItem) item.get(position)).getTitle()); 
      } 

      return convertView; 
     } 

     /** 
     * Filter 
     */ 
     public Filter getFilter() 
     { 
      Filter filter = new Filter() { 

       @SuppressWarnings("unchecked") 
       @Override 
       protected void publishResults(CharSequence constraint, FilterResults results) { 

        item = (ArrayList<Item>) results.values; 
        notifyDataSetChanged(); 
       } 

       @SuppressWarnings("null") 
       @Override 
       protected FilterResults performFiltering(CharSequence constraint) { 

        FilterResults results = new FilterResults(); 
        ArrayList<Item> filteredArrayList = new ArrayList<Item>(); 


        if(originalItem == null || originalItem.size() == 0) 
        { 
         originalItem = new ArrayList<Item>(item); 
        } 

       /* 
       * if constraint is null then return original value 
       * else return filtered value 
       */ 
        if(constraint == null && constraint.length() == 0) 
        { 
         results.count = originalItem.size(); 
         results.values = originalItem; 
        } 
        else 
        { 
         constraint = constraint.toString().toLowerCase(Locale.ENGLISH); 
         for (int i = 0; i < originalItem.size(); i++) 
         { 
          String title = originalItem.get(i).getTitle().toLowerCase(Locale.ENGLISH); 
          if(title.startsWith(constraint.toString())) 
          { 
           filteredArrayList.add(originalItem.get(i)); 
          } 
         } 
         results.count = filteredArrayList.size(); 
         results.values = filteredArrayList; 
        } 

        return results; 
       } 
      }; 

      return filter; 
     } 
    } 

} 

Layout_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" 
android:weightSum="100"> 


    <TextView 
     android:id="@+id/tvItemTitle" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_weight="20" 
     android:gravity="center_vertical" 
     android:paddingLeft="20dp" 
     android:text="TextView1" 
     android:textAppearance="@style/TextAppearance.AppCompat" 
     android:textSize="18sp" /> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="80"> 

    <TextView 
     android:gravity="center" 
     android:text="1/6" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:id="@+id/textView2"/> 

</LinearLayout> 

</LinearLayout> 

This is what the App looks like for now

Jede mögliche Hilfe würde geschätzt!

Sie haben einen Grammatikfehler gefunden? Behalte es nicht für dich! Englisch ist nicht meine Muttersprache. Teile es, damit ich mich selbst korrigieren kann!

Antwort

0

Zuerst müssen Sie Ihre EntryItem durch ein Feld ändern Addieren des Wertes, um anzuzeigen, wie folgt aus:

public class EntryItem implements Item { 

    public final String title; 
    public final String value; 

    public EntryItem(String title, String value) { 
     this.title = title; 
     this.value = value; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public String getValue() { 
     return value; 
    } 

    @Override 
    public boolean isSection() { 
     return false; 
    } 
} 

Dann müssen Sie die TextView Bezug Ihrer TextView von id erhalten textView2 und setText() innen getView(int, View, ViewGroup) Ihres Adapters.

if (item.get(position).isSection()) { 
    //your code... 
} else { 
    //your code... 
    TextView tvItemTitle = (TextView) convertView.findViewById(R.id.textView2); 
    tvItemTitle.setText(((EntryItem) item.get(position)).getValue()); 
} 

Schließlich aktualisieren Sie Ihre EntryItem Daten:

countryList.add(new SectionItem("Atomes")); // 0 
// State Name 
countryList.add(new EntryItem("Électron", "1/6")); // 1 
countryList.add(new EntryItem("Monstre", "1/6")); // 2 
countryList.add(new EntryItem("Neutron", "1/6")); //3 
countryList.add(new EntryItem("Proton", "1/6")); //4 
countryList.add(new EntryItem("Stigmate", "1/6")); //5 

// Header 
countryList.add(new SectionItem("Chaos")); //6 
// State Name 
countryList.add(new EntryItem("Casbah", "1/6")); //7 
countryList.add(new EntryItem("Chaos", "1/6")); //8 
countryList.add(new EntryItem("Gaufrette", "1/6"));//9 
countryList.add(new EntryItem("Lorenz", "1/6")); //10 

// Header 
countryList.add(new SectionItem("Monolith")); //11 
// State Name 
countryList.add(new EntryItem("Chanceux", "1/6")); //12 
countryList.add(new EntryItem("Monolith", "1/6")); //13 
countryList.add(new EntryItem("Ubik", "1/6")); //14 

// Header 
countryList.add(new SectionItem("Autres")); //15 
// State Name 
countryList.add(new EntryItem("Beau", "1/6")); //16 
countryList.add(new EntryItem("Enclume", "1/6")); //17 
countryList.add(new EntryItem("Huitre", "1/6")); //18 
Verwandte Themen