Android Fragment FragmentStatePagerAdapter更改TextView的背景颜色,但仅在创建时更改
由于我是Android类型的新手,我希望你能在这里帮我一把: 该应用程序使用由FragmentManager管理的不同片段。该类由FragmentStatePagerAdapter扩展 如果我使用这个特定片段:Android Fragment FragmentStatePagerAdapter更改TextView的背景颜色,但仅在创建时更改,android,android-fragments,Android,Android Fragments,由于我是Android类型的新手,我希望你能在这里帮我一把: 该应用程序使用由FragmentManager管理的不同片段。该类由FragmentStatePagerAdapter扩展 如果我使用这个特定片段: import android.app.ListActivity; import android.app.ProgressDialog; import android.content.Intent; import android.graphics.Color; import android
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import com.loopj.android.http.AsyncHttpResponseHandler;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Created by welser on 22.06.2015.
*/
public class RalisierungsFragment extends ListFragment {
// Store instance variables
private String title;
private int page;
public int value = 111;
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PID = "pid";
private static final String TAG_DESCRIPTION = "description";
private static final String TAG_NAME = "name";
private static final String TAG_PRICE = "price";
private static final String TAG_DATUM = "created_at";
static int [] colors = new int[] {0xF0FFFF, 0xD3D3D3 };
// Hashmap for ListView
ArrayList<HashMap<String, String>>productsList = new ArrayList<HashMap<String, String>>();
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
// url to get all products list
//private static String url_all_products = "http://novaten.cloud.hs-furtwangen.de/phpandroid/get_all_products.php";
private static String url_all_products = "http://141.28.100.152/phpandroid/get_all_products.php";
// products JSONArray
JSONArray products = null;
public TextView text;
// newInstance constructor for creating fragment with arguments
public static RalisierungsFragment newInstance(int page, String title) {
RalisierungsFragment rs = new RalisierungsFragment();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("someTitle", title);
rs.setArguments(args);
return rs;
}
// Store instance variables based on arguments passed
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
page = getArguments().getInt("someInt", 0);
title = getArguments().getString("someTitle");
text = (TextView) getActivity().findViewById(R.id.name);
// Loading products in Background Thread
new LoadAllProducts().execute();
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity()); // abgendert
pDialog.setMessage("Loading data. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
public String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ",value + "");
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
String price = c.getString(TAG_PRICE);
String date = c.getString(TAG_DATUM);
String description = c.getString(TAG_DESCRIPTION);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
map.put(TAG_DATUM, date);
map.put(TAG_PRICE, price);
map.put(TAG_DESCRIPTION, description);
set(products.length());
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
getActivity().runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
getActivity(), productsList,
R.layout.list_item, new String[]{TAG_PID,
TAG_NAME, TAG_PRICE, TAG_DATUM,TAG_DESCRIPTION},
new int[]{R.id.pid, R.id.name, R.id.price, R.id.datum, R.id.description});
//text.setBackgroundColor(Color.GREEN);
setListAdapter(adapter);
}
});
}
public void set(int i){
value = i;
}
}
}
导入android.app.ListActivity;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.graphics.Color;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.support.v4.app.FragmentTransaction;
导入android.support.v4.app.ListFragment;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.ImageView;
导入android.widget.ListAdapter;
导入android.widget.ListView;
导入android.widget.simpledapter;
导入android.widget.TextView;
导入com.loopj.android.http.AsyncHttpResponseHandler;
导入org.apache.http.NameValuePair;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
/**
*welser于2015年6月22日创建。
*/
公共类RalisierungsFragment扩展了ListFragment{
//存储实例变量
私有字符串标题;
私人网页;
公共int值=111;
//JSON节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串TAG_PRODUCTS=“PRODUCTS”;
私有静态最终字符串标记_PID=“PID”;
私有静态最终字符串标记_DESCRIPTION=“DESCRIPTION”;
私有静态最终字符串标记_NAME=“NAME”;
私有静态最终字符串标记_PRICE=“PRICE”;
私有静态最终字符串标记_DATUM=“created_at”;
静态int[]颜色=新int[]{0xF0FFFF,0xD3D3};
//ListView的Hashmap
ArrayListproductsList=新的ArrayList();
//进度对话框
私人对话;
//创建JSON解析器对象
JSONParser jParser=新的JSONParser();
//获取所有产品列表的url
//私有静态字符串url\u所有产品=”http://novaten.cloud.hs-furtwangen.de/phpandroid/get_all_products.php";
私有静态字符串url\u所有产品=”http://141.28.100.152/phpandroid/get_all_products.php";
//产品JSONArray
JSONArray产品=null;
公共文本查看文本;
//用于创建带有参数的片段的newInstance构造函数
公共静态RalisierungsFragment newInstance(整型页面,字符串标题){
RalisierungsFragment rs=新的RalisierungsFragment();
Bundle args=新Bundle();
args.putInt(“someInt”,第页);
args.putString(“someTitle”,title);
rs.setArguments(args);
返回rs;
}
//根据传递的参数存储实例变量
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
page=getArguments().getInt(“someInt”,0);
title=getArguments().getString(“someTitle”);
text=(TextView)getActivity().findViewById(R.id.name);
//在后台线程中加载产品
新建LoadAllProducts().execute();
}
/**
*通过发出HTTP请求加载所有产品的后台异步任务
* */
类LoadAllProducts扩展了AsyncTask{
/**
*在启动后台线程显示进度对话框之前
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(getActivity());//abgendert
pDialog.setMessage(“正在加载数据,请稍候…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
/**
*从url获取所有产品
* */
公共字符串doInBackground(字符串…args){
//建筑参数
List params=new ArrayList();
//从URL获取JSON字符串
JSONObject json=jParser.makeHttpRequest(url_all_products,“GET”,params);
//检查日志cat中的JSON响应
Log.d(“所有产品:,价值+”);
试一试{
//检查成功标签
int success=json.getInt(TAG_success);
如果(成功==1){
//发现的产品
//获取一系列产品
products=json.getJSONArray(TAG_products);
//在所有产品中循环
对于(int i=0;ivalue
地图放置(标签PID,id);
地图放置(标签名称、名称);
地图放置(标签基准、日期);
地图出售(标签价格、价格);
地图放置(标签描述,描述);
set(products.length());
//将哈希列表添加到ArrayList
productsList.add(地图);
}
}否则{
//没有发现任何产品
//启动添加新产品活动
}
public class YourAdapter extends BaseAdapter {
private String[] mStrings;//the array to display
private LayoutInflater mInflater;
private YourAdapter(Context context, String[] datas) {
mInflater = LayoutInflater.from(context);
mStrings = datas;
}
@Override
public int getCount() {
return mStrings.length();
}
@Override
public long getItemIdAtPosition(int position) {
return position;
}
@Override
public Object getItemAtPosition(int position) {
return mStrings[position];
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//the following line is bad in terme of oprimization, you should use a holder, but it is a bit too long to write here, see the link below for example
convertView = mInflater.inflate(R.layout.your_row);
//set the datas and the colors to the row
TextView textView = (TextView) convertView.findViewById(R.id.your_text_view_id);
textView.setText(mStrings[position]);
convertView.setBackgroundColor(R.color.your_color);//you can change the color following the position or the string that you put in
return convertView;
}
}
YourAdapter adapter = new YourAdapter(this, productList);
listView.setAdapter(adapter);
ListAdapter adapter = new SimpleAdapter(
getActivity(), productsList,
R.layout.list_item, new String[]{TAG_PID,
TAG_NAME, TAG_PRICE, TAG_DATUM,TAG_DESCRIPTION},
new int[]{R.id.pid, R.id.name, R.id.price, R.id.datum, R.id.description}){
@Override
public View getView (int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if ("major".equals(productsList.get(position).get(TAG_NAME))){
view.setBackgroundColor(Color.BLUE);
} else if ("cirtical".equals(productsList.get(position).get(TAG_NAME))){
view.setBackgroundColor(Color.RED);
} else if ("warning".equals(productsList.get(position).get(TAG_NAME))) {
view.setBackgroundColor(Color.RED);
} else if ("info".equals(productsList.get(position).get(TAG_NAME))) {
view.setBackgroundColor(Color.GREEN);
} else {
view.setBackgroundColor(Color.BLACK);
}
return view;
}
};
setListAdapter(adapter);