我在我的Android应用程序中使用了列表视图,我使用字符串数组值填充它,它工作正常!但是,当我尝试使用JSON内容时,使用类,使用名称和代码作为类中的数据成员。这是我的代码:
private void fillListView() {
ListView mainListView;
ArrayAdapter<String> listAdapter;
ArrayAdapter<Planets> listPlanetsClassAdapter;
mainListView = (ListView) findViewById(R.id.lst_Details);
String[] planets = new String[]{"Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune"};
String[] planets1 = new String[]{"Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune"};
ArrayList<String> planetList = new ArrayList<String>();
ArrayList<Planets> planetLISTJSON= new ArrayList<Planets>();
Planets obj_Planets = new Planets();
planetList.addAll(Arrays.asList(planets));
// JSONArray jsonArray = new JSONArray(Arrays.asList(planets));
for ( int i=0;i<planets1.length;++i)
{
obj_Planets.code=12;
obj_Planets.name="My Gola";//planets1[i];
planetLISTJSON.add(obj_Planets);
}
// into the ArrayAdapter constructor, you must not add more items.
// Otherwise an exception will occur.
listAdapter.add("Ceres");
listAdapter.add("Pluto");
listAdapter.add("Haumea");
listAdapter.add("Makemake");
listAdapter.add("Eris");
//mainListView.deferNotifyDataSetChanged();
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter(listPlanetsClassAdapter);
}
public class Planets {
public String name;
public int code;
}
答案 0 :(得分:0)
创建一个自定义适配器,扩展public class MainActivity extends AppCompatActivity {
private TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findPath();
}
private void findPath() {
TextView txtMatrixDisplay = (TextView) findViewById(R.id.textView_display);
// 1st example
int[][] matrix1 = new int[][]{
{ 3, 4, 1, 2, 8, 6 },
{ 6, 1, 8, 2, 7, 4 },
{ 5, 9, 3, 9, 9, 5 },
{ 8, 4, 1, 3, 2, 6 },
{ 3, 7, 2, 8, 6, 4 } };
String txtMatrix1 = "";
for(int i=0; i<matrix1.length; i++){
for(int j=0; j<matrix1[0].length; j++){
txtMatrix1 += matrix1[i][j] + " ";
}
txtMatrix1 += "\n";
}
txtMatrixDisplay.setText(txtMatrix1);
Solution firstExample = new Solution();
firstExample.Path(matrix1);
String result1 = "\n";
if(firstExample.getStat()){
result1 += "Yes\n";
} else {
result1 += "No\n";
}
result1 += firstExample.getCost() + "\n";
result1 += firstExample.getPath() + "\n";
txtMatrixDisplay.append(result1);
// 2nd example
int[][] matrix2 = new int[][]{
{3, 4, 1, 2, 8, 6 },
{6, 1, 8, 2, 7, 4 },
{5, 9, 3, 9, 9, 5 },
{8, 4, 1, 3, 2, 6 },
{3, 7, 2, 1, 2, 3 } };
String txtMatrix2 = "\n";
for(int i=0; i<matrix2.length; i++){
for(int j=0; j<matrix2[0].length; j++){
txtMatrix2 += matrix2[i][j] + " ";
}
txtMatrix2 += "\n";
}
txtMatrixDisplay.append(txtMatrix2);
Solution secondExample = new Solution();
secondExample.Path(matrix2);
String result2 = "\n";
if(secondExample.getStat()){
result2 += "Yes\n";
} else {
result2 += "No\n";
}
result2 += secondExample.getCost() + "\n";
result2 += secondExample.getPath() + "\n";
txtMatrixDisplay.append(result2);
// 1st example
int[][] matrix3 = new int[][]{
{19, 10, 19, 10, 19 },
{21, 23, 20, 19, 12 },
{20, 12, 20, 11, 10 } };
String txtMatrix3 = "\n";
for(int i=0; i<matrix3.length; i++){
for(int j=0; j<matrix3[0].length; j++){
txtMatrix3 += matrix3[i][j] + " ";
}
txtMatrix3 += "\n";
}
txtMatrixDisplay.append(txtMatrix3);
Solution thirdExample = new Solution();
thirdExample.Path(matrix3);
String result3 = "\n";
if(thirdExample.getStat()){
result3 += "Yes\n";
} else {
result3 += "No\n";
}
result3 += thirdExample.getCost() + "\n";
result3 += thirdExample.getPath();
txtMatrixDisplay.append(result3);
}
}
并覆盖方法ArrayAdapter
,以返回包含您要显示的内容的自己的视图。
例如,
<强> UserAdapter.class 强>
getView()
使用自定义适配器
public class UsersAdapter extends ArrayAdapter<User> {
public UsersAdapter(Context context, ArrayList<User> users) {
super(context, 0, users);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
User user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_user, parent, false);
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvHome = (TextView) convertView.findViewById(R.id.tvHome);
// Populate the data into the template view using the data object
tvName.setText(user.name);
tvHome.setText(user.hometown);
// Return the completed view to render on screen
return convertView;
}
}
您可以查看更多详情here