在Android

时间:2016-04-19 16:36:42

标签: java android listview

我有一个自定义动态列表视图,在页面顶部,我有一个按钮可以无限地向listview添加项目:

代码

 btnadd.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            note = new StructNote();
            note.title = " edit your title ";
            note.description = "";
            G.notes.add(note);

            adapter.notifyDataSetChanged();

        }
    });
  

1-我如何排序这个列表视图(最后创建的项目是第一个   创建项目)

     

2-如何在我的第一页中设置按钮,以便用户可以打开   最后一页打开的列表视图项目(第一页)(快捷方式)。

这是我的适配器注释

public class AdapterNote extends ArrayAdapter<StructNote> {

    public AdapterNote(ArrayList<StructNote> array) {
        super(G.context, R.layout.adapter_notes, array);
    }

    static Typeface myfont = Typeface.createFromAsset(G.context.getAssets(), "homa.ttf");


    private static class ViewHolder {

        public Button      btnedit;
        public ViewGroup   layoutRoot;
        public TextView    txtTitle;
        public TextView    txtDescription;
        public ImageView   imgDelete;


        public ViewHolder(View view) {
            radio = (RadioButton) view.findViewById(R.id.radio);

            btnedit = (Button) view.findViewById(R.id.btnedit);
            txtTitle = (TextView) view.findViewById(R.id.txtTitle);
            txtDescription = (TextView) view.findViewById(R.id.txtDescription);
            layoutRoot = (ViewGroup) view.findViewById(R.id.layoutRoot);
            imgDelete = (ImageView) view.findViewById(R.id.imgDelete);
        }


        public void fill(final ArrayAdapter<StructNote> adapter, final StructNote item, final int position) {
            txtTitle.setText(item.title);
            txtDescription.setText(item.description);
            txtTitle.setTypeface(myfont);
            txtDescription.setTypeface(myfont);
            btnedit.setTypeface(myfont);
            adapter.getItem(position);
            G.selectedItemPositon = position;

            imgDelete.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    adapter.remove(item);

                }
            });

            btnedit.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(G.currentActivity, ActivityEdit.class);
                    intent.putExtra("POSITION", position);
                    G.currentActivity.startActivity(intent);
                }
            });
            layoutRoot.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Intent intent = new Intent(G.currentActivity, CounterActivity.class);
                    intent.putExtra("POSITION", position);
                    G.selectedItemPositon = position;
                    G.currentActivity.startActivity(intent);
                }
            });

        }

    }


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

        StructNote item = getItem(position);
        if (convertView == null) {
            convertView = G.inflater.inflate(R.layout.adapter_notes, parent, false);
            holder = new ViewHolder(convertView);

            convertView.setTag(holder);

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

        }

        holder.fill(this, item, position);

        return convertView;

    }

}

G class

public class G extends Application {

    public static Context               context;
    public static LayoutInflater        inflater;
    public static Activity              currentActivity;
    public static ArrayList<StructNote> notes = new ArrayList<StructNote>();
    public static SharedPreferences     preferences;
    public static int                   selectedItemPositon;


    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        preferences = PreferenceManager.getDefaultSharedPreferences(context);
    }
}

这是第一页我的按钮

 btnlastz.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            if (G.notes.size() == 0) {

                Intent ii = new Intent(Firstpage.this, ActivityMain.class);
                Firstpage.this.startActivity(ii);

            } else {
                Intent zz = new Intent(Firstpage.this, CounterActivity.class);

                zz.putExtra("position", ActivityMain.note.Turn);

                Firstpage.this.startActivity(zz);
            }

        }
    });

1 个答案:

答案 0 :(得分:0)

  

1-我如何排序这个列表视图(最后创建的项目是第一个   创建项目)

这是一个简单的例子

List items = new ArrayList();
//some fictitious objectList where we're populating data
for(Object obj : objectList) {
// THE KEY IS TO ADD THE ITEM AT POITIN 0

    items.add(0, obj);
    listAdapter.notifyDataSetChanged();
}
//THIS WILL REFRESH THE LISTVIEW
listView.post(new Runnable() {
    @Override
    public void run() {
        listView.smoothScrollToPosition(0);
    }
}
  

2-如何在我的第一页中设置按钮,以便用户可以打开   最后一页打开的列表视图项目(第一页)(快捷方式)。

要实现此目的,您需要保存上次打开的项目的位置,然后从该按钮单击获取位置。为了使这个过程变得简单,我建议你使用我个人最喜欢的TinyDB,快速简便,

示例:在项目中包含该类,然后

 // Save the position of item clicked here
layoutRoot.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
    //JUST TWO LINES
    TinyDB tinydb = new TinyDB(mContext);
    tinydb.putInt("position", position);
    Intent intent = new Intent(G.currentActivity, CounterActivity.class);
    intent.putExtra("POSITION", position);
    G.selectedItemPositon = position;
    G.currentActivity.startActivity(intent);
    }
    });

然后点击按钮

    int position;
    TinyDB tinydb = new TinyDB(mContext);
    tinydb.getInt("position", position);
    Intent intent = new Intent(G.currentActivity, CounterActivity.class);
    intent.putExtra("POSITION", position);
    startActivity(intent);