2016-03-16 35 views
-2

我正在從本網站的工作下載圖像和從Web和惰性負載ListView - http://androidexample.com/Download_Images_From_Web_And_Lazy_Load_In_ListView_-_Android_Example/index.php?view=article_discription&aid=112&aaid=134從ListView中的String.xml獲取字符串數組

但是,我想自定義列表視圖上的textview。我希望我的textview從string.xml中提取字符串而不是代碼holder.text.setTexr("Title " + position)

請幫助我,因爲我是初學者。我很感謝。謝謝。

XML:

<string-array name="titles"> 
    <item>1</item> 
    <item>2</item> 
    <item>3</item> 
    <item>4</item> 
    <item>5</item> 
    <item>6</item> 
    <item>7</item> 
    <item>8</item> 
    <item>9</item> 
    <item>10</item> 
    <item>11</item> 
    <item>12</item> 
</string-array> 

這是我的主要活動

public class IngredientCategoryMain extends Activity { 

ListView list; 
CategoryImageAdapter adapter; 
ArrayAdapter arrayAdapter; 



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

    list=(ListView)findViewById(R.id.listView); 
    adapter=new CategoryImageAdapter(this, mStrings); 
    list.setAdapter(adapter); 

} 

@Override 
public void onDestroy() { 
    list.setAdapter(null); 
    super.onDestroy(); 
} 

public View.OnClickListener listener=new View.OnClickListener() { 
    @Override 
    public void onClick(View arg0) { 
     adapter.imageLoader.clearCache(); 
     adapter.notifyDataSetChanged(); 


    } 
}; 

public void onItemClick(int mPosition) { 
    String tempValues = mTitles[mPosition]; 
    Toast.makeText(IngredientCategoryMain.this, tempValues, Toast.LENGTH_LONG).show(); 
} 

private String[] mStrings={ 
     "https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Ic_cake_48px.svg/2000px-Ic_cake_48px.svg.png", 
     "https://pixabay.com/static/uploads/photo/2013/04/01/21/30/can-99137_960_720.png", 
     "http://publicdomainvectors.org/photos/Gerald_G_Fast_Food_Drinks_(FF_Menu)_9.png", 
     "https://pixabay.com/static/uploads/photo/2014/03/25/16/59/apple-297775_960_720.png", 
     "https://pixabay.com/static/uploads/photo/2012/04/16/11/14/mortar-35544_960_720.png", 
     "https://pixabay.com/static/uploads/photo/2013/07/13/10/05/cattle-156498_960_720.png", 
     "https://pixabay.com/static/uploads/photo/2013/07/12/15/39/acorn-150258_960_720.png", 
     "http://publicdomainvectors.org/photos/johnny_automatic_bread_with_knife.png", 
     "https://pixabay.com/static/uploads/photo/2015/09/13/00/12/chicken-937584_960_720.jpg", 
     "http://publicdomainvectors.org/photos/bowl-of-steaming-soup-01.png", 
     "https://pixabay.com/static/uploads/photo/2014/04/02/10/38/fish-304097_960_720.png", 
     "http://publicdomainvectors.org/photos/Erbsen-lineart.png" 
}; 

適配器:

public class CategoryImageAdapter extends BaseAdapter implements OnClickListener { 

private Activity activity; 
private String[] data; 
private static LayoutInflater inflater = null; 
public ImageLoader imageLoader; 

public CategoryImageAdapter(Activity a, String[] d) { 
    activity = a; 
    data = d; 
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    imageLoader = new ImageLoader(activity.getApplicationContext()); 

} 


@Override 
public int getCount() { 
    return data.length; 
} 

@Override 
public Object getItem(int position) { 
    return position; 
} 

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

@Override 
public void onClick(View v) { 

} 

public static class ViewHolder { 
    public TextView text; 
    public ImageButton imageButton; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View vi=convertView; 
    ViewHolder holder; 
    if(convertView == null){ 
     vi = inflater.inflate(R.layout.ingcategoryrow, null); 
     holder = new ViewHolder(); 
     holder.text = (TextView)vi.findViewById(R.id.textView2); 
     holder.imageButton=(ImageButton)vi.findViewById(R.id.imageButton2); 
     vi.setTag(holder); 
    } 
    else 
     holder=(ViewHolder)vi.getTag(); 

