在列表视图中永久保存新输入的数据

时间:2014-08-03 14:32:10

标签: java android android-listview android-arrayadapter

您好我制作了像olx这样的销售购买应用。在哪个用户发布免费添加。问题是当使用帖子添加它出现在listview中但是当用户再次使用他们的设备登录时,添加将消失.....

public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    View rowView = inflater.inflate(R.layout.single_adds_layout, parent, false);
    TextView title = (TextView) rowView.findViewById(R.id.title);
    TextView detailedMessage = (TextView) rowView.findViewById(R.id.detailed_message);
    TextView ownerName = (TextView) rowView.findViewById(R.id.owner);
    TextView ownerEmail = (TextView) rowView.findViewById(R.id.owner_email);
    TextView price = (TextView) rowView.findViewById(R.id.price);
    ImageView image = (ImageView) rowView.findViewById(R.id.imageView10);
    Button btn33 = (Button) rowView.findViewById(R.id.buybutton);
    btn33.setBackgroundResource(R.drawable.buy_corner);
    btn33.setOnClickListener(this);

    Advertisement dataObj = values[position];
    title.setText(dataObj.getTitle());
    detailedMessage.setText(dataObj.getDetailedMessage());
    ownerName.setText(dataObj.getOwnerName());
    ownerEmail.setText(dataObj.getOwnerEmail());
    price.setText(String.valueOf(dataObj.getPrice()));
    btn33.setTag(price.getText());
    if(dataObj.getImageId().contains(">>>")){
        String picturePath = dataObj.getImageId().replace(">>>", ""); 
        image.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }else {
        setDrawable(image, dataObj.getImageId());
    }

    return rowView;

}

private void setDrawable(ImageView image, String drawableName) {
    AssetManager manager = image.getContext().getAssets();

    // Read a Bitmap from Assets
    InputStream open = null;
    try {
        open = manager.open(drawableName+".jpg");
        Bitmap bitmap = BitmapFactory.decodeStream(open);
        // Assign the bitmap to an ImageView in this layout
        image.setImageBitmap(bitmap);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (open != null) {
            try {
                open.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } 
}

这是我的Xml

  <LinearLayout
        android:id="@+id/parentLinear"
        android:layout_width="match_parent"
        android:layout_height="410dp"
        android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="66dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="Ad Title!"
            android:textColor="#000000"
            android:textSize="15sp"
            android:textStyle="bold|italic"
            android:typeface="serif" />

        <EditText
            android:id="@+id/editTextTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textView1"
            android:layout_alignBottom="@+id/textView1"
            android:layout_alignParentRight="true"
            android:ems="10"
            android:hint="Enter Title Here" >

            <requestFocus />
        </EditText>

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="66dp" >

        <EditText
            android:id="@+id/editTextDescription"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:ems="10"
            android:hint="Enter Description Here" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="Ad\nDescription!"
            android:textColor="#000000"
            android:textSize="15sp"
            android:textStyle="bold|italic"
            android:typeface="serif" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="62dp" >

        <EditText
            android:id="@+id/editTextOwnerName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:ems="10"
            android:hint="Enter Name Here" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/editTextOwnerName"
            android:layout_alignParentLeft="true"
            android:text="Owner\nName!"
            android:textColor="#000000"
            android:textSize="15sp"
            android:textStyle="bold|italic"
            android:typeface="serif" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="64dp" >

        <EditText
            android:id="@+id/editTextOwnerEmail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:ems="10"
            android:hint="Enter Email id Here" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="Owner\nEmail"
            android:textColor="#000000"
            android:textSize="15sp"
            android:textStyle="bold|italic"
            android:typeface="serif" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="62dp" >

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="Ad Price!"
            android:textColor="#000000"
            android:textSize="15sp"
            android:textStyle="bold|italic"
            android:typeface="serif" />

        <EditText
            android:id="@+id/editTextPrice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:ems="10"
            android:hint="Enter Price Here" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dp" >

        <Button
            android:id="@+id/buttonAddImage"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="Add Image"
            android:textStyle="bold|italic"
            android:typeface="serif" />

        <ImageView
            android:id="@+id/creatae"
            android:layout_width="100sp"
            android:layout_height="100sp"
            android:layout_alignParentBottom="true"
            android:src="@drawable/ic_launcher" />
    </RelativeLayout>

    <Button
        android:id="@+id/buttonSaveAdd"
        android:layout_width="68dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Save"
        android:textStyle="bold|italic"
        android:typeface="serif" />

    </LinearLayout>

点击“保存添加”

case R.id.buttonSaveAdd:
        EditText editTextTitle = (EditText) findViewById(R.id.editTextTitle);
        EditText editTextDes = (EditText) findViewById(R.id.editTextDescription);
        EditText editTextOwner = (EditText) findViewById(R.id.editTextOwnerName);
        EditText editTextOwnerEmail = (EditText) findViewById(R.id.editTextOwnerEmail);
        EditText editTextPrice = (EditText) findViewById(R.id.editTextPrice);

        //populating data object from the values received 
        //from view
        String title = editTextTitle.getText().toString(); 
        String description = editTextDes.getText().toString();
        String ownerName = editTextOwner.getText().toString();
        String ownerEmail = editTextOwnerEmail.getText().toString();
        String pricce = editTextPrice.getText().toString();
        Advertisement  object = new Advertisement(title, description,
                ownerName, ownerEmail, currentImageName, Integer.parseInt(pricce), 100);
        Button_mak.ads.add(object);

        this.finish();
        break;

1 个答案:

答案 0 :(得分:0)

使用SQLite将它们保存在本地数据库中,然后在用户再次打开该活动时将它们重新加载到列表视图中。我最近发表了一篇关于在Android中使用SQLite的博客文章。可以找到here