我正在尝试将本地JSON数据显示到listview中的片段,但每当我运行应用程序时,只要我点击main2activity中的voir_of就会崩溃
这是我想要显示填充数据的列表视图的文件的XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.root.afrochim.Main2Activity">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
</LinearLayout>
这是门票
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/background_layout"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Référence:"
android:textSize="25dp" />
<TextView
android:id="@+id/ref"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text=""
android:textSize="25dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Date planifiée:"
android:textSize="25dp" />
<TextView
android:id="@+id/dateplan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text=""
android:textSize="25dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Article:"
android:textSize="25dp" />
<TextView
android:id="@+id/article"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text=""
android:textSize="25dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Entrepot:"
android:textSize="25dp" />
<TextView
android:id="@+id/entrepot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text=""
android:textSize="25dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Quantité d'article:"
android:textSize="25dp" />
<TextView
android:id="@+id/qte"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text=""
android:textSize="25dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="unité de mesure d'article:"
android:textSize="25dp" />
<TextView
android:id="@+id/unitemes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text=""
android:textSize="25dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="état:"
android:textSize="25dp" />
<TextView
android:id="@+id/etat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text=""
android:textSize="25dp" />
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView4"
android:layout_width="20pt"
android:layout_height="20pt"
android:layout_gravity="center"
android:layout_weight="1"
app:srcCompat="@drawable/start" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="20pt"
android:layout_height="20pt"
android:layout_gravity="right"
android:layout_weight="1"
app:srcCompat="@drawable/stop" />
</LinearLayout>
</LinearLayout>
这是笔记课
public class note {
String ref = null;
String date = null;
String article = null;
String entrp = null;
Double qte = null;
String unite = null;
String etat = null;
public note(String ref, String date, String article, String entrp, Double qte, String unite, String etat) {
this.ref = ref;
this.article = article;
this.date = date;
this.entrp = entrp;
this.etat = etat;
this.unite = unite;
this.qte = qte;
}
}
这是main2activity
public class Main2Activity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
FragmentManager fragmentmanager = getFragmentManager();
if (id == R.id.of) {
fragmentmanager.beginTransaction().replace(R.id.content_frame, new firstfragment()).commit();
} else if (id == R.id.comm) {
fragmentmanager.beginTransaction().replace(R.id.content_frame, new secondfragment()).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
这是第一个片段
public class firstfragment extends Fragment {
View myview;
public firstfragment() {
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
myview = inflater.inflate(R.layout.voir_of, container, false);
return myview;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<note> items = new ArrayList<note>();
items = getdatajson();
MycustomAdapter myadapter = new MycustomAdapter(items);
ListView ls = (ListView) getView().findViewById(R.id.listview);
ls.setAdapter(myadapter);
}
public ArrayList<note> getdatajson() {
ArrayList<note> list = new ArrayList<note>();//to verify
String json;
String refj = null;
String datej = null;
String articlej = null;
String entrpj = null;
Double qtej = null;
String unitej = null;
String etatj = null;
try {
InputStream is = getActivity().getAssets().open("data.json");//ouvrir fichier json
int size = is.available();
byte[] buffer = new byte[size];
is.read();
is.close();
json = new String(buffer, "UTF-8");
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
refj = obj.getString("reference");
datej = obj.getString("date");
articlej = obj.getString("article");
entrpj = obj.getString("entrepot");
qtej = obj.getDouble("quantité");
unitej = obj.getString("unité");
etatj = obj.getString("etat");
list.add(new note(refj, datej, articlej, entrpj, qtej, unitej, etatj));
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return list;
}
public class MycustomAdapter extends BaseAdapter {
ArrayList<note> tab = new ArrayList<note>();//to verify
MycustomAdapter(ArrayList<note> tab) {
this.tab = tab;
}
@Override
public int getCount() {
return tab.size();
}
@Override
public Object getItem(int i) {
return tab.get(i).ref;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater linflater = getLayoutInflater();
View view1 = linflater.inflate(R.layout.ticket, null);
// parcour le tableau et remplir ticket by id
TextView ref_view = (TextView) view1.findViewById(R.id.ref);
TextView date_view = (TextView) view1.findViewById(R.id.dateplan);
TextView article_view = (TextView) view1.findViewById(R.id.article);
TextView entrp_view = (TextView) view1.findViewById(R.id.entrepot);
TextView qte_view = (TextView) view1.findViewById(R.id.qte);
TextView unite_view = (TextView) view1.findViewById(R.id.unitemes);
TextView etat_view = (TextView) view1.findViewById(R.id.etat);
ref_view.setText(tab.get(i).ref);
date_view.setText(tab.get(i).date);
article_view.setText(tab.get(i).article);
entrp_view.setText(tab.get(i).entrp);
qte_view.setText((tab.get(i).qte).toString());
unite_view.setText(tab.get(i).unite);
etat_view.setText(tab.get(i).etat);
return view1;
}
}
}