    holder.text.setText("Title " + position); 
    ImageButton imageButton = holder.imageButton; 
    imageLoader.DisplayImage(data[position], imageButton); 
    vi.setOnClickListener(new OnItemClickListener(position)); 
    return vi; 
} 

private class OnItemClickListener implements OnClickListener{ 
    private int mPosition; 

    OnItemClickListener(int position) { 
     mPosition = position; 
    } 


    @Override 
    public void onClick(View arg0) { 
     IngredientCategoryMain sct = (IngredientCategoryMain)activity; 
     sct.onItemClick(mPosition); 

    } 
} 

}

+0

你是說'position'的值直接映射到字符串數組中的同一個索引嗎?所以如果位置是0,字符串值是1? –

+0

是的,我該怎麼做 –

回答

0

這裏工作我的代碼,請使用此代碼:

String.xml

<string-array name="titles"> 
     <item>1</item> 
     <item>2</item> 
     <item>3</item> 
     <item>4</item> 
     <item>5</item> 
     <item>6</item> 
     <item>7</item> 
     <item>8</item> 
     <item>9</item> 
     <item>10</item> 
     <item>11</item> 
     <item>12</item> 
    </string-array> 

.java文件

public class MainActivity extends Activity { 

    ListView lv; 
    String[] title; 
    CategoryImageAdapter adapter; 

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

     lv=(ListView) findViewById(R.id.listView1); 

     title=getResources().getStringArray(R.array.titles); 

     adapter=new CategoryImageAdapter(MainActivity.this, title); 
     lv.setAdapter(adapter); 

    } 

    public class CategoryImageAdapter extends BaseAdapter implements OnClickListener { 

     private Activity activity; 
     private String[] data; 
     private LayoutInflater inflater = null; 

     public CategoryImageAdapter(Activity a, String[] d) { 
      activity = a; 
      data = d; 
      inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     } 


     @Override 
     public int getCount() { 
      return data.length; 
     } 

     @Override 
     public Object getItem(int position) { 
      return position; 
     } 

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

     @Override 
     public void onClick(View v) { 

     } 


     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View vi=convertView; 
      ViewHolder holder; 
      if(convertView == null){ 
       vi = inflater.inflate(R.layout.row_lv, null); 
       holder = new ViewHolder(); 
       holder.text = (TextView)vi.findViewById(R.id.textView1); 
       vi.setTag(holder); 
      } 
      else 
       holder=(ViewHolder)vi.getTag(); 

      holder.text.setText("Title " + data[position]); 

      return vi; 
     } 
    } 
     public class ViewHolder { 
      public TextView text; 

     } 

} 
+0

這裏是我的.java文件 –

+0

public class IngredientCategoryMain extends Activity { ListView list; CategoryImageAdapter適配器; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_ingredient_category_main); list =(ListView)findViewById(R.id.listView); adapter = new CategoryImageAdapter(this,mStrings); 列表。setAdapter(適配器); –

+0

如何將上述代碼添加到我當前的.java文件中。我嘗試了很多次,但仍然失敗。 –

0

首先得到該陣列

String[] mTestArray = getResources().getStringArray(R.array.titles);  

然後用它作爲

holder.text.setText(mTestArray[position] + position); 

讓您的數組中一樣的構造函數:

public CategoryImageAdapter(Activity a, String[] d) { 
    activity = a; 
    data = d; 
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    imageLoader = new ImageLoader(activity.getApplicationContext()); 
    mTestArray = getResources().getStringArray(R.array.testArray); 
} 
+0

我應該調用String [] mTestArray = getResources()。getStringArray(R.array.testArray);在公共視圖下getView? –

+0

根本沒有。因爲在滾動listview時多次調用view,所以在適配器構造函數中調用它。 –

+0

我仍然無法做到,如果你能具體在哪裏添加這些代碼,我會很感激。 –