如何使用Plist中的字幕创建TableViewCell

时间:2016-03-30 08:47:25

标签: arrays uitableview dictionary plist tableviewcell

我想使用TableViewCell创建title and subtitle cell plist,标题和字幕值。<plist version="1.0"> <dict> <key>Stations</key> <array> <dict> <key>Title</key> <string>F1</string> <key>Subtitle</key> <string>F1 A</string> </dict> <dict> <key>Title</key> <string>F2</string> <key>Subtitle</key> <string>F2 A</string> </dict> <dict> <key>Title</key> <string>F3</string> <key>Subtitle</key> <string>F3 A</string> </dict> </array> <key>Service center</key> <array> <dict> <key>Title</key> <string>F4</string> <key>Subtitle</key> <string>F4 A</string> </dict> <dict> <key>Title</key> <string>F5</string> <key>Subtitle</key> <string>F5 A</string> </dict> </array> </dict> </plist> 。需要哪种方法?怎么做?

自最近开始学习目标。有人可以帮我从这里出去吗?谢谢

plist中:

public class ListViewAdapter extends BaseAdapter {

private  Context mContext;
private LayoutInflater mLayoutInflater;
DatabaseHelper mydb;
int pos = 0;
enter code here
public ArrayList<Employee_info> mArrayList = new ArrayList<Employee_info>();


public ListViewAdapter(Context context,ArrayList<Employee_info> arrayList){
    mContext=context;
    mArrayList=arrayList;
    mLayoutInflater=LayoutInflater.from(mContext);

    mydb = new DatabaseHelper(mContext);
}
enter code here
@Override
public int getCount() {
    return mArrayList.size();
}
enter code here
@Override
public Object getItem(int position) {
    return mArrayList.get(position);
}

enter code here
@Override
public long getItemId(int position) {
    return position;
}
enter code here

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder=new ViewHolder();
    View view=convertView;

