如何在ListView中显示名称,标题,但在SingleListItem中只显示Employee No.?

时间:2014-11-28 14:42:16

标签: android listview listitem

我已成功生成一个ListView,它从远程MySQL数据库获取JSON数据,并将3个字段(员工编号,员工姓名和标题)添加到名为的输出字符串中 "员工&#34 ;. 这显示在Android手机上,好的,但我真正想要做的只是显示员工 名称和标题 - 然后当单击列表中的此记录时,我想显示 只有员工在下一个活动中没有。 使用拆分字符串,我设法只显示"员工否#34;在下一个活动中,但那 仍然留下仅在ListView中显示员工姓名+标题的问题。 如果我只在输出字符串中将这两个字段一起添加,那么"员工否#34;将会 &#34不可见#34;当单个项目点击.. 提前谢谢了! 这是我的MainActivity中的相关代码..

    for (int i = 0; i < jsonMainNode.length(); i++) {
    JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
    String name = jsonChildNode.optString("employee name");
    String number = jsonChildNode.optString("employee no");
    String title = jsonChildNode.optString("title");
    String outPut = name + "-" + title + "-" + number;
    employeeList.add(createEmployee("employees", outPut)); 
   }

  } catch (JSONException e) {
   Toast.makeText(getApplicationContext(), "Error" + e.toString(),
     Toast.LENGTH_SHORT).show();
  }

  SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList,
    android.R.layout.simple_list_item_activated_1,
    new String[] { "employees" }, new int[] { android.R.id.text1 });
  listView.setAdapter(simpleAdapter);
 }

1 个答案:

答案 0 :(得分:0)

您必须创建一个Employee对象,它有三个字段

private String emplyeeName;
private String employeeTitle;
private String employeeNum;

然后使用从服务器返回的数据填充ArrayList,并使用自定义适配器填充它。 这样您就可以管理列表视图项中应该出现的内容。

希望它会有所帮助。如果有不明显的事情,请不要犹豫。 :)

编辑1:

以下是一个例子:

MainActivity中onCreate中的代码

final ArrayList<Employee> arr = new ArrayList<Employee>();

    final ListView lv = (ListView) findViewById(R.id.listView);
    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

            String EmployeeNum = arr.get(position).getEmployeeNum();

            Intent i = new Intent(MainActivity.this, SecondActivity.class);
            i.putExtra("EmployeeNum", EmployeeNum);
            startActivity(i);
        }
    });


    CustomAdapter adapter = new CustomAdapter(this , R.layout.activity_main , arr);
    lv.setAdapter(adapter);

自定义适配器

public class CustomAdapter extends ArrayAdapter<Employee> {

ArrayList<Employee> Info;
private Context context;

CustomAdapter(Context context, int resource, ArrayList<Employee> objects) {
    super(context, resource, objects);
    // TODO Auto-generated constructor stub
    this.context = context;
    Info = objects;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    EmployeeItemHolder employeeHolder;
    View view = convertView;
    if (view == null) {
        employeeHolder = new EmployeeItemHolder();
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        view = inflater.inflate(R.layout.custom_row, parent, false);

        employeeHolder.title = (TextView) view.findViewById(R.id.tvtextView);


        view.setTag(employeeHolder);
    } else {
        employeeHolder = (EmployeeItemHolder) view.getTag();

    }

    Employee eItem = (Employee) this.Info.get(position);
    employeeHolder.title.setText(eItem.getEmployeeTitle()+"-"+eItem.getEmplyeeName());

    return view;
}

private static class EmployeeItemHolder {
    TextView title;
}
}

custom_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/tvtextView" />
</LinearLayout>

编辑2:

要与此代码集成,请转换您的employeeList

ArrayList<String> 

ArrayList<Employee>

以下是一个例子:

ArrayList<Employee> employeeList = new ArrayList<Employee>();
    for (int i = 0; i < jsonMainNode.length(); i++) {
        JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
        Employee emp = new Employee();
        emp.setEmplyeeName(jsonChildNode.optString("employee name"));
        emp.setEmployeeTitle(jsonChildNode.optString("title"));
        emp.setEmployeeNum(jsonChildNode.optString("employee no"));

        employeeList.add(emp); 
    }

} catch (JSONException e) {
    Toast.makeText(getApplicationContext(), "Error" + e.toString(),
            Toast.LENGTH_SHORT).show();
}
CustomAdapter adapter = new CustomAdapter(this, R.layout.activity_main, employeeList );
listView.setAdapter(adapter);

希望“能帮到你”