我想显示数据库中的3个字段,但我不知道simple_list_item_1
SQLiteDatabase db = dbcenter.getReadableDatabase();
cursor = db.rawQuery("SELECT * FROM TABLE", null);
daftar = new String[cursor.getCount()];
cursor.moveToFirst();
for (int cc = 0; cc < cursor.getCount(); cc++) {
cursor.moveToPosition(cc);
daftar[cc] = cursor.getString(1).toString();
}
ListView01 = (ListView) view.findViewById(R.id.listView1);
ListView01.setAdapter(new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, daftar));
ListView01.setSelected(true);
在上面的视图中,代码显示表中的数据,但只显示一个字段。
你能给我一个例子吗,用3代list_item
用java代码将其替换为修改后的<TextView>
答案 0 :(得分:0)
当您致电new ArrayAdapter(..., android.R.layout.simple_list_item_1, ...)
时,您告诉系统使用android.R.layout.simple_list_item_1
表示您的数据。如果这不起作用,那么实际上只有一个选择:自己处理充气和填充视图。您可以通过创建ArrayAdapter
的自定义子类并覆盖onCreateView()
来实现此目的。
官方文件:https://developer.android.com/reference/android/widget/ArrayAdapter.html
您还可以自定义集合中数据对象使用的视图类型。要自定义用于数据对象的视图类型,请覆盖
getView(int, View, ViewGroup)
并为视图资源充气。
答案 1 :(得分:0)
创建一个名为custom_layout的xml,如
$parts= explode(",", $pattern);
$var1 = $parts[0];
$var2 = $parts[1];
$var3 = $parts[2];
使用像R.layout.custom_layout而不是android.R.layout.simple_list_item_1
答案 2 :(得分:0)
您可以使用3个文本视图创建自定义列表视图
现在custom_list_view布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:text="@string/name"
android:textSize="20sp"
android:layout_marginTop="19dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/price"
android:layout_toStartOf="@+id/price"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/price"
android:text="Price"
android:textSize="20sp"
android:layout_alignBaseline="@+id/name"
android:layout_alignBottom="@+id/name"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/stock"
android:text="Stock"
android:textSize="20sp"
android:layout_alignBaseline="@+id/price"
android:layout_alignBottom="@+id/price"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="81dp"
android:layout_marginEnd="81dp"/>
</RelativeLayout>
在布局中创建列表视图,我在mainactivity布局中创建
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.pro.salman.customlistview.MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list_item"/>
</RelativeLayout>
自定义列表视图类
public class CustomListView extends BaseAdapter {
private ArrayList<HashMap<String,String>> list;
private LayoutInflater mLayoutInflater;
private Context mContext;
public CustomListView(ArrayList<HashMap<String,String>> list,Context c) {
this.list = list;
this.mContext = c;
mLayoutInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = mLayoutInflater.inflate(R.layout.custom_list_view,parent,false);
Holder h = new Holder();
// set id's
h.name = (TextView)(view.findViewById(R.id.name));
h.price = (TextView)(view.findViewById(R.id.price));
h.stock = (TextView)(view.findViewById(R.id.stock));
HashMap<String,String> hashMap = new HashMap<>();
hashMap = list.get(position);
h.name.setText(hashMap.get("nameKey"));
h.price.setText(hashMap.get("priceKey"));
h.stock.setText(hashMap.get("stockKey"));
return view;
}
private class Holder
{
TextView name;
TextView price;
TextView stock;
}
}
现在在主要活动课
public class MainActivity extends AppCompatActivity {
private ListView mListView;
private ArrayList<HashMap<String,String>> mArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mArrayList = new ArrayList<HashMap<String, String>>();
for(int i =0;i<20;i++)
{
HashMap<String,String> mHashMap = new HashMap<>();
mHashMap.put("nameKey","name "+String.valueOf(i));
mHashMap.put("priceKey","price "+String.valueOf(i));
mHashMap.put("stockKey","stock "+String.valueOf(i));
mArrayList.add(mHashMap);
}
mListView = (ListView)(findViewById(R.id.list_item));
CustomListView customListView = new CustomListView(mArrayList,this);
mListView.setAdapter(customListView);
}
}
结果