我是android新手。我有一个使用listview的应用程序,我想为每个列表视图项编号。 像,
1个列表项
2个列表项
3列表项
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>
我想在tvSlNo中设置编号。 请帮忙
答案 0 :(得分:0)
在listViewAdapter的getView()方法中,只需使用位置变量并在TextView上设置它。您可能需要在该位置添加1,因为它从0开始。所以使用(位置+ 1)然后将其设置为TextView
答案 1 :(得分:0)
尽管这个问题可能为时已晚,但出于其他参考目的。
检查您的代码在哪里:txt.setText(position + 1)
;
您已接近解决方案。您只需将position + 1
转换为String
,因为setText()
仅将String
单独带入。