我已经为JSOUP库构建了一个简单的应用程序。
我从网页获得的数据被添加到一个数组中,我用作GridView的适配器。
以下是一些代码:
活动:
public class test extends AppCompatActivity {
String[] custom = new String[18];
String[] data = new String[18];
String datawiki = "";
LinearLayout linear_layout_progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
linear_layout_progress = (LinearLayout) findViewById(R.id.linear_layout_progress);
new Title().execute();
}
private void fill() {
GridView gridView = (GridView) findViewById(R.id.grid);
gridView.setAdapter(new dataAdapter(this, custom, data));
gridView.setOnTouchListener(new View.OnTouchListener() {
// Setting on Touch Listener for handling the touch inside ScrollView
@Override
public boolean onTouch(View v, MotionEvent event) {
// Disallow the touch request for parent scroll on touch of child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
TextView textView = (TextView)findViewById(R.id.txtwiki);
textView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
textView.setText(datawiki);
}
private class Title extends AsyncTask<Void, Void, Void> {
String title;
boolean gone = false;
@Override
protected void onPreExecute() {
// SHOW THE SPINNER WHILE LOADING FEEDS
linear_layout_progress.setVisibility(View.VISIBLE);
}
@Override
protected Void doInBackground(Void... params) {
try {
int relasedata = -2;
int m = 0;
int i = -1;
String url = "https://en.wikipedia.org/wiki/";
String a = "The_Elder_Scrolls_V:_Skyrim";
Document doc = Jsoup.connect(url + a).get();
Element table = doc.select("table").get(0); //select the first table.
Elements th = table.select("th");
Elements td = table.select("td");
for (Element element : th){
if (m != 0) {
custom[++i] = element.text();
if (element.text().contains("Release")) {
relasedata = i;
}
}
m++;
}
i=0;
m=0;
int x = 0;
for (Element element : td) {
if (m != 0) {
// Elements aa = element.select("a");
if (i == relasedata){
data[i] ="• " + td.get(i+1).text();
} else {
if (data[i] == null) {
data[i] = "• " + element.text();
} else {
data[i] = data[i] + " • " + element.text();
}
}
i++;
}
m++;
}
Elements p = doc.select("p");
datawiki= p.get(0).text();
gone = true;
} catch (IOException e) {
gone=false;
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
linear_layout_progress.setVisibility(View.GONE);
if (!gone) {
Toast toast = Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_SHORT);
toast.show();
}
fill();
}
@Override
protected void onCancelled(){
this.cancel(true);
}
}
@Override
public void onBackPressed() {
super.finish();
}
}
这是基本适配器:
public class dataAdapter extends BaseAdapter {
private Context context;
private final String[] customC;
private final String[] dataD;
public dataAdapter(Context context, String[] customC, String[] dataD) {
this.context = context;
this.customC = customC;
this.dataD = dataD;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.single_grid, null);
// set value into textview
TextView custom = (TextView) gridView
.findViewById(R.id.txt_custom);
custom.setText(customC[position]);
// set image based on selected text
final TextView data = (TextView) gridView
.findViewById(R.id.txt_data);
data.setText(dataD[position]);
data.post(new Runnable() {
@Override
public void run() {
if (data.getLineCount() > 2){
data.setTextColor(Color.GREEN);
}
}
});
} else {
gridView = (View) convertView;
}
return gridView;
}
@Override
public int getCount() {
return customC.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
当gridview的高度较小,无法对所有数据进行竞争时,我的问题就出现了。 喜欢这样:
要显示所有数据,用户应滚动gridView,但是当他这样做时,数据将改变位置。这是一个显示此问题的视频: Click Here
那我怎么能这样做呢?这个位置总是一样的?