在列表视图中传递 json 数组的值

To pass the values of json array in listview

我正在做一个学生记录项目,其中有四个字段,例如 class name roll no. marks 并且用户正在通过编辑文本将数据放入其中 我将这些值存储到 json 数组中。此外,我将值放入数组列表中,并传递给我的片段 class .

新记录class

public class newrecord extends Activity {

public final static String TAG = "SomeValue";
Button but;
EditText roll, name, marks, Class;
TextView text;
String rollno, nameno, classno, marksno;

TopRatedFragment frags;
public static final String ROLL_key = "roll number";
public static final String NAME_key = "Name";
public static final String Class_key = "Class";
public static final String MARKS_key = "marks";

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.newrecord);

    final newrecord rd = new newrecord();
    // data = new dbbase(getApplicationContext());
    text = (TextView) findViewById(R.id.textviewenter);
    roll = (EditText) findViewById(R.id.editRollno);
    name = (EditText) findViewById(R.id.editName);
    marks = (EditText) findViewById(R.id.editMarks);
    Class = (EditText) findViewById(R.id.edittextclass);
    but = (Button) findViewById(R.id.btnadd);
    frags = new TopRatedFragment();
    final JSONArray ary = new JSONArray();
    final JSONObject obj = new JSONObject();

    but.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View V) {

            try {

                obj.put("Roll No", roll.getText().toString());
                obj.put("name", name.getText().toString());
                obj.put("marks", marks.getText().toString());
                obj.put("Class", Class.getText().toString());

                ary.put(obj);

                rd.nameno = name.getText().toString();
                rd.rollno = roll.getText().toString();
                rd.marksno = marks.getText().toString();
                rd.classno = Class.getText().toString();

                Log.d(TAG, "Values Inserted");
            } catch (Exception e) {
                text.setText("error");

            }

            ArrayList<String> list_list = new ArrayList<String>();
            list_list.add(rollno);
            list_list.add(nameno);
            list_list.add(classno);
            list_list.add(marksno);

            Intent i = new Intent(newrecord.this, MainActivity.class);
            // Bundle b = new Bundle();

            i.putStringArrayListExtra("fite", list_list);
            startActivity(i);

        }

    });

}

public ArrayList<HashMap<String, String>> Getalldata() {
    ArrayList<HashMap<String, String>> dude;
    dude = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> map = new HashMap<String, String>();

    map.put(ROLL_key, rollno);
    map.put(NAME_key, nameno);
    map.put(Class_key, classno);
    map.put(MARKS_key, marksno);
    dude.add(map);
    return dude;

}
}

我想 return 在我的片段的列表视图中列出。 正如我在 edittexts 中添加的值应该出现在我的片段 class.

的列表中

我的topfragment class

public class TopRatedFragment extends Fragment {
ListView List;
ListAdapter mAdapter;
int nr = 0;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_top_rated,
            container, false);


    Intent in = new Intent();

    newrecord record = new newrecord();
    String tim = in.getStringExtra("fite");

    ArrayList<String> liist = new ArrayList<String>();
    if (liist.size() == 0) {
        liist.add("Template1");
    } else {
        liist=in.getStringArrayListExtra("fite")
    }



    List = (ListView) rootView.findViewById(R.id.listView1);


    List.setAdapter(mAdapter);

    mAdapter = new SelectionAdapter(getActivity(), R.layout.simplerow,
            R.id.rowTextView, liist);
    List.setAdapter(mAdapter);


    List.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                long arg3) {
            // TODO Auto-generated method stub
            ((BaseAdapter) mAdapter).notifyDataSetChanged();
        }
    });

更改此行

 Intent in = new Intent();

Intent in = getActivity().getIntent();

希望本文对您有所帮助:)

试试这个:

在新记录中class

Intent menulist = new Intent(this, cart_activity.class);
menulist.putExtra("cartlist", cart_Array_List);
startActivity(menulist);

在你的片段中:

 intent = getIntent();
    if (intent != null) {
        arr_cart_items = (ArrayList<HashMap<String, String>>) intent
                .getSerializableExtra("cartlist");


    }

确保您的 ArrayList 不为空。 我曾经犯过的一个常见错误是将文本颜色设置为白色,因此列表视图的内容实际上并不可见。所以也请检查一下。