如何在列表视图中显示数据(recyclerview 更好)?
how to show data in list-view (recyclerview is better)?
我想在这种 table
中显示来自 string-array
的 string.xml
的一些数据
下面是我要找的 table 图片:
我必须做 custom adapter
但我不知道怎么做
下面是我自定义的代码listview
<?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"
xmlns:tools="http://schemas.android.com/tools">
<TextView
android:id="@+id/list_item_string"
tools:text="asifgiouweifhsklajfhkh"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:paddingLeft="8dp"
android:textSize="18sp"
android:textStyle="bold"
android:layout_toStartOf="@+id/quantityView"/>
<Button
android:id="@+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:text="??" />
<TextView
android:text="QO"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/list_item_string"
android:layout_alignEnd="@+id/delete_btn"
android:layout_marginEnd="80dp"
android:id="@+id/unitView"/>
<TextView
android:text="1234"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/quantityView"
android:layout_alignBottom="@+id/unitView"
android:layout_toStartOf="@+id/unitView"
android:layout_alignParentTop="true"/>
怎么做?谢谢
列表视图适配器:
public class TalktimeAdapter extends ArrayAdapter<PlanDetails> {
Context context;
View view;
public TalktimeAdapter(Context context,List<PlanDetails> items) {
super(context,0, items);
this.context=context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
PlanDetails rowitem= getItem(position);
view=convertView;
ViewHolder holder;
if (view==null){
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.fulltalk_items,parent,false);
holder=new ViewHolder();
holder.planoverview=(TextView)view.findViewById(R.id.planoverview);
holder.planbenefit=(TextView)view.findViewById(R.id.planbenefit);
holder.planamount=(TextView)view.findViewById(R.id.planamount);
view.setTag(holder);
}else{
holder=(ViewHolder)view.getTag();
}
assert rowitem != null;
holder.planoverview.setText(rowitem.getOne());
holder.planbenefit.setText(rowitem.getTwo());
holder.planamount.setText(rowitem.getThree());
return view;
}
private static class ViewHolder
{
TextView planoverview,planbenefit,planamount;
}
}
型号class:
public class PlanDetails {
String one,two,three;
public PlanDetails(String one, String two, String three) {
this.one = one;
this.two = two;
this.three = three;
}
public String getThree() {
return three;
}
public void setThree(String three) {
this.three = three;
}
public String getTwo() {
return two;
}
public void setTwo(String two) {
this.two = two;
}
public String getOne() {
return one;
}
public void setOne(String one) {
this.one = one;
}
}
Activity:
public class FullTalkTimeActivity extends AppCompatActivity {
ListView listView;
TalktimeAdapter adapter;
List<PlanDetails> row;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fulltalk_view);
listView=(ListView)findViewById(R.id.planList);
row = new ArrayList<>();
row.add(new PlanDetails("oneoneone","twotwotwo","11"));
row.add(new PlanDetails("oneoneone","twotwotwo","12"));
row.add(new PlanDetails("oneoneone","twotwotwo","13"));
row.add(new PlanDetails("oneoneone","twotwotwo","14"));
row.add(new PlanDetails("oneoneone","twotwotwo","15"));
row.add(new PlanDetails("oneoneone","twotwotwo","16"));
row.add(new PlanDetails("oneoneone","twotwotwo","17"));
row.add(new PlanDetails("oneoneone","twotwotwo","18"));
row.add(new PlanDetails("oneoneone","twotwotwo","19"));
row.add(new PlanDetails("oneoneone","twotwotwo","21"));
row.add(new PlanDetails("oneoneone","twotwotwo","31"));
row.add(new PlanDetails("oneoneone","twotwotwo","41"));
row.add(new PlanDetails("oneoneone","twotwotwo","51"));
row.add(new PlanDetails("oneoneone","twotwotwo","61"));
row.add(new PlanDetails("oneoneone","twotwotwo","71"));
row.add(new PlanDetails("oneoneone","twotwotwo","81"));
row.add(new PlanDetails("oneoneone","twotwotwo","91"));
row.add(new PlanDetails("oneoneone","twotwotwo","101"));
row.add(new PlanDetails("oneoneone","twotwotwo","121"));
row.add(new PlanDetails("oneoneone","twotwotwo","1011"));
row.add(new PlanDetails("oneoneone","twotwotwo","1521"));
row.add(new PlanDetails("oneoneone","twotwotwo","1081"));
row.add(new PlanDetails("oneoneone","twotwotwo","1291"));
row.add(new PlanDetails("oneoneone","twotwotwo","1001"));
row.add(new PlanDetails("oneoneone","twotwotwo","1261"));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
PlanDetails pm= row.get(i);
Toast.makeText(FullTalkTimeActivity.this,pm.getThree().trim(),Toast.LENGTH_SHORT).show();
}
});
adapter = new TalktimeAdapter(getActivity(), row);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
我想在这种 table
中显示来自string-array
的 string.xml
的一些数据
下面是我要找的 table 图片:
我必须做 custom adapter
但我不知道怎么做
下面是我自定义的代码listview
<?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"
xmlns:tools="http://schemas.android.com/tools">
<TextView
android:id="@+id/list_item_string"
tools:text="asifgiouweifhsklajfhkh"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:paddingLeft="8dp"
android:textSize="18sp"
android:textStyle="bold"
android:layout_toStartOf="@+id/quantityView"/>
<Button
android:id="@+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:text="??" />
<TextView
android:text="QO"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/list_item_string"
android:layout_alignEnd="@+id/delete_btn"
android:layout_marginEnd="80dp"
android:id="@+id/unitView"/>
<TextView
android:text="1234"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/quantityView"
android:layout_alignBottom="@+id/unitView"
android:layout_toStartOf="@+id/unitView"
android:layout_alignParentTop="true"/>
怎么做?谢谢
列表视图适配器:
public class TalktimeAdapter extends ArrayAdapter<PlanDetails> {
Context context;
View view;
public TalktimeAdapter(Context context,List<PlanDetails> items) {
super(context,0, items);
this.context=context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
PlanDetails rowitem= getItem(position);
view=convertView;
ViewHolder holder;
if (view==null){
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.fulltalk_items,parent,false);
holder=new ViewHolder();
holder.planoverview=(TextView)view.findViewById(R.id.planoverview);
holder.planbenefit=(TextView)view.findViewById(R.id.planbenefit);
holder.planamount=(TextView)view.findViewById(R.id.planamount);
view.setTag(holder);
}else{
holder=(ViewHolder)view.getTag();
}
assert rowitem != null;
holder.planoverview.setText(rowitem.getOne());
holder.planbenefit.setText(rowitem.getTwo());
holder.planamount.setText(rowitem.getThree());
return view;
}
private static class ViewHolder
{
TextView planoverview,planbenefit,planamount;
}
}
型号class:
public class PlanDetails {
String one,two,three;
public PlanDetails(String one, String two, String three) {
this.one = one;
this.two = two;
this.three = three;
}
public String getThree() {
return three;
}
public void setThree(String three) {
this.three = three;
}
public String getTwo() {
return two;
}
public void setTwo(String two) {
this.two = two;
}
public String getOne() {
return one;
}
public void setOne(String one) {
this.one = one;
}
}
Activity:
public class FullTalkTimeActivity extends AppCompatActivity {
ListView listView;
TalktimeAdapter adapter;
List<PlanDetails> row;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fulltalk_view);
listView=(ListView)findViewById(R.id.planList);
row = new ArrayList<>();
row.add(new PlanDetails("oneoneone","twotwotwo","11"));
row.add(new PlanDetails("oneoneone","twotwotwo","12"));
row.add(new PlanDetails("oneoneone","twotwotwo","13"));
row.add(new PlanDetails("oneoneone","twotwotwo","14"));
row.add(new PlanDetails("oneoneone","twotwotwo","15"));
row.add(new PlanDetails("oneoneone","twotwotwo","16"));
row.add(new PlanDetails("oneoneone","twotwotwo","17"));
row.add(new PlanDetails("oneoneone","twotwotwo","18"));
row.add(new PlanDetails("oneoneone","twotwotwo","19"));
row.add(new PlanDetails("oneoneone","twotwotwo","21"));
row.add(new PlanDetails("oneoneone","twotwotwo","31"));
row.add(new PlanDetails("oneoneone","twotwotwo","41"));
row.add(new PlanDetails("oneoneone","twotwotwo","51"));
row.add(new PlanDetails("oneoneone","twotwotwo","61"));
row.add(new PlanDetails("oneoneone","twotwotwo","71"));
row.add(new PlanDetails("oneoneone","twotwotwo","81"));
row.add(new PlanDetails("oneoneone","twotwotwo","91"));
row.add(new PlanDetails("oneoneone","twotwotwo","101"));
row.add(new PlanDetails("oneoneone","twotwotwo","121"));
row.add(new PlanDetails("oneoneone","twotwotwo","1011"));
row.add(new PlanDetails("oneoneone","twotwotwo","1521"));
row.add(new PlanDetails("oneoneone","twotwotwo","1081"));
row.add(new PlanDetails("oneoneone","twotwotwo","1291"));
row.add(new PlanDetails("oneoneone","twotwotwo","1001"));
row.add(new PlanDetails("oneoneone","twotwotwo","1261"));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
PlanDetails pm= row.get(i);
Toast.makeText(FullTalkTimeActivity.this,pm.getThree().trim(),Toast.LENGTH_SHORT).show();
}
});
adapter = new TalktimeAdapter(getActivity(), row);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}