1

i'm working on a student record project And there are four fields in it like class name roll no. marks and User is putting data into it by edit text and i'm storing the values into the json Array . Also i'm putting values in array list and i'm passing to my fragment class .

New record 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;

}
}

And i want to return list in the listview in my fragment . As in the values that i add in edittexts should appear in the list in my fragment class.

My 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();
        }
    });
androidcodes
  • 141
  • 1
  • 3
  • 11
  • possible duplicate of [Send data from activity to fragment in android](http://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android) – M D Feb 06 '15 at 09:14
  • actually i want the json aray values in my list view and i have applied the the intent but its not wokring – androidcodes Feb 06 '15 at 09:18
  • Why have you set the adapter twice? – anu_r Feb 06 '15 at 09:23
  • sorry its a mistake actully it was commented in actual code i was trying to workout with simple adapter previously – androidcodes Feb 06 '15 at 09:26

2 Answers2

0

Change this line

 Intent in = new Intent();

To

Intent in = getActivity().getIntent();

I hope this one will help you :)

user3515851
  • 469
  • 1
  • 4
  • 14
0

Try this out:

In NewRecord class

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

In your fragment:

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


    }

Make sure your ArrayList is not empty. And one common mistake that I used to do was to set text color as white, so the contents of the listview was not actually visible. So please check that too.

anu_r
  • 1,602
  • 7
  • 30
  • 61