1

我想将资源文件夹(可绘制)的图像路径存储到 sqlite,然后检索它。我怎样才能做到这一点?

更新: 检索图像后,我在这里有一个按钮来重新排序/排序列表视图。这就是为什么我需要将图像路径存储到 sqlite。

这是我迄今为止尝试过的:

    static final int[] imgs = {
        R.drawable.dinaretreat, // 0
        R.drawable.cobterrace, // 1
        R.drawable.ventassostreet, // 2
        R.drawable.summerhillblvddrouin, // 3
        R.drawable.todmanstreetdrouin, // 4
        R.drawable.aqueductroad, // 5
        R.drawable.northroad, // 6
        R.drawable.pottsroad, // 7
        R.drawable.onemcclenaghanplace, // 8
        R.drawable.twomcclenaghanplace, // 9
        R.drawable.threemcclenaghanplace, // 10
        R.drawable.fourmcclenaghanplace, // 11
        R.drawable.fivemcclenaghanplace, // 12
        R.drawable.sevenmcclenaghanplace, // 13
        R.drawable.elevenplacemcclenaghanplace, // 14
        R.drawable.twelvemcclenaghanplace, // 15
        R.drawable.fivethreetwosummerhillblvddrouin, // 16
        R.drawable.seventeenajaxstreetdrouin, // 17
        R.drawable.onethirtythreemountainviewblvd, // 18
        R.drawable.fivethreeonesummerhillblvddrouin // 19
};


    String[] text, price;
ArrayList<String> priceList;

private DBHelper dbHelper;
Cursor cursor;

MyCustomAdapter adapter;

Button back, filter;
TextView highest, lowest, location;

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

    initControls();

    displayRecords();
}

// TODO displayRecords
private void displayRecords() {

    checkDatabaseConnection();

    text = dbHelper.getAll();
    price = dbHelper.getAllPrices();

    adapter = new MyCustomAdapter(imgs, text, price);

    lv.setAdapter(adapter);

}

private void initControls() {

    // TextViews
    highest = (TextView) findViewById (R.id.tvHighest);
    lowest = (TextView) findViewById (R.id.tvLowest);
    location = (TextView) findViewById (R.id.tvLocation);

    // Buttons
    filter = (Button) findViewById (R.id.btFilter);
    back = (Button) findViewById (R.id.btBack);
    back.setOnClickListener(this);
    filter.setOnClickListener(this);

    // ListView
    lv = (ListView) findViewById (R.id.lv);
    lv.setFastScrollEnabled(true);

    lv.setOnItemClickListener(this);

}

@Override
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
    String strHouseName = "house_name";
    String strHousePrice = "house_price";

    textview1 = (TextView) v.findViewById(R.id.text1);
        textview2 = (TextView) v.findViewById(R.id.text2);

        String strName = textview1.getText().toString().trim();
        String strPrice = textview2.getText().toString().trim();

    Intent i;

    i = new Intent(this, ViewHouse.class);
    i.putExtra(strHouseName, strName);
    i.putExtra(strHousePrice, strPrice);
    startActivity(i);


}