    if(view == null){
        //view = mLayoutInflater.inflate(R.layout.list_view,parent,false);
        view = mLayoutInflater.inflate(R.layout.list_view,null);
        holder.txtName = (TextView)view.findViewById(R.id.txtName);
        holder.txtCode = (TextView)view.findViewById(R.id.txtCode);
        holder.txtStatus = (TextView)view.findViewById(R.id.txtStatus);
        //// Start //////////////

        Button b1 = (Button) view.findViewById(R.id.button1);
        Button b2 = (Button) view.findViewById(R.id.button2);
        Button b3 = (Button) view.findViewById(R.id.button3);

        b1.setTag(position);
        final Employee_info emp_info = mArrayList.get(position);
        b2.setTag(position);
        b3.setTag(position);


        final ViewHolder finalHolder = holder;
        b3.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int x = (int) v.getTag();
                       /* if (itemPos.contains(v)) {

                            finalHolder.txtCode.setTextColor(Color.RED);
                            finalHolder.txtName.setTextColor(Color.RED);
                            finalHolder.txtStatus.setTextColor(Color.RED);
                        }
                        else {*/
                        if (mArrayList.get(x).getGet_status() != null) {
                            if (mArrayList.get(x).getGet_status().equals("Unpaid")) {
                                String upd1 = mArrayList.get(x).getGet_code();
                                String upd2 = mArrayList.get(x).getGet_name();
                                String upd3 = "Paid";
                                mydb.UpdateData(upd1, upd2, upd3);

                                // THis code for only catch Month, year, Employee ID, Status
                                Calendar cal=Calendar.getInstance();
                                SimpleDateFormat month_date = new SimpleDateFormat("MMM");
                                String month_name = month_date.format(cal.getTime());
                                int thisYear = Calendar.getInstance().get(Calendar.YEAR);
                                mydb.insertEmpSalary(upd1, month_name, String.valueOf(thisYear), upd3);

                                finalHolder.txtStatus.setTextColor(Color.GREEN);
                                ListViewAdapter.this.notifyDataSetChanged();

                            } else {
                                String upd1 = mArrayList.get(x).getGet_code();
                                String upd2 = mArrayList.get(x).getGet_name();
                                String upd3 = "Unpaid";
                                mydb.UpdateData(upd1, upd2, upd3);
                                finalHolder.txtStatus.setTextColor(Color.RED);
                            }
                        }
                    }
                    //}
                });

        b1.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(final View position) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                        builder.setTitle("Confirm");
                        builder.setMessage("Are you sure?");
                        builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                try
                                {
                                    pos = (int) position.getTag();
                                    mydb.deleteData(mArrayList.get(pos).getGet_code());
                                    mArrayList.remove(pos);
                                    ListViewAdapter.this.notifyDataSetChanged();
                                    Toast.makeText(mContext, mArrayList.get(pos).getGet_code() + " Employee is Delete", Toast.LENGTH_LONG).show();
                                }
                                catch (Exception e)
                                {
                                    e.printStackTrace();
                                }
                                dialog.dismiss();
                            }
                        });
                        builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                        AlertDialog alert = builder.create();
                        alert.show();
                    }
                });

        b2.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AlertDialog.Builder alert = new AlertDialog.Builder(mContext);
                        alert.setTitle("Alert Dialog With EditText"); //Set Alert dialog title here
                        alert.setMessage("Enter Your Code");

                        pos = (int) v.getTag();
                        LinearLayout layout = new LinearLayout(mContext);
                        layout.setOrientation(LinearLayout.VERTICAL);

                        final EditText input = new EditText(mContext);
                        input.setText(mArrayList.get(pos).getGet_code());
                        layout.addView(input);

                        final EditText input1 = new EditText(mContext);
                        input1.setText(mArrayList.get(pos).getGet_name());
                        layout.addView(input1);

                        final EditText input2 = new EditText(mContext);
                        input2.setText(mArrayList.get(pos).getGet_status());
                        layout.addView(input2);

                        alert.setView(layout);

                        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                try
                                {

                                    String srt = input.getEditableText().toString();
                                    Log.d("ccccccc", srt);
                                    String upd1 = input.getText().toString();
                                    String upd2 = input1.getText().toString();
                                    String upd3 = input2.getText().toString();

                                    /*String upd1 =  mArrayList.get(pos).getGet_code();
                                    String upd2 =  mArrayList.get(pos).getGet_name();
                                    String upd3 =  mArrayList.get(pos).getGet_status();
                                    Log.d("Code1",mArrayList.get(pos).getGet_code());
                                    Log.d("Name1",mArrayList.get(pos).getGet_name());
                                    Log.d("Status1",mArrayList.get(pos).getGet_status());*/
                                    Log.d("Code",upd1);
                                    Log.d("Name", upd2);
                                    Log.d("Status", upd3);


                                    boolean isUpdate = mydb.UpdateData(upd1, upd2, upd3);
                                    ListViewAdapter.this.notifyDataSetChanged();
                                    notifyDataSetChanged();


                                    if (isUpdate == true)
                                    {
                                        Log.e("Update Complete", String.valueOf(isUpdate));
                                        ListViewAdapter.this.notifyDataSetChanged();
                                        Toast.makeText(mContext, srt + " Employee is Updated", Toast.LENGTH_LONG).show();
                                    }
                                    else
                                    {
                                        Toast.makeText(mContext, srt + " Employee is not Updated", Toast.LENGTH_LONG).show();
                                    }
                                }
                                catch(Exception e)
                                {
                                    e.printStackTrace();
                                }
                                dialog.dismiss();
                            }
                        });
                        alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                dialog.dismiss();
                            }
                        });
                        AlertDialog alertDialog = alert.create();
                        alertDialog.show();
                    }
                });


        view.setTag(holder);

    } else {
        holder=(ViewHolder)view.getTag();
    }

    Employee_info employee_info = mArrayList.get(position);
    holder.txtName.setText(employee_info.getGet_name());
    holder.txtCode.setText(employee_info.getGet_code());
    holder.txtStatus.setText(employee_info.getGet_status());
    return view;
}

private class ViewHolder{
    private TextView txtName,txtCode,txtStatus;
    public Button b1;
}
}

1 个答案:

答案 0 :(得分:0)

试试这里的例子: http://www.ios-blog.co.uk/tutorials/how-to-populate-a-uitableview-from-a-plist-property-list/

您的plist在根目录中包含dict,因此要从plist中检索数据,您应该使用:

NSDictionary *dictFromPlist = [[NSDictionary alloc] initWithContentsOfFile:

然后使用(你应该为这些数组创建两个属性)来获取两个数组:

self.stationsArray = dictFromPlist[@"Stations"];
self.serviceCenterArray = dictFromPlist[@"Service center"];

无论如何,看看你如何继续下去。这是一个非常常见的用例,因此有很多好的信息,例如: iOS Populating UITableView with Data from Plist