2016-12-19 1 views
1

Ich bin neu in Android. Ich habe eine Anwendung, die mit listview arbeitet, und ich möchte jedes Listenansichtselement nummerieren. Wie,Wie Nummer zu Listview Artikel in Android geben

1 Listeneintrag

2 Listeneintrag

3 Listeneintrag

MainActivity.java

public class MainActivity extends AppCompatActivity { 
private OrderDbManager orderDbManager; 


private ListView listView; 

private SimpleCursorAdapter adapter; 

final String[] from = new String[] { OrderDbHelper.FOOD_NAME, 
     OrderDbHelper.QUANTITY, OrderDbHelper.PRICE }; 

final int[] to = new int[] {R.id.tvFoodName, R.id.tvItemQuantity, R.id.tvItemPrice }; 

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

    orderDbManager = new OrderDbManager(this); 
    orderDbManager.open(); 

    final Cursor cursor = orderDbManager.fetch(); 

    listView = (ListView) findViewById(R.id.listView); 
    listView.setEmptyView(findViewById(R.id.empty)); 

    adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, from, to, 0); 
    adapter.notifyDataSetChanged(); 

    LayoutInflater layoutInflater = getLayoutInflater(); 
        listView.addHeaderView(layoutInflater.inflate(R.layout.cart_header,listView, false)); 
    listView.setAdapter(adapter); 
    } 
    public class MyAdapter extends SimpleCursorAdapter { 
    public MyAdapter(Context context, int layout, Cursor c, 
    String[] from,int[] to) { 
     super(context, layout, c, from, to); 

    } 


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

    @Override 
    public Object getItem(int arg0) { 
     return null; 
    } 

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = layoutInflater.inflate(R.layout.list_item, null, true); 
     TextView txt= (TextView) convertView.findViewById(R.id.tvSlNo); 
     txt.setText(position + 1); 

     return convertView; 
    } 

    } 
} 

activity_main.xml

<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" > 

<ListView 
    android:id="@+id/listView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" > 
</ListView> 

list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_margin="5dp" 
android:minHeight="50dp" 
android:orientation="horizontal" 
android:weightSum="1"> 

<TextView 
    android:id="@+id/tvSlNo" 
    android:layout_width="0dip" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:textStyle="normal" 
    android:textColor="@android:color/black" 
    android:layout_weight="0.1" /> 

<TextView 
    android:id="@+id/tvFoodName" 
    android:layout_width="0dip" 
    android:layout_height="match_parent" 
    android:layout_weight="0.5" 
    android:gravity="center" 
    android:textStyle="normal" 
    android:textColor="@android:color/black" 
    android:text=""/> 


<TextView 
    android:id="@+id/tvItemQuantity" 
    android:layout_width="0dip" 
    android:layout_height="match_parent" 
    android:layout_weight="0.15" 
    android:textStyle="normal" 
    android:textColor="@android:color/black" 
    android:gravity="center"/> 

<TextView 
    android:id="@+id/tvItemPrice" 
    android:layout_width="0dip" 
    android:layout_height="match_parent" 
    android:layout_weight="0.25" 
    android:gravity="center" 
    android:textStyle="normal" 
    android:textColor="@android:color/black" 
    android:text=""/> 
    </LinearLayout> 

ich will in tvSlNo Nummerierung einzustellen. bitte helfen

+0

Veröffentlichen Sie Ihren Adapter bitte. –

+0

Ich verwende SimpleCursorAdapter und habe keine andere Adapterklasse – Rohith

+0

Sie müssen einen eigenen Adapter schreiben, um Ihre Liste zu füllen, damit Sie mehr Kontrolle über Ihre Liste haben. Es gibt viele Beispiele über das Internet. –

Antwort

0

In Ihrer getView() - Methode Ihres listViewAdapter verwenden Sie einfach die Positionsvariable und setzen Sie sie auf Ihrem TextView. Sie müssen möglicherweise 1 zu der Position hinzufügen, da es mit 0 beginnt. Verwenden Sie also (Position + 1) und stellen Sie es dann auf Ihren TextView

Verwandte Themen