在listview中传递json数组的值

时间:2015-02-06 09:11:25

标签: android json listview fragment

我正在处理学生记录项目并且其中有四个字段,例如 class name roll no。标记和用户通过编辑文本将数据放入其中 并且我将值存储到json数组中。另外,我将值放在数组列表中,然后传递给我的片段类。

新记录类

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;

}
}

我想在我的片段中返回列表视图中的列表。 因为我在edittexts中添加的值应该出现在我的片段类的列表中。

我的topfragment类

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();
        }
    });

2 个答案:

答案 0 :(得分:0)

更改此行

 Intent in = new Intent();

Intent in = getActivity().getIntent();

我希望这个会帮助你:)。

答案 1 :(得分:0)

试试这个:

在NewRecord类中

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不为空。 我过去常犯的一个常见错误是将文本颜色设置为白色,因此listview的内容实际上并不可见。所以请检查一下。