【问题标题】:To pass the values of json array in listview在 listview 中传递 json 数组的值
【发布时间】:2015-02-06 09:11:25
【问题描述】:

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

新记录类

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;

}
}

我想在我的片段的列表视图中返回列表。 正如我在编辑文本中添加的值一样,应该出现在我的片段类的列表中。

我的顶级片段类

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

【问题讨论】:

  • 实际上我想要列表视图中的 json aray 值并且我已经应用了意图但它没有工作
  • 为什么要设置两次适配器?
  • 很抱歉这是一个错误,实际上它是在我之前尝试使用简单适配器锻炼的实际代码中注释的

标签: android json listview fragment


【解决方案1】:

试试这个:

在 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 不为空。 我曾经犯的一个常见错误是将文本颜色设置为白色,因此列表视图的内容实际上是不可见的。所以也请检查一下。

【讨论】:

  • 你检查过这段代码是否有效??因为它没有给我任何回应
【解决方案2】:

改变这一行

 Intent in = new Intent();

Intent in = getActivity().getIntent();

希望这篇文章能帮到你:)

【讨论】:

  • 使用这一行 list_list.add( roll.getText().toString()) 而不是 list_list.add(rollno);
  • 问题是你在arraylist中添加了空值。所以只有它没有出现:)
  • 它也不起作用我已经尝试了所有方法或者我的方法是错误的\
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多