2017-04-21 6 views
0

から画像名を取得は問題の表示名 はありませんが、私はリストビュー でそれらを表示したい名前 をしかし、写真を表示する際に問題があります。解決策は何ですか?は、私はデータベースを持っていると私は2つの列の最初の列の 名前持ってsqliteの

Adbterリストビュー

public class adbter_listview extends ArrayAdapter<String> { 

    Activity activity; 
    ArrayList<Integer> icons; 
    ArrayList<String> name; 

    public adbter_listview(Activity activity, ArrayList<Integer> icons, ArrayList<String> name) { 
     super(activity, R.layout.custom_listview, name); 
     this.activity = activity; 
     this.icons = icons; 
     this.name = name; 
    } 

    @NonNull 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = activity.getLayoutInflater(); 
     View v = inflater.inflate(R.layout.custom_listview, parent, false); 
     ImageView iv = (ImageView) v.findViewById(R.id.imageView); 
     TextView tv = (TextView) v.findViewById(R.id.textView); 
     iv.setImageResource(icons.get(position)); 
     tv.setText(name.get(position)); 
     return v; 
    } 
} 

MainActivity

public class MainActivity extends AppCompatActivity { 

    ListView con; 
    ArrayList<String> icons_name = new ArrayList<>(); 
    ArrayList<Integer> icons = new ArrayList<>(); 
    ArrayList<String> name = new ArrayList<>(); 

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

     con = (ListView) findViewById(R.id.con); 


     db connect = new db(MainActivity.this); 
     SQLiteDatabase read = connect.getReadableDatabase(); 
     Cursor save = read.rawQuery("select image from game",null); 
     save.moveToFirst(); 

     while(save.isAfterLast()==false) 
     { 
      icons_name.add(save.getString(0)); 
      icons.add(R.drawable.user); 
      save.moveToNext(); 
     } 


     adbter_listview adb = new adbter_listview(MainActivity.this,icons,name); 
     con.setAdapter(adb); 

    } 

} 

私を助けてください

+0

「写真を表示するには問題がありますか?」という問題はありますか?どうしましたか? –

+0

あなたのカラム名はImgとNameですが、あなたは 'image'を選択していますか?また、あなたのデータベースのものではなく、あなたのアイコンのarraylistにdrawableリソースを追加しています。 –

答えて

0

取得INTこの行を使用して描画可能のID、あなたのケースに

int drawableResourceId = this.getResources().getIdentifier("nameOfDrawable", "drawable", this.getPackageName()); 

nameOfDrawable = save.getString(0);私は

が、その後

icons.add(drawableResourceId); 

が、これは動作しますホープ、アイコンのArrayListに追加しますね。

+0

は動作しません:(//////// while(save.isAfterLast()== false){String nameOfDrawable = save.getString(0); getIdentifier(); getIdentifier(); getIdentifier( "nameOfDrawable"、 "drawable"、this.getPackageName()); icons_name.add(nameOfDrawable); icons.add(Integer.parseInt(save.getString(0))); saveMoveToNext();} –

+0

あなたは私を助けることができます:) –

+0

nameOfDrawableは文字列ではいけません、そこに変数名を入れてください。 write、drawableResourceId = this.getResources()。getIdentifier(nameOfDrawable、 "drawable"、this.getPackageName()); – Exigente05

0

代わりのアダプタでこの

icons.add(R.drawable.user); 

使用

icons.add(Integer.parseInt(save.getString(1))); 

それは画像がドローアブルフォルダからある場合にのみ動作し

R.drawable.icons.get(position) 

のようにそれを使用しかし、最初に削除する必要がありますoncreate mainThread呼び出しから別のスレッドへのデータベース呼び出し。新しいスレッドを作成するか、asynctaskを使用するか、ローダーを使用してください。 mainthreadでsqlitedatabase呼び出しを作成しないでください。

+0

動作しない:(一方////////(偽save.isAfterLast()==) {文字列nameOfDrawable = save.getString(0); INT drawableResourceId = this.getResources()getIdentifier(」。 icons_name.add(nameOfDrawable); icons.add(Integer.parseInt(save.getString(0))); を保存します。moveToNext(); } –

+0

私を助けることができます:) –

+0

は、ドロウアブルフォルダ内のすべての画像ですか?では、なぜsqlite dbからそれを取得していますか?どんなプロジェクトでも、あなたは学んでいますか? – Ari

関連する問題