所以我在custom adapter for listview in fragment Android
上关注了@AndiGeeky的回答但它没有用。 这是我的代码。
public class TabLectura extends Fragment implements View.OnClickListener {
private View view;
private ListView list_calllog;
private static final String ARG_SECTION_NUMBER = "section_number";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
if(view==null)
{
view=inflater.inflate(R.layout.tab_lectura, container,false);
}
else
{
ViewGroup parent = (ViewGroup) view.getParent();
parent.removeView(view);
}
ClassOrdenDetalle detalle = new ClassOrdenDetalle(123,"321",15,15);
ArrayList<ClassOrdenDetalle> ordeness = new ArrayList<>();
ordeness.add(detalle);
MyTableAdapter adapter=new MyTableAdapter(getActivity(),R.layout.list_item_table,ordeness);
list_calllog=(ListView)view.findViewById(R.id.tableArticulosView);
list_calllog.setAdapter(adapter);
return view;
}
public static TabLectura newInstance(int sectionNumber) {
TabLectura fragment = new TabLectura();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public void onClick(View v) {
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == getActivity().RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
Toast.makeText(getActivity(),"SCAN_RESULT --- >>> " + contents ,
Toast.LENGTH_LONG).show();
// Handle successful scan
} else if (resultCode == getActivity().RESULT_CANCELED) {
Toast.makeText(getActivity(),"Error" ,
Toast.LENGTH_LONG).show();
}
}
}
class MyTableAdapter extends ArrayAdapter<ClassOrdenDetalle> {
Context context;
int layoutResourceId;
ArrayList<ClassOrdenDetalle> data = null;
public MyTableAdapter(Context mContext, int layoutResourceId, ArrayList<ClassOrdenDetalle> data) {
super(mContext, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = mContext;
this.data = data;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(layoutResourceId, parent,false);
ClassOrdenDetalle item = data.get(position);
Holder holder=new Holder();
holder.text_cantidad=(TextView)view.findViewById(R.id.txtcantidad);
holder.text_codigo=(TextView)view.findViewById(R.id.txtarticulo);
holder.text_codigo.setText(item.getCod_articulo());
holder.text_cantidad.setText(item.getCantidad()+"");
return view;
}
public class Holder
{
TextView text_codigo;
TextView text_cantidad;
}
}}
这是我的tab_lectura.xml
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_gravity="fill_vertical"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:padding="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Nro Orden"
android:textColor="@color/colorPrimaryDark"
android:id="@+id/textView3" />
<Spinner
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/spinner" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="80dp"
android:paddingTop="10dp"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Contenedor"
android:textColor="@color/colorPrimaryDark"
android:id="@+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:paddingLeft="10dp"
android:id="@+id/edtContenedor" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:weightSum="2"
android:layout_height="match_parent">
<TextView
android:layout_width="0dp"
android:id="@+id/txtarticulo"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_weight="1"
android:text="Articulo"
android:textColor="@color/colorPrimaryDark" />
<TextView
android:layout_width="0dp"
android:id="@+id/txtcantidad"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Cantidad"
android:textColor="@color/colorPrimaryDark" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tableArticulosView" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnQRCode"
android:src="@mipmap/qr_code"
android:background="@null"
android:layout_weight="1"
android:layout_gravity="right" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Guardar"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</LinearLayout>
</LinearLayout>
</FrameLayout></android.support.v4.widget.NestedScrollView>
这是我的list_item_table.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp" >
<!-- Tag value -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:layout_gravity="center_vertical" >
<TextView android:id="@+id/article_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="14sp" />
</LinearLayout>
<View android:layout_width="3dp" android:layout_height="3dp" />
<!-- Tag count -->
<TextView android:id="@+id/article_count"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical|end"
android:gravity="center_vertical|center"
android:textStyle="bold"
android:textSize="14sp" /></LinearLayout>
LOGCAT错误
09-19 15:23:07.939 13248-13248/? E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException
at com.todosoft.frutifor.TabLectura$MyTableAdapter.getView(TabLectura.java:120)
at android.widget.AbsListView.obtainView(AbsListView.java:2177)
at android.widget.ListView.onMeasure(ListView.java:1131)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15848)
at android.support.v4.widget.NestedScrollView.measureChildWithMargins(NestedScrollView.java:1415)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v4.widget.NestedScrollView.onMeasure(NestedScrollView.java:480)
at android.view.View.measure(View.java:15848)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1560)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:735)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
at android.view.View.measure(View.java:15848)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Loope09-19 15:28:08.072 1324813248/com.todosoft.frutiforI/Process: Sending signal. PID: 13248 SIG: 9
这是行错误
holder.text_codigo.setText(item.getCod_articulo());
因为holder.text_codigo
是null
。
提前感谢您的帮助,并对不良内容(仅限于stackoverflow)感到抱歉,我是创建帖子的新手。
答案 0 :(得分:0)
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
Holder holder;
if (view == null) {
LayoutInflater inflater= LayoutInflater.from(parent.getContext());
view = inflater.inflate(layoutResourceId, parent, false);
holder = new Holder();
holder.text_cantidad=(TextView) view.findViewById(R.id.article_count);
holder.text_codigo=(TextView) view.findViewById(R.id.article_code);
view.setTag(holder);
} else {
holder = (Holder) view.getTag();
}
ClassOrdenDetalle item = data.get(position);
holder.text_codigo.setText(item.getCod_articulo());
holder.text_cantidad.setText(item.getCantidad()+"");
return view;
}
答案 1 :(得分:0)
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Context context = activity.getApplicationContext();
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(layoutResourceId, null);
}
ClassOrdenDetalle item = data.get(position);
Holder holder=new Holder();
holder.text_cantidad=(TextView)convertView.findViewById(R.id.txtcantidad);
holder.text_codigo=(TextView)convertView.findViewById(R.id.txtarticulo);
holder.text_codigo.setText(item.getCod_articulo());
holder.text_cantidad.setText(item.getCantidad()+"");
return view;
}