@SuppressLint("InlinedApi")
private void displayDialog() {
    // TODO displayDialog
    final ArrayAdapter<String> adp = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, sortBy);

    LayoutInflater li = LayoutInflater.from(this);
    View promptsView = li.inflate(R.layout.dialog_layout, null);

    promptsView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));

    final Spinner mSpinner= (Spinner) promptsView
            .findViewById(R.id.spDialog);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("Sort By...");
    builder.setIcon(R.drawable.launcher);

    mSpinner.setAdapter(adp);
    mSpinner.setSelection(0);
    mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

    public void onItemSelected(AdapterView<?> parent, View v,
            int pos, long id) {
        strSpinner = mSpinner.getSelectedItem().toString();

        if(strSpinner.equals("Highest Price")){
            highest.setTypeface(Typeface.DEFAULT_BOLD);
            lowest.setTypeface(Typeface.DEFAULT);
            location.setTypeface(Typeface.DEFAULT);
            price = dbHelper.sortHighestPrice();

            adapter = new MyCustomAdapter(imgs, text, price);
            lv.setAdapter(adapter);
            adapter.notifyDataSetChanged();

        } else if (strSpinner.equals("Lowest Price")){
            highest.setTypeface(Typeface.DEFAULT);
            lowest.setTypeface(Typeface.DEFAULT_BOLD);
            location.setTypeface(Typeface.DEFAULT); 
            price = dbHelper.sortLowestPrice();

            adapter = new MyCustomAdapter(imgs, text, price);
            lv.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        } else if (strSpinner.equals("Location")) {
            highest.setTypeface(Typeface.DEFAULT);
            lowest.setTypeface(Typeface.DEFAULT);
            location.setTypeface(Typeface.DEFAULT_BOLD);
        } else {
            Log.d("Default", "Default");
        }
    }

    public void onNothingSelected(AdapterView<?> arg0) {

    }
    });

    builder.setPositiveButton("Okay",
            new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    });
    builder.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    });

    builder.setView(promptsView);
    AlertDialog alert = builder.create();
    alert.show();


} 

    class MyCustomAdapter extends BaseAdapter
{
    String[] data_text1;
    String[] data_text2;
    int[] data_image;

MyCustomAdapter() {
    data_text1 = null;
    data_text2 = null;
    data_image = null;
}

MyCustomAdapter(int[] image, String[] house, String[] price) {
    data_text1 = house;
    data_text2 = price;
    data_image = image;
}

public int getCount() {
    return data_text1.length;
}

public String getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return position;
}

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

    LayoutInflater inflater = getLayoutInflater();
    View row;

    row = inflater.inflate(R.layout.listrow, null);

    textview1 = (TextView) row.findViewById(R.id.text1);
    textview2 = (TextView) row.findViewById(R.id.text2);
    ImageView imageview = (ImageView) row.findViewById(R.id.image);

    imageview.setScaleType(ImageView.ScaleType.FIT_XY);
    textview1.setText(data_text1[position]);
    String strPrice = "$" + (new DecimalFormat("#,###.00")).format(Integer.parseInt(data_text2[position])) ;
    textview2.setText(strPrice);
    imageview.setImageResource(data_image[position]);

    return (row);

    }
}

在这里,我已经声明了这些可绘制对象。如何将它们的路径保存到 sqlite,然后将它们显示在我的自定义列表视图上?有任何想法吗?非常感谢您的帮助。谢谢。

4

1 回答 1

0

由于 in 中的资源res是常量,因此无需将它们存储在数据库中!相反,只需从资源数组中加载可绘制对象:

在文件中定义一个数组,例如res/values/images.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="list_images">
        <item>@drawable/dinaretreat</item>
        <item>@drawable/cobterrace</item>
        <item>@drawable/ventassostreet</item>
        <item>@drawable/summerhillblvddrouin</item>
        <item>@drawable/todmanstreetdrouin</item>
        <item>@drawable/aqueductroad</item>
        <item>@drawable/northroad</item>
        <item>@drawable/pottsroad</item>
        <item>@drawable/onemcclenaghanplace</item>
        <item>@drawable/twomcclenaghanplace</item>
        <item>@drawable/threemcclenaghanplace</item>
        <item>@drawable/fourmcclenaghanplace</item>
        <item>@drawable/fivemcclenaghanplace</item>
        <item>@drawable/sevenmcclenaghanplace</item>
        <item>@drawable/elevenplacemcclenaghanplace</item>
        <item>@drawable/twelvemcclenaghanplace</item>
        <item>@drawable/fivethreetwosummerhillblvddrouin</item>
        <item>@drawable/seventeenajaxstreetdrouin</item>
        <item>@drawable/onethirtythreemountainviewblvd</item>
        <item>@drawable/fivethreeonesummerhillblvddrouin</item>
    </array>
</resources>

在您的Adapter中,获取资源数组,并对其进行索引:

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = getLayoutInflater();
    View row = inflater.inflate(R.layout.listrow, null);

    textview1 = (TextView) row.findViewById(R.id.text1);
    textview1.setText(data_text1[position]);

    textview2 = (TextView) row.findViewById(R.id.text2);
    String strPrice = "$" + (new DecimalFormat("#,###.00")).format(Integer.parseInt(data_text2[position])) ;
    textview2.setText(strPrice);

    ImageView imageview = (ImageView) row.findViewById(R.id.image);
    imageview.setScaleType(ImageView.ScaleType.FIT_XY);
    imageview.setImageResource((context.getResources().getIntArray())[position]);

    return (row);
}
于 2013-10-31T08:10:53.177 回答