你好,我在我的Android片段的簡單列表視圖我怎麼能我如何添加顏色,背景或動畫到ListView
編輯 我要添加2交替的背景圖像添加顏色,背景或動畫 感謝EXP: ROW1圖像1,2行圖像2,ROW3圖像1,ROW4:圖像2 ........ 第一個錯誤是:不能解析符號的ArrayList
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_users, container, false);
listView = (ListView) rootview.findViewById(R.id.listView);
listView.setOnItemClickListener(this);
getJSON();
return rootview;
}
private void showUser(){
JSONObject jsonObject = null;
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
try {
jsonObject = new JSONObject(JSON_STRING);
JSONArray result = jsonObject.getJSONArray(TAG_JSON_ARRAY);
for(int i = 0; i<result.length(); i++){
JSONObject jo = result.getJSONObject(i);
String name = jo.getString("Nom");
String email = jo.getString("Email");
String login = jo.getString("Login");
HashMap<String,String> employees = new HashMap<>();
employees.put("name",name);
employees.put("email",email);
employees.put("login",login);
list.add(employees);
}
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(
getActivity(), list, R.layout.list_row,
new String[]{"name","email"},
new int[]{R.id.nom, R.id.email2});
AboutAdapter aboutAdapter = new AboutAdapter(UsersFragment.this, arrayList);
listView.setAdapter(aboutAdapter);
listView.setAdapter(adapter);
}
private void getJSON() {
class GetJSON extends AsyncTask<Void, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(getActivity(), "Fetching Data", "Wait...", false, false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
JSON_STRING = s;
showUser();
}
@Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequest(URL);
return s;
}
}
GetJSON gj = new GetJSON();
gj.execute();
}
fragment_user.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:drawSelectorOnTop="false"/>
</FrameLayout>
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/nom"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/email2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
AboutAdapter.java 第二誤差錯誤:(6,23)字符串類型不允許(在 '可拉伸',值爲 'image1的')。 我如何使用塞康圖像2
public class AboutAdapter extends BaseAdapter {
private LayoutInflater inflater;
private Context mcontext;
private String[] arrayList;
public AboutAdapter(Context context, String[] arrayList) {
this.mcontext = context;
this.arrayList= arrayList;
inflater = LayoutInflater.from(mcontext);
}
@Override
public int getCount() {
return arrayList.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder {
ImageView imag;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
Holder holder;
if (vi == null) {
vi = inflater.inflate(R.layout.list_row, null);
holder = new Holder();
holder.imag = (ImageView) vi
.findViewById(R.id.image1);
vi.setTag(holder);
} else {
holder = (Holder) vi.getTag();
}
// same as for image pass array of image and set
holder.imag.setBackgroundResource(R.drawable.list_selector);
return vi;
}
}
list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="image1"/>
<item android:state_pressed="true"
android:drawable="@color/itemselected" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="@color/itemselected" />
</selector>
請按照[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)包含相關代碼 – MikeT
那麼,您的問題是否已解決? –
還沒有。感謝您的跟進,我用我的代碼 –