如何在android中的listview中添加新的listitem

时间:2015-08-20 07:11:40

标签: java android listview

我有一个5行的listView。我想在用户点击列表中的任何行时添加新行。我使用过onItemClickListner。我应该用onItemClick方法写什么?

这是我单行的xml代码

single_row.xml

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="7dp"
        android:src="@drawable/meme5" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/imageView1"
        android:layout_toRightOf="@id/imageView1"
        android:text="meme1" />

    <TextView
        android:id="@+id/age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_alignLeft="@+id/name"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/name"
        android:text="Age" />

</RelativeLayout>

和activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.AbhayWork.listview_customadaptor.MainActivity" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>

这是我的java代码

MainActivity.java

package com.example.listview_addrow;

import java.util.ArrayList;
import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class MainActivity extends Activity implements OnItemClickListener{

    ListView list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list=(ListView) findViewById(R.id.listView);
        list.setAdapter(new customAdapter(this));

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub


    }
}

class singlerow {
    String name;
    String age;
    int image;


    // TODO Auto-generated constructor stub
    public singlerow(String name, String age,int image) {
        this.name = name;
        this.age = age;
        this.image=image;
        // TODO Auto-generated constructor stub
    }

}

class customAdapter extends BaseAdapter {
    ArrayList<singlerow> list;
    Context context;

    public customAdapter(Context c) {
        context = c;

        list = new ArrayList<singlerow>();
        Resources res = c.getResources();
        String[] names = res.getStringArray(R.array.nameArray);
        String[] ages = res.getStringArray(R.array.ageArray);
        int[] images={R.drawable.meme1,R.drawable.meme2,R.drawable.meme3,R.drawable.meme4,R.drawable.meme5,R.drawable.meme6,R.drawable.meme7,R.drawable.meme8,R.drawable.meme9,R.drawable.meme10};



        for (int i = 0; i < 5; i++) {

            list.add(new singlerow(names[i], ages[i],images[i]));

        }
        // TODO Auto-generated constructor stub
    }



    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        // TODO Auto-generated method stub
        return list.get(i);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    class myViewHolder
    {
        TextView myText1;
        TextView myText2;
        ImageView myImage;

        public myViewHolder(View v) {
            // TODO Auto-generated constructor stub
            myText1=(TextView) v.findViewById(R.id.name);
            myText2=(TextView) v.findViewById(R.id.age);
            myImage=(ImageView) v.findViewById(R.id.imageView1);
        }

    }

     @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
         View row=convertView;
         myViewHolder holder=null;
         if(row==null)
         {
             LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             row = inflater.inflate(R.layout.single_row, parent, false);
             holder=new myViewHolder(row);
             row.setTag(holder);
             Log.d("ViewHolder", "Creating a new row");     
         }
         else
         {
             holder=(myViewHolder) row.getTag();
             Log.d("ViewHolder", "Recycling");
         }      
         singlerow temp = list.get(position);

        holder.myText1.setText(temp.name);
        holder.myText2.setText(temp.age);
        holder.myImage.setImageResource(temp.image);


        return row;
    }


}

1 个答案:

答案 0 :(得分:0)

您应该在adpater列表和通话notifyDataSetChanged中添加项目。

为此,你应该在你的活动类中有一个适配器的字段:

private CustomAdapter customAdapter;

然后在你onItemClickMethod你可以做到:

customAdapter.list.add(new SingleRow("name", "age", "image"));
customAdapter.notifyDataSetChanged();